def test_run_method_with_args(self):
        service = Mock(spec=["callRemote"])
        self.services.lookup.return_value = service
        call = ServiceCall(
            monitor="localhost",
            service="service",
            method="method",
            args=['arg'],
            kwargs={'arg': 1},
        )
        expected_result = service.callRemote.return_value

        dfr = self.ref.run(call)

        self.assertIsInstance(dfr, defer.Deferred)
        self.assertTrue(dfr.called)
        self.assertEqual(dfr.result, expected_result)
        self.services.lookup.assert_called_once_with(
            call.service,
            call.monitor,
        )
        service.callRemote.assert_called_once_with(
            call.method,
            call.args[0],
            arg=call.kwargs['arg'],
        )
    def test_run_lookup_failure(self):
        expected_error = ValueError("boom")
        self.services.lookup.side_effect = expected_error
        call = ServiceCall(
            monitor="localhost",
            service="the_service",
            method="method",
            args=[],
            kwargs={},
        )

        result = []
        dfr = self.ref.run(call)
        dfr.addErrback(lambda x: result.append(x))

        self.services.lookup.assert_called_once_with(
            call.service,
            call.monitor,
        )
        self.assertEqual(len(result), 1)
        failure = result[0]
        self.assertIsInstance(failure, Failure)
        actual_error = failure.value
        self.assertIsInstance(actual_error, ValueError)
        self.logger.error.assert_called_once_with(
            "Failed to retrieve remote service "
            "service=%s worker=%s error=(%s) %s",
            call.service,
            1,
            "ValueError",
            expected_error,
        )
Ejemplo n.º 3
0
    def test_missing_method(self):
        servicecall = ServiceCall(
            monitor="localhost",
            service="EventService", method="sendEvent",
            args=["event"], kwargs={},
        )

        self.getattr.return_value = None

        handler = Mock()
        dfr = self.executor.submit(servicecall)
        dfr.addErrback(handler)

        self.getattr.assert_called_once_with(
            self.zem, servicecall.method, None,
        )
        self.assertEqual(len(handler.mock_calls), 1)

        f = handler.call_args[0][0]
        self.assertIsInstance(f, Failure)
        self.assertIsInstance(f.value, AttributeError)
        self.assertRegexpMatches(str(f.value), ".*sendEvent.*")

        self.zem.sendEvent.assert_not_called()
        self.zem.sendEvents.assert_not_called()
Ejemplo n.º 4
0
    def test_exception_from_service(self):
        servicecall = ServiceCall(
            monitor="localhost",
            service="EventService", method="sendEvent", args=[], kwargs={},
        )

        error = ValueError("boom")
        sendEvent = self.zem.sendEvent
        sendEvents = self.zem.sendEvents
        self.getattr.return_value = sendEvent
        sendEvent.side_effect = error

        handler = Mock()
        dfr = self.executor.submit(servicecall)
        dfr.addErrback(handler)

        self.assertEqual(len(handler.mock_calls), 1)

        self.getattr.assert_called_once_with(
            self.zem, servicecall.method, None,
        )
        f = handler.call_args[0][0]
        self.assertIsInstance(f, Failure)
        self.assertIsInstance(f.value, ValueError)
        self.assertEqual(str(f.value), "boom")

        sendEvent.assert_called_once_with()
        sendEvents.assert_not_called()
Ejemplo n.º 5
0
 def setUp(self):
     self.queue = "queue"
     self.monitor = "localhost"
     self.service = "service"
     self.method = "method"
     self.call = ServiceCall(
         monitor=self.monitor,
         service=self.service,
         method=self.method,
         args=[],
         kwargs={},
     )
     self.task = ServiceCallTask(self.queue, self.call)
Ejemplo n.º 6
0
    def test_sendEvents(self):
        servicecall = ServiceCall(
            monitor="localhost",
            service="EventService", method="sendEvents",
            args=[["event"]], kwargs={},
        )

        sendEvent = self.zem.sendEvent
        sendEvents = self.zem.sendEvents
        self.getattr.return_value = sendEvents

        dfr = self.executor.submit(servicecall)

        self.assertIsInstance(dfr, defer.Deferred)
        self.getattr.assert_called_once_with(
            self.zem, servicecall.method, None,
        )
        self.assertEqual(sendEvents.return_value, dfr.result)
        sendEvents.assert_called_once_with(["event"])
        sendEvent.assert_not_called()
Ejemplo n.º 7
0
    def test_wrong_method(self):
        servicecall = ServiceCall(
            monitor="localhost",
            service="EventService", method="badmethod",
            args=["event"], kwargs={},
        )

        handler = Mock()
        dfr = self.executor.submit(servicecall)
        dfr.addErrback(handler)

        self.assertEqual(len(handler.mock_calls), 1)

        f = handler.call_args[0][0]
        self.assertIsInstance(f, Failure)
        self.assertIsInstance(f.value, TypeError)
        self.assertRegexpMatches(str(f.value), ".*badmethod.*")

        self.zem.sendEvent.assert_not_called()
        self.zem.sendEvents.assert_not_called()
        self.getattr.assert_not_called()
    def test_run_callremote_failure(self):
        service = Mock(spec=["callRemote"])
        self.services.lookup.return_value = service
        call = ServiceCall(
            monitor="localhost",
            service="service",
            method="method",
            args=[],
            kwargs={},
        )
        expected_error = ValueError("boom")
        service.callRemote.side_effect = expected_error

        result = []
        dfr = self.ref.run(call)
        dfr.addErrback(lambda x: result.append(x))

        self.services.lookup.assert_called_once_with(
            call.service,
            call.monitor,
        )
        service.callRemote.assert_called_once_with(call.method)
        self.assertEqual(len(result), 1)
        failure = result[0]
        self.assertIsInstance(failure, Failure)
        actual_error = failure.value
        self.assertIsInstance(actual_error, ValueError)
        self.logger.error.assert_called_once_with(
            "Failed to execute remote method "
            "service=%s method=%s id=%s worker=%s error=(%s) %s",
            call.service,
            call.method,
            call.id.hex,
            1,
            "ValueError",
            expected_error,
        )