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_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_start_up_binds_first_of_real_endpoint_options(self): service = RegionService(sentinel.ipcWorker) # endpoint_1.listen(...) will bind to a random high-numbered port. endpoint_1 = TCP4ServerEndpoint(reactor, 0) # endpoint_2.listen(...), if attempted, will crash because only root # (or a user with explicit capabilities) can do stuff like that. It's # a reasonable assumption that the user running these tests is not # root, but we'll check the port number later too to be sure. endpoint_2 = TCP4ServerEndpoint(reactor, 1) service.endpoints = [[endpoint_1, endpoint_2]] yield service.startService() self.addCleanup(wait_for_reactor(service.stopService)) # A single port has been bound. self.assertThat( service.ports, MatchesAll(HasLength(1), AllMatch(IsInstance(tcp.Port))), ) # The port is not listening on port 1; i.e. a belt-n-braces check that # endpoint_2 was not used. [port] = service.ports self.assertThat(port.getHost().port, Not(Equals(1)))
def test_start_up_can_be_cancelled(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() self.assertThat(service.starting, IsInstance(Deferred)) service.starting.cancel() def check(port): self.assertThat(port, Is(None)) self.assertThat(service.ports, HasLength(0)) return service.stopService() return service.starting.addCallback(check)
def test_start_up_binds_first_successful_of_endpoint_options(self): service = RegionService(sentinel.ipcWorker) endpoint_broken = Mock() endpoint_broken.listen.return_value = fail(factory.make_exception()) endpoint_okay = Mock() endpoint_okay.listen.return_value = succeed(sentinel.port) service.endpoints = [[endpoint_broken, endpoint_okay]] yield service.startService() self.assertThat(service.ports, Equals([sentinel.port]))
def test_start_up_binds_first_of_endpoint_options(self): service = RegionService(sentinel.ipcWorker) endpoint_1 = Mock() endpoint_1.listen.return_value = succeed(sentinel.port1) endpoint_2 = Mock() endpoint_2.listen.return_value = succeed(sentinel.port2) service.endpoints = [[endpoint_1, endpoint_2]] yield service.startService() self.assertThat(service.ports, Equals([sentinel.port1]))
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_starting_and_stopping_the_service(self): service = RegionService(sentinel.ipcWorker) self.assertThat(service.starting, Is(None)) service.startService() self.assertThat(service.starting, IsInstance(Deferred)) def check_started(_): # Ports are saved as private instance vars. self.assertThat(service.ports, HasLength(1)) [port] = service.ports self.assertThat(port, IsInstance(tcp.Port)) self.assertThat(port.factory, IsInstance(Factory)) self.assertThat(port.factory.protocol, Equals(RegionServer)) return service.stopService() service.starting.addCallback(check_started) def check_stopped(ignore, service=service): self.assertThat(service.ports, Equals([])) service.starting.addCallback(check_stopped) return service.starting
def test_startService_returns_Deferred(self): service = RegionService(sentinel.ipcWorker) # Don't configure any endpoints. self.patch(service, "endpoints", []) d = service.startService() self.assertThat(d, IsInstance(Deferred)) # It's actually the `starting` Deferred. self.assertIs(service.starting, d) def started(_): return service.stopService() return d.addCallback(started)
def test_start_up_errors_are_logged(self): ipcWorker = MagicMock() service = RegionService(ipcWorker) # Ensure that endpoint.listen fails with a obvious error. exception = ValueError("This is not the messiah.") endpoints = self.patch(service, "endpoints", [[Mock()]]) endpoints[0][0].listen.return_value = fail(exception) logged_failures_expected = [ AfterPreprocessing((lambda failure: failure.value), Is(exception)) ] with TwistedLoggerFixture() as logger: yield service.startService() self.assertThat(logger.failures, MatchesListwise(logged_failures_expected))
def test_start_up_logs_failure_if_all_endpoint_options_fail(self): service = RegionService(sentinel.ipcWorker) error_1 = factory.make_exception_type() error_2 = factory.make_exception_type() endpoint_1 = Mock() endpoint_1.listen.return_value = fail(error_1()) endpoint_2 = Mock() endpoint_2.listen.return_value = fail(error_2()) service.endpoints = [[endpoint_1, endpoint_2]] with TwistedLoggerFixture() as logger: yield service.startService() self.assertDocTestMatches( """\ RegionServer endpoint failed to listen. Traceback (most recent call last): ... %s: """ % fullyQualifiedName(error_2), logger.output)
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()