|
| 1 | +from collections import defaultdict |
| 2 | +from concurrent import futures |
| 3 | +from contextlib import contextmanager |
| 4 | + |
| 5 | +import grpc |
| 6 | + |
| 7 | +from feast.core.JobService_pb2 import GetJobResponse |
| 8 | +from feast.core.JobService_pb2 import Job as JobProto |
| 9 | +from feast.core.JobService_pb2 import JobStatus, JobType |
| 10 | +from feast.core.JobService_pb2_grpc import ( |
| 11 | + JobServiceServicer, |
| 12 | + JobServiceStub, |
| 13 | + add_JobServiceServicer_to_server, |
| 14 | +) |
| 15 | +from feast.remote_job import RemoteRetrievalJob |
| 16 | + |
| 17 | + |
| 18 | +@contextmanager |
| 19 | +def mock_server(servicer): |
| 20 | + """Instantiate a helloworld server and return a stub for use in tests""" |
| 21 | + server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) |
| 22 | + add_JobServiceServicer_to_server(servicer, server) |
| 23 | + port = server.add_insecure_port("[::]:0") |
| 24 | + server.start() |
| 25 | + |
| 26 | + try: |
| 27 | + with grpc.insecure_channel("localhost:%d" % port) as channel: |
| 28 | + yield JobServiceStub(channel) |
| 29 | + finally: |
| 30 | + server.stop(None) |
| 31 | + |
| 32 | + |
| 33 | +class TestRemoteJob: |
| 34 | + def test_remote_ingestion_job(self): |
| 35 | + """ Test wating for the remote ingestion job to complete """ |
| 36 | + |
| 37 | + class MockServicer(JobServiceServicer): |
| 38 | + """ |
| 39 | + The RemoteJob is expected to call GetJob until its done. |
| 40 | + This mock JobService returns RUNNING status on the first call, and DONE on the second. |
| 41 | + """ |
| 42 | + |
| 43 | + _job_statuses = [JobStatus.JOB_STATUS_DONE, JobStatus.JOB_STATUS_RUNNING] |
| 44 | + _call_count = defaultdict(int) |
| 45 | + |
| 46 | + def GetJob(self, request, context): |
| 47 | + |
| 48 | + self._call_count["GetJob"] += 1 |
| 49 | + return GetJobResponse( |
| 50 | + job=JobProto( |
| 51 | + id="test", |
| 52 | + type=JobType.RETRIEVAL_JOB, |
| 53 | + status=self._job_statuses.pop(), |
| 54 | + retrieval=JobProto.RetrievalJobMeta(output_location="foo"), |
| 55 | + ) |
| 56 | + ) |
| 57 | + |
| 58 | + mock_servicer = MockServicer() |
| 59 | + with mock_server(mock_servicer) as service: |
| 60 | + remote_job = RemoteRetrievalJob(service, lambda: {}, "test", "foo") |
| 61 | + |
| 62 | + assert remote_job.get_output_file_uri(timeout_sec=2) == "foo" |
| 63 | + assert mock_servicer._call_count["GetJob"] == 2 |
0 commit comments