def test_worker_spawned_as_separate_process(self): outfile = tempfile.NamedTemporaryFile() outfile.close() params = Params() params.report = outfile.name + "%s" params.concurrency = 15 params.worker_count = 15 params.iterations = 1 saved_spawn_worker = apiritif.loadgen.spawn_worker apiritif.loadgen.spawn_worker = mock_spawn_worker try: sup = Supervisor(params) sup.start() while sup.is_alive(): time.sleep(0.1) process_ids = [] for i in range(params.worker_count): with open(params.report % i) as f: process_ids.extend(f.readlines()) self.assertEqual(params.worker_count, len(set(process_ids))) finally: apiritif.loadgen.spawn_worker = saved_spawn_worker for i in range(params.worker_count): os.remove(params.report % i)
def test_supervisor(self): outfile = tempfile.NamedTemporaryFile() params = Params() params.tests = dummy_tests params.report = outfile.name + "%s" params.concurrency = 9 params.iterations = 5 sup = Supervisor(params) sup.start() while sup.is_alive(): time.sleep(1)
def test_empty_supervisor(self): outfile = tempfile.NamedTemporaryFile() params = Params() params.tests = [] params.report = outfile.name + "%s" params.concurrency = 9 params.iterations = 5 sup = Supervisor(params) sup.start() while sup.is_alive(): time.sleep(1) self.assertEqual(CLOSE, sup.workers._state)
def test_writers_x3(self): # writers must: # 1. be the same for threads of one process # 2. be set up only once # 3. be different for different processes def dummy_worker_init(self, params): """ :type params: Params """ super(Worker, self).__init__(params.concurrency) self.params = params store.writer = DummyWriter(self.params.report, self.params.workers_log) outfile = tempfile.NamedTemporaryFile() outfile.close() params = Params() # use this log to spy on writers workers_log = outfile.name + '-workers.log' params.workers_log = workers_log params.tests = [ os.path.join(RESOURCES_DIR, "test_smart_transactions.py") ] params.report = outfile.name + "%s" # it causes 2 processes and 3 threads (totally) params.concurrency = 3 params.worker_count = 2 params.iterations = 2 saved_worker_init = Worker.__init__ Worker.__init__ = dummy_worker_init try: sup = Supervisor(params) sup.start() while sup.is_alive(): time.sleep(1) with open(workers_log) as log: writers = log.readlines() self.assertEqual(2, len(writers)) self.assertNotEqual(writers[0], writers[1]) finally: Worker.__init__ = saved_worker_init os.remove(workers_log) for i in range(params.worker_count): os.remove(params.report % i)
def test_handlers(self): # handlers must: # 1. be unique for thread # 2. be set up every launch of test suite def log_line(line): with open(thread.handlers_log, 'a') as log: log.write("%s\n" % line) def mock_get_handlers(): transaction_handlers = thread.get_from_thread_store( 'transaction_handlers') if not transaction_handlers: transaction_handlers = {'enter': [], 'exit': []} length = "%s/%s" % (len(transaction_handlers['enter']), len(transaction_handlers['exit'])) log_line("get: {pid: %s, idx: %s, iteration: %s, len: %s}" % (os.getpid(), thread.get_index(), thread.get_iteration(), length)) return transaction_handlers def mock_set_handlers(handlers): log_line("set: {pid: %s, idx: %s, iteration: %s, handlers: %s}," % (os.getpid(), thread.get_index(), thread.get_iteration(), handlers)) thread.put_into_thread_store(transaction_handlers=handlers) outfile = tempfile.NamedTemporaryFile() outfile.close() params = Params() # use this log to spy on writers handlers_log = outfile.name + '-handlers.log' thread.handlers_log = handlers_log params.tests = [ os.path.join(RESOURCES_DIR, "test_smart_transactions.py") ] params.report = outfile.name + "%s" # it causes 2 processes and 3 threads (totally) params.concurrency = 3 params.worker_count = 2 params.iterations = 2 saved_get_handlers = apiritif.get_transaction_handlers saved_set_handlers = apiritif.set_transaction_handlers apiritif.get_transaction_handlers = mock_get_handlers apiritif.set_transaction_handlers = mock_set_handlers try: sup = Supervisor(params) sup.start() while sup.is_alive(): time.sleep(1) with open(handlers_log) as log: handlers = log.readlines() self.assertEqual(36, len(handlers)) self.assertEqual( 6, len([ handler for handler in handlers if handler.startswith('set') ])) self.assertEqual( 0, len([ handler for handler in handlers if handler.endswith('2/2}') ])) finally: apiritif.get_transaction_handlers = saved_get_handlers apiritif.set_transaction_handlers = saved_set_handlers os.remove(handlers_log) for i in range(params.worker_count): os.remove(params.report % i)