|
| 1 | +import concurrent.futures |
1 | 2 | import uuid
|
2 | 3 | from unittest.mock import patch
|
3 | 4 |
|
|
7 | 8 | from octue.resources.service_backends import GCPPubSubBackend
|
8 | 9 | from tests import TEST_PROJECT_NAME
|
9 | 10 | from tests.base import BaseTestCase
|
10 |
| -from tests.cloud.pub_sub.mocks import MockService, MockSubscription, MockTopic |
| 11 | +from tests.cloud.pub_sub.mocks import MockPullResponse, MockService, MockSubscription, MockTopic |
11 | 12 |
|
12 | 13 |
|
13 | 14 | class MockAnalysis:
|
@@ -61,13 +62,34 @@ def test_repr(self):
|
61 | 62 | asking_service = Service(backend=self.BACKEND)
|
62 | 63 | self.assertEqual(repr(asking_service), f"<Service({asking_service.name!r})>")
|
63 | 64 |
|
| 65 | + def test_service_id_cannot_be_non_none_empty_vaue(self): |
| 66 | + """Ensure that a ValueError is raised if a non-None empty value is provided as the service_id.""" |
| 67 | + with self.assertRaises(ValueError): |
| 68 | + Service(backend=self.BACKEND, service_id="") |
| 69 | + |
| 70 | + with self.assertRaises(ValueError): |
| 71 | + Service(backend=self.BACKEND, service_id=[]) |
| 72 | + |
| 73 | + with self.assertRaises(ValueError): |
| 74 | + Service(backend=self.BACKEND, service_id={}) |
| 75 | + |
64 | 76 | def test_ask_on_non_existent_service_results_in_error(self):
|
65 | 77 | """Test that trying to ask a question to a non-existent service (i.e. one without a topic in Google Pub/Sub)
|
66 | 78 | results in an error."""
|
67 | 79 | with patch("octue.cloud.pub_sub.service.Topic", new=MockTopic):
|
68 | 80 | with self.assertRaises(exceptions.ServiceNotFound):
|
69 | 81 | MockService(backend=self.BACKEND).ask(service_id="hello", input_values=[1, 2, 3, 4])
|
70 | 82 |
|
| 83 | + def test_timeout_error_raised_if_no_messages_received_when_waiting(self): |
| 84 | + """Test that a concurrent.futures.TimeoutError is raised if no messages are received while waiting.""" |
| 85 | + service = Service(backend=self.BACKEND) |
| 86 | + mock_topic = MockTopic(name="world", namespace="hello", service=service) |
| 87 | + mock_subscription = MockSubscription(name="world", topic=mock_topic, namespace="hello", service=service) |
| 88 | + |
| 89 | + with patch("octue.cloud.pub_sub.service.pubsub_v1.SubscriberClient.pull", return_value=MockPullResponse()): |
| 90 | + with self.assertRaises(concurrent.futures.TimeoutError): |
| 91 | + service.wait_for_answer(subscription=mock_subscription) |
| 92 | + |
71 | 93 | def test_ask(self):
|
72 | 94 | """ Test that a service can ask a question to another service that is serving and receive an answer. """
|
73 | 95 | responding_service = self.make_new_server(self.BACKEND, run_function_returnee=MockAnalysis(), use_mock=True)
|
|
0 commit comments