def test_stopping_cancels_startup(self): service = RegionService(sentinel.ipcWorker) # Return an inert Deferred from the listen() call. endpoints = self.patch(service, "endpoints", [[Mock()]]) endpoints[0][0].listen.return_value = Deferred() service.startService() service.stopService() def check(_): # The CancelledError is suppressed. self.assertThat(service.ports, HasLength(0)) return service.starting.addCallback(check)
def test_stopping_logs_errors_when_closing_connections(self): service = RegionService(sentinel.ipcWorker) service.starting = Deferred() service.starting.addErrback( lambda failure: failure.trap(CancelledError)) service.factory.protocol = HandshakingRegionServer connections = { service.factory.buildProtocol(None), service.factory.buildProtocol(None), } for conn in connections: transport = self.patch(conn, "transport") transport.loseConnection.side_effect = OSError("broken") # Pretend it's already connected. service.connections[conn.ident].add(conn) logger = self.useFixture(TwistedLoggerFixture()) # stopService() completes without returning an error. yield service.stopService() # Connection-specific errors are logged. self.assertDocTestMatches( """\ Failure when closing RPC connection. Traceback (most recent call last): ... builtins.OSError: broken ... Failure when closing RPC connection. Traceback (most recent call last): ... builtins.OSError: broken """, logger.dump(), )
def test_stopping_when_start_up_failed(self): service = RegionService(sentinel.ipcWorker) # Ensure that endpoint.listen fails with a obvious error. exception = ValueError("This is a very naughty boy.") endpoints = self.patch(service, "endpoints", [[Mock()]]) endpoints[0][0].listen.return_value = fail(exception) service.startService() # The test is that stopService() succeeds. return service.stopService()
def test_worker_registers_and_unregister_rpc_connection(self): yield deferToDatabase(load_builtin_scripts) pid = random.randint(1, 512) self.patch(os, "getpid").return_value = pid ( master, connected, disconnected, ) = self.make_IPCMasterService_with_wrap() rpc_started = self.wrap_async_method(master, "registerWorkerRPC") yield master.startService() worker = IPCWorkerService(reactor, socket_path=self.ipc_path) rpc = RegionService(worker) yield worker.startService() yield rpc.startService() yield connected.get(timeout=2) yield rpc_started.get(timeout=2) rackd = yield deferToDatabase(factory.make_RackController) connid = str(uuid.uuid4()) address = factory.make_ipv4_address() port = random.randint(1000, 5000) yield worker.rpcRegisterConnection(connid, rackd.system_id, address, port) def getConnection(): region = RegionController.objects.get_running_controller() process = RegionControllerProcess.objects.get(region=region, pid=pid) endpoint = RegionControllerProcessEndpoint.objects.get( process=process, address=address, port=port) return RegionRackRPCConnection.objects.filter( endpoint=endpoint, rack_controller=rackd).first() connection = yield deferToDatabase(getConnection) self.assertIsNotNone(connection) self.assertEqual( {connid: (rackd.system_id, address, port)}, master.connections[pid]["rpc"]["connections"], ) yield worker.rpcUnregisterConnection(connid) connection = yield deferToDatabase(getConnection) self.assertIsNone(connection) yield rpc.stopService() yield worker.stopService() yield disconnected.get(timeout=2) yield master.stopService()
def test_stopping_closes_connections_cleanly(self): service = RegionService(sentinel.ipcWorker) service.starting = Deferred() service.starting.addErrback( lambda failure: failure.trap(CancelledError)) service.factory.protocol = HandshakingRegionServer connections = { service.factory.buildProtocol(None), service.factory.buildProtocol(None), } for conn in connections: # Pretend it's already connected. service.connections[conn.ident].add(conn) transports = {self.patch(conn, "transport") for conn in connections} yield service.stopService() self.assertThat( transports, AllMatch( AfterPreprocessing(attrgetter("loseConnection"), MockCalledOnceWith())))
def test_worker_registers_rpc_endpoints(self): yield deferToDatabase(load_builtin_scripts) pid = random.randint(1, 512) self.patch(os, "getpid").return_value = pid ( master, connected, disconnected, ) = self.make_IPCMasterService_with_wrap() rpc_started = self.wrap_async_method(master, "registerWorkerRPC") yield master.startService() worker = IPCWorkerService(reactor, socket_path=self.ipc_path) rpc = RegionService(worker) yield worker.startService() yield rpc.startService() yield connected.get(timeout=2) yield rpc_started.get(timeout=2) def getEndpoints(): region = RegionController.objects.get_running_controller() process = RegionControllerProcess.objects.get(region=region, pid=pid) return set([(endpoint.address, endpoint.port) for endpoint in ( RegionControllerProcessEndpoint.objects.filter( process=process))]) endpoints = yield deferToDatabase(getEndpoints) self.assertEqual( master._getListenAddresses(master.connections[pid]["rpc"]["port"]), endpoints, ) yield rpc.stopService() yield worker.stopService() yield disconnected.get(timeout=2) yield master.stopService()