def setUp(self): self.config = load_config() self.config['name'] = local_name self.config['persistent_store'] = Mock() self.replay_context = ReplayContext(**self.config) self.request_context = zmq.Context() self.request_socket = self.request_context.socket(zmq.REQ) self.request_socket.connect( self.config['replay_endpoints'][local_name])
class TestReplayContext(unittest.TestCase): def setUp(self): self.config = load_config() self.config['name'] = local_name self.config['persistent_store'] = mock.Mock() self.replay_context = ReplayContext(**self.config) self.request_context = zmq.Context() self.request_socket = self.request_context.socket(zmq.REQ) self.request_socket.connect( self.config['replay_endpoints'][local_name]) def tearDown(self): self.request_socket.close() self.replay_context.publisher.close() @requires_network def test_get_replay(self): # Setup the store to return what we ask. answer = [{'foo': 'bar'}] self.config['persistent_store'].get = mock.Mock(side_effect=[answer]) # Doesn't matter what we send as long as it is legit JSON, # since the store is mocked self.request_socket.send(u'{"id": 1}'.encode('utf-8')) self.replay_context._req_rep_cycle() rep = self.request_socket.recv_multipart() print(rep) print(answer) assert len(answer) == len(rep) for r, a in zip(rep, answer): self.assertDictEqual(json.loads(r.decode('utf-8')), a) @requires_network def test_get_error(self): # Setup the store to return what we ask. answer = ValueError('No luck!') self.config['persistent_store'].get = mock.Mock(side_effect=[answer]) # Doesn't matter what we send as long as it is legit JSON, # since the store is mocked self.request_socket.send(u'{"id": 1}'.encode('utf-8')) self.replay_context._req_rep_cycle() rep = self.request_socket.recv_multipart() print(rep) assert len(rep) == 1 and "error: 'No luck!'" == rep[0].decode('utf-8')
def setUp(self): self.config = load_config() self.config['name'] = local_name self.config['mute'] = True self.config['persistent_store'] = Mock() self.replay_context = ReplayContext(**self.config) self.replay_thread = ReplayThread(self.replay_context) self.context = zmq.Context()
def test_init_invalid_endpoint(): try: config = load_config() config['name'] = local_name config['persistent_store'] = Mock() tmp = zmq.Context() placeholder = tmp.socket(zmq.REP) placeholder.bind('tcp://*:{0}'.format( config["replay_endpoints"][local_name].rsplit(':')[-1])) context = ReplayContext(**config) finally: placeholder.close()
def test_init_missing_store(): config = load_config() context = ReplayContext(**config)
def test_init_missing_endpoint(): """ Try to initialize the context with a nonexistant service name. """ config = load_config() config['persistent_store'] = Mock() config['name'] = "failboat" context = ReplayContext(**config)