Swarm-NG  1.1
bdb_concurrent.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 ## @file bdb_concurrent.py Testing that concurrent reads from a BDB log file works
4 #
5 # swarm command is run to generate a log file and it is scheduled to remove it
6 # immedaitely after the integration is done.
7 #
8 # this test only passes if the query is quick enough to open the file and get the result.
9 #
10 #
11 #
12 
13 import swarmng
14 import sys
15 import os
16 import time
17 import psutil
18 import swarmng.logdb
19 from swarmng.range_type import Range
20 from swarmng.query import truncate
21 from argparse import Namespace
22 import unittest
23 from bsddb3.db import DBError
24 
25 class BDBConcurrencyTest(unittest.TestCase):
26  def runTest(self):
27  ## Setting up the output log file
28  output_file_name='Testing/testing_log.db'
29  final_time = 100
30 
31  try:
32  os.remove(output_file_name)
33  except OSError:
34  pass
35 
36  def do_query():
37  d = swarmng.logdb.IndexedLogDB(output_file_name)
38  q = list(truncate(1,d.final_conditions(Range.single(0))))
39  self.assertEqual(len(q), 1)
40  key, record = q[0]
41  return record.time
42 
43 
44  def do_integration():
45  cfg = swarmng.config(
46  nsys=64,
47  nbod=3,
48  log_writer='bdb',
49  log_output_db=output_file_name,
50  log_interval=1,
51  integrator='hermite_cpu_log',
52  time_step=0.01,
53  nogpu=1
54  );
55  ref = swarmng.generate_ensemble( cfg )
56  ens = ref.clone()
57 
58  swarmng.init(cfg)
59 
60  integ = swarmng.Integrator.create( cfg )
61 
62  integ.ensemble = ens
63 
64  swarmng.sync
65  for i in range(1,final_time+1):
66  integ.destination_time = i
67  integ.integrate()
68 
69 
70  pid = os.fork()
71  if pid == 0:
72  do_integration()
73  os.abort()
74  else:
75  child = psutil.Process(pid)
76 
77  time.sleep(.05)
78  counter = 0
79  while child.status != psutil.STATUS_ZOMBIE:
80  try:
81  # Get the last_time for system 0, it
82  # should be greater than 0
83  last_time = do_query()
84  self.assertGreater(last_time,0)
85 
86  # Only increment the counter if it is not at
87  # the final time
88  if(last_time < final_time):
89  counter += 1
90  except DBError as e:
91  print("Querying failed", e)
92  time.sleep(.05)
93 
94  # The query must have run before the integration
95  # over at least once
96  assert(counter > 0)
97 
98  # Wait for the child process to finish
99  os.waitpid(pid, 0)