def make_req(self, *args): proc = Process(target=self.start_service, args=(self.addr, )) proc.start() self.client = Client(self.addr) res, err = self.client.call('divide', *args) proc.terminate() return res, err
def _run_test_with_encoder(self, encoder): proc = Process(target=self.start_service, args=(self.addr, encoder)) proc.start() self.client = Client(self.addr, encoder=encoder) res, err = self.client.call('divide', 10, 2) proc.terminate() expected = 5 self.assertEqual(expected, res)
def make_req(self, *args): proc = Process(target=self.start_service, args=(self.addr,)) proc.start() self.client = Client(self.addr) res, err = self.client.call('divide', *args) proc.terminate() return res, err
def main(): clients = [] for port in range(5001, 5008): clients.append(Client('tcp://127.0.0.1:{}'.format(port))) for c in clients: start = time.time() result, error = c.call('test') print('{:18} - {:.2f} ms'.format(result, (time.time() - start) * 1000))
def _run_test_with_encoder(self, encoder): proc = Process( target=self.start_service, args=(self.addr, encoder) ) proc.start() self.client = Client(self.addr, encoder=encoder) res, err = self.client.call('divide', 10, 2) proc.terminate() expected = 5 self.assertEqual(expected, res)
def run(N, addr): # Fork service service_process = Process(target=start_service, args=(addr, N)) service_process.start() time.sleep(0.1) # Wait for service to connect # Create client and make reqs c = Client(addr) bench(c, N) c.sock.close() time.sleep(0.2) service_process.terminate()
class TestEncodingDecoding(BaseTestCase): """ Test the communication between endpoints using various various mechanisms """ def _run_test_with_encoder(self, encoder): proc = Process(target=self.start_service, args=(self.addr, encoder)) proc.start() self.client = Client(self.addr, encoder=encoder) res, err = self.client.call('divide', 10, 2) proc.terminate() expected = 5 self.assertEqual(expected, res) def test_json_encoder(self): self._run_test_with_encoder(encoder.JSONEncoder()) def test_msgpack_encoder(self): self._run_test_with_encoder(encoder.MsgPackEncoder())
class TestTCPProtocol(BaseTestCase): def make_req(self, *args): proc = Process(target=self.start_service, args=(self.addr, )) proc.start() self.client = Client(self.addr) res, err = self.client.call('divide', *args) proc.terminate() return res, err def test_req_rep_w_success(self): res, err = self.make_req(6, 2) self.assertEqual(3, res) self.assertTrue(err is None) def test_req_rep_w_error(self): res, err = self.make_req(6, 0) self.assertTrue(res is None) self.assertTrue(err is not None)
class TestTCPProtocol(BaseTestCase): def make_req(self, *args): proc = Process(target=self.start_service, args=(self.addr,)) proc.start() self.client = Client(self.addr) res, err = self.client.call('divide', *args) proc.terminate() return res, err def test_req_rep_w_success(self): res, err = self.make_req(6,2) self.assertEqual(3, res) self.assertTrue(err is None) def test_req_rep_w_error(self): res, err = self.make_req(6,0) self.assertTrue(res is None) self.assertTrue(err is not None)
class TestEncodingDecoding(BaseTestCase): """ Test the communication between endpoints using various various mechanisms """ def _run_test_with_encoder(self, encoder): proc = Process( target=self.start_service, args=(self.addr, encoder) ) proc.start() self.client = Client(self.addr, encoder=encoder) res, err = self.client.call('divide', 10, 2) proc.terminate() expected = 5 self.assertEqual(expected, res) def test_json_encoder(self): self._run_test_with_encoder(encoder.JSONEncoder()) def test_msgpack_encoder(self): self._run_test_with_encoder(encoder.MsgPackEncoder())
def make(addr): c = Client(addr) c.sock._set_recv_timeout(timeout) return c
def setUp(self): addr = 'inproc://test' self.client = Client(addr) self.service = Service(addr) self.service.register('divide', lambda x, y: x / y) self.service.register('echo', lambda x: x)
# client from nanoservice import Client c = Client('ipc:///tmp/service.sock') res, err = c.call('greet', 'John Doe') print('Greeting: {}'.format(res)) res, err = c.call('add', 2, 3) print('Addition: 2 + 3 = {}'.format(res))