def test_spawn_server(self):
     """Test the _spawn_server method."""
     popen_calls = []
     ac = ActivationClient(self.config)
     self.patch(tcpactivation.subprocess, "Popen",
                lambda *args, **kwargs: popen_calls.append((args, kwargs)))
     ac._spawn_server()
     self.assertEqual(len(popen_calls), 1)
 def test_wait_server_timeouts(self):
     """If the server takes too long to start then timeout."""
     ac = ActivationClient(self.config)
     clock = task.Clock()
     self.patch(twisted.internet, "reactor", clock)
     self.patch(ac, "is_already_running", lambda: defer.succeed(False))
     d = ac._wait_server_active()
     clock.pump([tcpactivation.DELAY_BETWEEN_CHECKS] *
                tcpactivation.NUMBER_OF_CHECKS)
     return self.assertFailure(d, ActivationTimeoutError)
 def test_do_get_active_description_not_running(self):
     """Test _do_get_active_description method when is not running."""
     server_spawned = []
     ac = ActivationClient(self.config)
     self.patch(
         ac, "_spawn_server", lambda *args: server_spawned.append(args))
     self.patch(ac, "is_already_running", lambda: defer.succeed(False))
     self.patch(ac, "_wait_server_active", lambda: defer.succeed(None))
     result = yield ac._do_get_active_description()
     self.assertEqual(result, self.client_description)
     self.assertEqual(len(server_spawned), 1)
def ipc_client_connect(client_factory, reactor=None):
    """Connect the IPC client factory."""
    ac = ActivationClient(get_activation_config())

    if reactor is None:
        from twisted.internet import reactor

    description = yield ac.get_active_client_description()
    client = endpoints.clientFromString(reactor, description)
    port = yield client.connect(client_factory)
    defer.returnValue(port)
 def test_get_active_description_waits_classwide(self):
     """Test the get_active_description method locks classwide."""
     d = defer.Deferred()
     ac1 = ActivationClient(self.config)
     ac2 = ActivationClient(self.config)
     self.patch(ac1, "_do_get_active_description", lambda: d)
     self.patch(ac2, "_do_get_active_description",
                lambda: defer.succeed(None))
     ac1.get_active_client_description()
     d2 = ac2.get_active_client_description()
     self.assertFalse(d2.called, "The second must wait for the first.")
     d.callback(self.client_description)
     self.assertTrue(d2.called, "The second can fire after the first.")
def client_connect(client_factory, service_name, activation_cmdline,
                   description, reactor=None):
    """Connect the IPC client factory."""
    config = ActivationConfig(service_name, activation_cmdline, description)
    ac = ActivationClient(config)
    description = yield ac.get_active_client_description()

    if reactor is None:
        from twisted.internet import reactor

    client = endpoints.clientFromString(reactor, description)
    port = yield client.connect(client_factory)
    defer.returnValue(port)
def client_test(config):
    """An IRL test of the client."""
    from twisted.internet import reactor
    print("starting the client.")
    ac = ActivationClient(config)
    d = ac.get_active_client_description()

    def got_description(description):
        """The description was found."""
        print("client got server description:", description)
        reactor.stop()

    d.addCallback(got_description)
    reactor.run()
    def test_wait_server_active(self):
        """Test the _wait_server_active method."""
        ac = ActivationClient(self.config)
        clock = task.Clock()
        self.patch(twisted.internet, "reactor", clock)
        self.patch(ac, "is_already_running", lambda: defer.succeed(False))

        d = ac._wait_server_active()

        self.assertFalse(d.called, "The deferred should not be fired yet.")
        clock.advance(tcpactivation.DELAY_BETWEEN_CHECKS)
        self.assertFalse(d.called, "The deferred should not be fired yet.")
        self.patch(ac, "is_already_running", lambda: defer.succeed(True))
        clock.advance(tcpactivation.DELAY_BETWEEN_CHECKS)
        self.assertTrue(d.called, "The deferred should be fired by now.")
 def test_do_get_active_description_running(self):
     """Test the _do_get_active_description method when is running."""
     ac = ActivationClient(self.config)
     self.patch(ac, "is_already_running", lambda: defer.succeed(True))
     result = yield ac._do_get_active_description()
     self.assertEqual(result, self.client_description)