|
| 1 | +import socket |
| 2 | +import threading |
| 3 | +from contextlib import closing |
| 4 | +from typing import List |
| 5 | + |
| 6 | +import grpc |
| 7 | +import pytest |
| 8 | + |
| 9 | +from feast import FeatureService, ValueType |
| 10 | +from feast.embedded_go.online_features_service import EmbeddedOnlineFeatureServer |
| 11 | +from feast.feast_object import FeastObject |
| 12 | +from feast.protos.feast.serving.ServingService_pb2 import ( |
| 13 | + FieldStatus, |
| 14 | + GetOnlineFeaturesRequest, |
| 15 | + GetOnlineFeaturesResponse, |
| 16 | +) |
| 17 | +from feast.protos.feast.serving.ServingService_pb2_grpc import ServingServiceStub |
| 18 | +from feast.protos.feast.types.Value_pb2 import RepeatedValue |
| 19 | +from feast.type_map import python_values_to_proto_values |
| 20 | +from feast.wait import wait_retry_backoff |
| 21 | +from tests.integration.feature_repos.integration_test_repo_config import ( |
| 22 | + IntegrationTestRepoConfig, |
| 23 | +) |
| 24 | +from tests.integration.feature_repos.repo_configuration import ( |
| 25 | + AVAILABLE_OFFLINE_STORES, |
| 26 | + AVAILABLE_ONLINE_STORES, |
| 27 | + REDIS_CONFIG, |
| 28 | + construct_test_environment, |
| 29 | + construct_universal_feature_views, |
| 30 | + construct_universal_test_data, |
| 31 | +) |
| 32 | +from tests.integration.feature_repos.universal.entities import ( |
| 33 | + customer, |
| 34 | + driver, |
| 35 | + location, |
| 36 | +) |
| 37 | + |
| 38 | +LOCAL_REPO_CONFIGS = [ |
| 39 | + IntegrationTestRepoConfig(online_store=REDIS_CONFIG, go_feature_retrieval=True), |
| 40 | +] |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture( |
| 44 | + params=[ |
| 45 | + c |
| 46 | + for c in LOCAL_REPO_CONFIGS |
| 47 | + if c.offline_store_creator in AVAILABLE_OFFLINE_STORES |
| 48 | + and c.online_store in AVAILABLE_ONLINE_STORES |
| 49 | + ] |
| 50 | +) |
| 51 | +def local_environment(request): |
| 52 | + e = construct_test_environment(request.param) |
| 53 | + |
| 54 | + def cleanup(): |
| 55 | + e.feature_store.teardown() |
| 56 | + |
| 57 | + request.addfinalizer(cleanup) |
| 58 | + return e |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture |
| 62 | +def initialized_registry(local_environment): |
| 63 | + fs = local_environment.feature_store |
| 64 | + |
| 65 | + entities, datasets, data_sources = construct_universal_test_data(local_environment) |
| 66 | + feature_views = construct_universal_feature_views(data_sources) |
| 67 | + |
| 68 | + feature_service = FeatureService( |
| 69 | + name="driver_features", features=[feature_views.driver] |
| 70 | + ) |
| 71 | + feast_objects: List[FeastObject] = [feature_service] |
| 72 | + feast_objects.extend(feature_views.values()) |
| 73 | + feast_objects.extend([driver(), customer(), location()]) |
| 74 | + |
| 75 | + fs.apply(feast_objects) |
| 76 | + fs.materialize(local_environment.start_date, local_environment.end_date) |
| 77 | + |
| 78 | + |
| 79 | +@pytest.fixture |
| 80 | +def grpc_server_port(local_environment, initialized_registry): |
| 81 | + fs = local_environment.feature_store |
| 82 | + |
| 83 | + embedded = EmbeddedOnlineFeatureServer( |
| 84 | + repo_path=str(fs.repo_path.absolute()), repo_config=fs.config, feature_store=fs, |
| 85 | + ) |
| 86 | + port = free_port() |
| 87 | + |
| 88 | + t = threading.Thread(target=embedded.start_grpc_server, args=("127.0.0.1", port)) |
| 89 | + t.start() |
| 90 | + |
| 91 | + wait_retry_backoff( |
| 92 | + lambda: (None, check_port_open("127.0.0.1", port)), timeout_secs=15 |
| 93 | + ) |
| 94 | + |
| 95 | + yield port |
| 96 | + embedded.stop_grpc_server() |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture |
| 100 | +def grpc_client(grpc_server_port): |
| 101 | + ch = grpc.insecure_channel(f"localhost:{grpc_server_port}") |
| 102 | + yield ServingServiceStub(ch) |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.integration |
| 106 | +@pytest.mark.universal |
| 107 | +def test_go_grpc_server(grpc_client): |
| 108 | + resp: GetOnlineFeaturesResponse = grpc_client.GetOnlineFeatures( |
| 109 | + GetOnlineFeaturesRequest( |
| 110 | + feature_service="driver_features", |
| 111 | + entities={ |
| 112 | + "driver_id": RepeatedValue( |
| 113 | + val=python_values_to_proto_values( |
| 114 | + [5001, 5002], feature_type=ValueType.INT64 |
| 115 | + ) |
| 116 | + ) |
| 117 | + }, |
| 118 | + full_feature_names=True, |
| 119 | + ) |
| 120 | + ) |
| 121 | + assert list(resp.metadata.feature_names.val) == [ |
| 122 | + "driver_id", |
| 123 | + "driver_stats__conv_rate", |
| 124 | + "driver_stats__acc_rate", |
| 125 | + "driver_stats__avg_daily_trips", |
| 126 | + ] |
| 127 | + for vector in resp.results: |
| 128 | + assert all([s == FieldStatus.PRESENT for s in vector.statuses]) |
| 129 | + |
| 130 | + |
| 131 | +def free_port(): |
| 132 | + sock = socket.socket() |
| 133 | + sock.bind(("", 0)) |
| 134 | + return sock.getsockname()[1] |
| 135 | + |
| 136 | + |
| 137 | +def check_port_open(host, port) -> bool: |
| 138 | + with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: |
| 139 | + return sock.connect_ex((host, port)) == 0 |
0 commit comments