def test_notification_gets_added_to_notifications(self):
        socket_path = self.patch_socket_path()
        service = LeaseSocketService(sentinel.service, reactor)
        service.startService()
        self.addCleanup(service.stopService)

        # Stop the looping call to check that the notification gets added
        # to notifications.
        process_done = service.done
        service.processor.stop()
        yield process_done
        service.processor = MagicMock()

        # Create test payload to send.
        packet = {"test": factory.make_name("test")}

        # Send notification to the socket should appear in notifications.
        yield deferToThread(self.send_notification, socket_path, packet)

        # Loop until the notifications has a notification.
        for elapsed, remaining, wait in retries(5, 0.1, reactor):
            if len(service.notifications) > 0:
                break
            else:
                yield pause(wait, reactor)

        # Should have one notitication.
        self.assertEquals([packet], list(service.notifications))
    def test_processNotification_gets_called_multiple_times(self):
        socket_path = self.patch_socket_path()
        service = LeaseSocketService(sentinel.service, reactor)
        dvs = [
            DeferredValue(),
            DeferredValue(),
        ]

        # Mock processNotifcation to catch the call.
        def mock_processNotification(*args, **kwargs):
            for dv in dvs:
                if not dv.isSet:
                    dv.set(args)
                    break

        self.patch(service, "processNotification", mock_processNotification)

        # Start the service and stop it at the end of the test.
        service.startService()
        self.addCleanup(service.stopService)

        # Create test payload to send.
        packet1 = {"test1": factory.make_name("test1")}
        packet2 = {"test2": factory.make_name("test2")}

        # Send notifications to the socket and wait for notifications.
        yield deferToThread(self.send_notification, socket_path, packet1)
        yield deferToThread(self.send_notification, socket_path, packet2)
        yield dvs[0].get(timeout=10)
        yield dvs[1].get(timeout=10)

        # Packet should be the argument passed to processNotification in
        # order.
        self.assertEquals((packet1, ), dvs[0].value)
        self.assertEquals((packet2, ), dvs[1].value)
    def test_processNotification_gets_called_with_notification(self):
        socket_path = self.patch_socket_path()
        service = LeaseSocketService(sentinel.service, reactor)
        dv = DeferredValue()

        # Mock processNotifcation to catch the call.
        def mock_processNotification(*args, **kwargs):
            dv.set(args)

        self.patch(service, "processNotification", mock_processNotification)

        # Start the service and stop it at the end of the test.
        service.startService()
        self.addCleanup(service.stopService)

        # Create test payload to send.
        packet = {"test": factory.make_name("test")}

        # Send notification to the socket and wait for notification.
        yield deferToThread(self.send_notification, socket_path, packet)
        yield dv.get(timeout=10)

        # Packet should be the argument passed to processNotifcation
        self.assertEquals((packet, ), dv.value)
 def test_stopService_deletes_socket(self):
     socket_path = self.patch_socket_path()
     service = LeaseSocketService(sentinel.service, reactor)
     service.startService()
     yield service.stopService()
     self.assertThat(socket_path, Not(PathExists()))
 def test_startService_creates_socket(self):
     socket_path = self.patch_socket_path()
     service = LeaseSocketService(sentinel.service, reactor)
     service.startService()
     self.addCleanup(service.stopService)
     self.assertThat(socket_path, PathExists())