def run_server(address): with varlink.ThreadingServer(address, ServiceRequestHandler) as server: print("Listening on", server.server_address) try: server.serve_forever() except KeyboardInterrupt: pass
def do_run(self, address): server = varlink.ThreadingServer(address, ServiceRequestHandler) server_thread = threading.Thread(target=server.serve_forever) server_thread.daemon = True server_thread.start() try: with varlink.Client(address) as client, \ client.open('org.varlink.service') as _connection: info = _connection.GetInfo() self.assertEqual(len(info['interfaces']), 1) self.assertEqual(info['interfaces'][0], "org.varlink.service") self.assertEqual(info, service.GetInfo()) desc = _connection.GetInterfaceDescription( info['interfaces'][0]) self.assertEqual( desc, service.GetInterfaceDescription("org.varlink.service")) _connection.close() finally: server.shutdown() server.server_close()
def setUpClass(cls): if hasattr(socket, "AF_UNIX"): cls.address = "unix:org.varlink.certification_" \ + str(os.getpid()) \ + threading.current_thread().getName() else: cls.address = "tcp:127.0.0.1:23456" cls.server = varlink.ThreadingServer(cls.address, ServiceRequestHandler) server_thread = threading.Thread(target=cls.server.serve_forever) server_thread.daemon = True server_thread.start()
def test_service(self): address = "tcp:127.0.0.1:23451" Example.sleep_duration = 0.1 server = varlink.ThreadingServer(address, ServiceRequestHandler) server_thread = threading.Thread(target=server.serve_forever) server_thread.daemon = True server_thread.start() try: client = varlink.Client.new_with_address(address) run_client(client) with \ client.open('org.example.more', namespaced=True) as con1, \ client.open('org.example.more', namespaced=True) as con2: self.assertEqual(con1.Ping("Test").pong, "Test") it = con1.TestMore(10, _more=True) m = next(it) self.assertTrue(hasattr(m.state, 'start')) self.assertFalse(hasattr(m.state, 'end')) self.assertFalse(hasattr(m.state, 'progress')) self.assertIsNotNone(m.state.start) for i in range(0, 110, 10): m = next(it) self.assertTrue(hasattr(m.state, 'progress')) self.assertFalse(hasattr(m.state, 'start')) self.assertFalse(hasattr(m.state, 'end')) self.assertIsNotNone(m.state.progress) self.assertEqual(i, m.state.progress) if i > 50: ret = con2.Ping("Test") self.assertEqual("Test", ret.pong) m = next(it) self.assertTrue(hasattr(m.state, 'end')) self.assertFalse(hasattr(m.state, 'start')) self.assertFalse(hasattr(m.state, 'progress')) self.assertIsNotNone(m.state.end) self.assertRaises(StopIteration, next, it) con1.StopServing(_oneway=True) time.sleep(0.5) self.assertRaises(varlink.ConnectionError, con1.Ping, "Test") finally: server.shutdown() server.server_close()
def run(self): mocked_service = varlink.Service(vendor=self.vendor, product=self.product, version=self.version, url=self.url) instanciated_service = self.service_to_mock() mocked_service._set_interface(self.interface_file, instanciated_service) class ServiceRequestHandler(varlink.RequestHandler): service = mocked_service self.varlink_server = varlink.ThreadingServer(self.address, ServiceRequestHandler) self.varlink_server.serve_forever()