|
| 1 | +import threading |
| 2 | +import time |
| 3 | +import typing |
| 4 | +import unittest |
| 5 | +from unittest.mock import MagicMock, Mock, patch |
| 6 | + |
| 7 | +from google.protobuf.json_format import MessageToDict |
| 8 | +from google.protobuf.struct_pb2 import Struct |
| 9 | +from grpc import Channel |
| 10 | + |
| 11 | +from openfeature.contrib.provider.flagd.config import Config |
| 12 | +from openfeature.contrib.provider.flagd.resolvers.process.connector.grpc_watcher import ( |
| 13 | + GrpcWatcher, |
| 14 | +) |
| 15 | +from openfeature.contrib.provider.flagd.resolvers.process.flags import FlagStore |
| 16 | +from openfeature.event import ProviderEventDetails |
| 17 | +from openfeature.schemas.protobuf.flagd.sync.v1.sync_pb2 import ( |
| 18 | + GetMetadataResponse, |
| 19 | + SyncFlagsResponse, |
| 20 | +) |
| 21 | +from openfeature.schemas.protobuf.flagd.sync.v1.sync_pb2_grpc import FlagSyncServiceStub |
| 22 | + |
| 23 | + |
| 24 | +class TestGrpcWatcher(unittest.TestCase): |
| 25 | + def setUp(self): |
| 26 | + config = Mock(spec=Config) |
| 27 | + config.retry_backoff_ms = 1000 |
| 28 | + config.retry_backoff_max_ms = 5000 |
| 29 | + config.retry_grace_period = 5 |
| 30 | + config.stream_deadline_ms = 1000 |
| 31 | + config.deadline_ms = 5000 |
| 32 | + config.selector = None |
| 33 | + config.provider_id = None |
| 34 | + config.tls = False |
| 35 | + config.cert_path = None |
| 36 | + config.channel_credentials = None |
| 37 | + config.host = "localhost" |
| 38 | + config.port = 5000 |
| 39 | + config.sync_metadata_disabled = False |
| 40 | + |
| 41 | + flag_store = Mock(spec=FlagStore) |
| 42 | + flag_store.update.return_value = None |
| 43 | + emit_provider_error = Mock() |
| 44 | + emit_provider_stale = Mock() |
| 45 | + channel = Mock(spec=Channel) |
| 46 | + self.provider_done = False |
| 47 | + self.provider_details: typing.Optional[ProviderEventDetails] = None |
| 48 | + self.context: typing.Optional[dict] = None |
| 49 | + |
| 50 | + with patch( |
| 51 | + "openfeature.contrib.provider.flagd.resolvers.process.connector.grpc_watcher.GrpcWatcher._generate_channel", |
| 52 | + return_value=channel, |
| 53 | + ): |
| 54 | + self.grpc_watcher = GrpcWatcher( |
| 55 | + config=config, |
| 56 | + flag_store=flag_store, |
| 57 | + emit_provider_ready=self.provider_ready, |
| 58 | + emit_provider_error=emit_provider_error, |
| 59 | + emit_provider_stale=emit_provider_stale, |
| 60 | + ) |
| 61 | + self.mock_stub = MagicMock(spec=FlagSyncServiceStub) |
| 62 | + self.mock_metadata = GetMetadataResponse(metadata={"attribute": "value1"}) |
| 63 | + self.mock_stub.GetMetadata = Mock(return_value=self.mock_metadata) |
| 64 | + self.grpc_watcher.stub = self.mock_stub |
| 65 | + self.grpc_watcher.active = True |
| 66 | + |
| 67 | + def provider_ready(self, details: ProviderEventDetails, context: dict): |
| 68 | + self.provider_done = True |
| 69 | + self.provider_details = details |
| 70 | + self.context = context |
| 71 | + |
| 72 | + def run_listen_and_shutdown_after(self): |
| 73 | + listener = threading.Thread(target=self.grpc_watcher.listen) |
| 74 | + listener.start() |
| 75 | + for _i in range(0, 100): |
| 76 | + if self.provider_done: |
| 77 | + break |
| 78 | + time.sleep(0.001) |
| 79 | + |
| 80 | + self.assertTrue(self.provider_done) |
| 81 | + self.grpc_watcher.shutdown() |
| 82 | + listener.join(timeout=0.5) |
| 83 | + |
| 84 | + def test_listen_with_sync_metadata_and_sync_context(self): |
| 85 | + sync_context = Struct() |
| 86 | + sync_context.update({"attribute": "value"}) |
| 87 | + mock_stream_with_sync_context = iter( |
| 88 | + [ |
| 89 | + SyncFlagsResponse( |
| 90 | + flag_configuration='{"flag_key": "flag_value"}', |
| 91 | + sync_context=sync_context, |
| 92 | + ), |
| 93 | + ] |
| 94 | + ) |
| 95 | + self.mock_stub.SyncFlags = Mock(return_value=mock_stream_with_sync_context) |
| 96 | + |
| 97 | + self.run_listen_and_shutdown_after() |
| 98 | + |
| 99 | + self.assertEqual( |
| 100 | + self.provider_details.message, "gRPC sync connection established" |
| 101 | + ) |
| 102 | + self.assertEqual(self.context, MessageToDict(sync_context)) |
| 103 | + |
| 104 | + def test_listen_with_sync_metadata_only(self): |
| 105 | + mock_stream_no_sync_context = iter( |
| 106 | + [ |
| 107 | + SyncFlagsResponse(flag_configuration='{"flag_key": "flag_value"}'), |
| 108 | + ] |
| 109 | + ) |
| 110 | + self.mock_stub.SyncFlags = Mock(return_value=mock_stream_no_sync_context) |
| 111 | + |
| 112 | + self.run_listen_and_shutdown_after() |
| 113 | + |
| 114 | + self.assertEqual( |
| 115 | + self.provider_details.message, "gRPC sync connection established" |
| 116 | + ) |
| 117 | + self.assertEqual(self.context, MessageToDict(self.mock_metadata.metadata)) |
| 118 | + |
| 119 | + def test_listen_with_sync_metadata_disabled_in_config(self): |
| 120 | + self.grpc_watcher.config.sync_metadata_disabled = True |
| 121 | + mock_stream_no_sync_context = iter( |
| 122 | + [ |
| 123 | + SyncFlagsResponse(flag_configuration='{"flag_key": "flag_value"}'), |
| 124 | + ] |
| 125 | + ) |
| 126 | + self.mock_stub.SyncFlags = Mock(return_value=mock_stream_no_sync_context) |
| 127 | + |
| 128 | + self.run_listen_and_shutdown_after() |
| 129 | + |
| 130 | + self.mock_stub.GetMetadata.assert_not_called() |
| 131 | + |
| 132 | + self.assertEqual( |
| 133 | + self.provider_details.message, "gRPC sync connection established" |
| 134 | + ) |
| 135 | + self.assertEqual(self.context, {}) |
0 commit comments