Skip to content

Commit 51d7651

Browse files
committed
feat(flagd-rpc): add caching with tests
Signed-off-by: Simon Schrottner <[email protected]>
1 parent 16179e3 commit 51d7651

File tree

3 files changed

+247
-0
lines changed

3 files changed

+247
-0
lines changed

providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/resolvers/grpc.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def __init__(
6363
self.deadline = config.deadline * 0.001
6464
self.connected = False
6565

66+
self._cache: typing.Optional[BaseCacheImpl] = (
67+
LRUCache(maxsize=self.config.max_cache_size)
68+
if self.config.cache_type == CacheType.LRU
69+
else None
70+
)
71+
6672
def _create_stub(
6773
self,
6874
) -> typing.Tuple[evaluation_pb2_grpc.ServiceStub, grpc.Channel]:
@@ -81,6 +87,14 @@ def _create_stub(
8187

8288
def initialize(self, evaluation_context: EvaluationContext) -> None:
8389
self.connect()
90+
self.retry_backoff_seconds = 0.1
91+
self.connected = False
92+
93+
self._cache = (
94+
LRUCache(maxsize=self.config.max_cache_size)
95+
if self.config.cache_type == CacheType.LRU
96+
else None
97+
)
8498

8599
def shutdown(self) -> None:
86100
self.active = False
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
Feature: Configuration Test
2+
3+
@rpc @in-process
4+
Scenario Outline: Default Config
5+
When we initialize a config
6+
Then the option "<option>" of type "<type>" should have the value "<default>"
7+
Examples: Basic
8+
| option | type | default |
9+
| resolverType | ResolverType | rpc |
10+
| host | String | localhost |
11+
| port | Integer | 8013 |
12+
| tls | Boolean | false |
13+
| deadline | Integer | 500 |
14+
@customCert
15+
Examples: Certificates
16+
| option | type | default |
17+
| certPath | String | null |
18+
@unixsocket
19+
Examples: Unixsocket
20+
| option | type | default |
21+
| socketPath | String | null |
22+
@events
23+
Examples: Events
24+
| option | type | default |
25+
| streamDeadlineMs | Integer | 600000 |
26+
| keepAlive | Long | 0 |
27+
| retryBackoffMs | Integer | 1000 |
28+
@sync
29+
Examples: Sync
30+
| option | type | default |
31+
| streamDeadlineMs | Integer | 600000 |
32+
| keepAlive | Long | 0 |
33+
| retryBackoffMs | Integer | 1000 |
34+
| selector | String | null |
35+
@caching
36+
Examples: caching
37+
| option | type | default |
38+
| cacheType | CacheType | lru |
39+
| maxCacheSize | Integer | 1000 |
40+
@offline
41+
Examples: offline
42+
| option | type | default |
43+
| offlineFlagSourcePath | String | null |
44+
45+
@rpc
46+
Scenario Outline: Default Config RPC
47+
When we initialize a config for "rpc"
48+
Then the option "<option>" of type "<type>" should have the value "<default>"
49+
Examples:
50+
| option | type | default |
51+
| port | Integer | 8013 |
52+
53+
@in-process
54+
Scenario Outline: Default Config In-Process
55+
When we initialize a config for "in-process"
56+
Then the option "<option>" of type "<type>" should have the value "<default>"
57+
Examples:
58+
| option | type | default |
59+
| port | Integer | 8015 |
60+
61+
Scenario Outline: Dedicated Config
62+
When we have an option "<option>" of type "<type>" with value "<value>"
63+
And we initialize a config
64+
Then the option "<option>" of type "<type>" should have the value "<value>"
65+
Examples:
66+
| option | type | value |
67+
| resolverType | ResolverType | in-process |
68+
| host | String | local |
69+
| tls | Boolean | True |
70+
| port | Integer | 1234 |
71+
| deadline | Integer | 123 |
72+
@customCert
73+
Examples:
74+
| option | type | value |
75+
| certPath | String | path |
76+
@unixsocket
77+
Examples:
78+
| option | type | value |
79+
| socketPath | String | path |
80+
@events
81+
Examples:
82+
| option | type | value |
83+
| streamDeadlineMs | Integer | 500000 |
84+
| keepAlive | Long | 5 |
85+
| retryBackoffMs | Integer | 5000 |
86+
@sync
87+
Examples:
88+
| option | type | value |
89+
| streamDeadlineMs | Integer | 500000 |
90+
| keepAlive | Long | 5 |
91+
| retryBackoffMs | Integer | 5000 |
92+
| selector | String | selector |
93+
@caching
94+
Examples:
95+
| option | type | value |
96+
| cacheType | CacheType | disabled |
97+
| maxCacheSize | Integer | 1236 |
98+
@offline
99+
Examples:
100+
| option | type | value |
101+
| offlineFlagSourcePath | String | path |
102+
103+
Scenario Outline: Dedicated Config via Env_var
104+
When we have an environment variable "<env>" with value "<value>"
105+
And we initialize a config
106+
Then the option "<option>" of type "<type>" should have the value "<value>"
107+
Examples:
108+
| option | env | type | value |
109+
| resolverType | FLAGD_RESOLVER | ResolverType | in-process |
110+
| resolverType | FLAGD_RESOLVER | ResolverType | IN-PROCESS |
111+
| resolverType | FLAGD_RESOLVER | ResolverType | rpc |
112+
| resolverType | FLAGD_RESOLVER | ResolverType | RPC |
113+
| host | FLAGD_HOST | String | local |
114+
| tls | FLAGD_TLS | Boolean | True |
115+
| port | FLAGD_PORT | Integer | 1234 |
116+
| deadline | FLAGD_DEADLINE_MS | Integer | 123 |
117+
@customCert
118+
Examples:
119+
| option | env | type | value |
120+
| certPath | FLAGD_SERVER_CERT_PATH | String | path |
121+
@unixsocket
122+
Examples:
123+
| option | env | type | value |
124+
| socketPath | FLAGD_SOCKET_PATH | String | path |
125+
@events
126+
Examples:
127+
| option | env | type | value |
128+
| streamDeadlineMs | FLAGD_STREAM_DEADLINE_MS | Integer | 500000 |
129+
| keepAlive | FLAGD_KEEP_ALIVE_TIME_MS | Long | 5 |
130+
| retryBackoffMs | FLAGD_RETRY_BACKOFF_MS | Integer | 5000 |
131+
@sync
132+
Examples:
133+
| option | env | type | value |
134+
| streamDeadlineMs | FLAGD_STREAM_DEADLINE_MS | Integer | 500000 |
135+
| keepAlive | FLAGD_KEEP_ALIVE_TIME_MS | Long | 5 |
136+
| retryBackoffMs | FLAGD_RETRY_BACKOFF_MS | Integer | 5000 |
137+
| selector | FLAGD_SOURCE_SELECTOR | String | selector |
138+
@caching
139+
Examples:
140+
| option | env | type | value |
141+
| cacheType | FLAGD_CACHE | CacheType | disabled |
142+
| maxCacheSize | FLAGD_MAX_CACHE_SIZE | Integer | 1236 |
143+
@offline
144+
Examples:
145+
| option | env | type | value |
146+
| offlineFlagSourcePath | FLAGD_OFFLINE_FLAG_SOURCE_PATH | String | path |
147+
148+
Scenario Outline: Dedicated Config via Env_var and set
149+
When we have an environment variable "<env>" with value "<env-value>"
150+
And we have an option "<option>" of type "<type>" with value "<value>"
151+
And we initialize a config
152+
Then the option "<option>" of type "<type>" should have the value "<value>"
153+
Examples:
154+
| option | env | type | value | env-value |
155+
| resolverType | FLAGD_RESOLVER | ResolverType | in-process | rpc |
156+
| host | FLAGD_HOST | String | local | l |
157+
| tls | FLAGD_TLS | Boolean | True | False |
158+
| port | FLAGD_PORT | Integer | 1234 | 3456 |
159+
| deadline | FLAGD_DEADLINE_MS | Integer | 123 | 345 |
160+
@customCert
161+
Examples:
162+
| option | env | type | value | env-value |
163+
| certPath | FLAGD_SERVER_CERT_PATH | String | path | rpc |
164+
@unixsocket
165+
Examples:
166+
| option | env | type | value | env-value |
167+
| socketPath | FLAGD_SOCKET_PATH | String | path | rpc |
168+
@events
169+
Examples:
170+
| option | env | type | value | env-value |
171+
| streamDeadlineMs | FLAGD_STREAM_DEADLINE_MS | Integer | 500000 | 400 |
172+
| keepAlive | FLAGD_KEEP_ALIVE_TIME_MS | Long | 5 | 4 |
173+
| retryBackoffMs | FLAGD_RETRY_BACKOFF_MS | Integer | 5000 | 400 |
174+
@sync
175+
Examples:
176+
| option | env | type | value | env-value |
177+
| streamDeadlineMs | FLAGD_STREAM_DEADLINE_MS | Integer | 500000 | 400 |
178+
| keepAlive | FLAGD_KEEP_ALIVE_TIME_MS | Long | 5 | 4 |
179+
| retryBackoffMs | FLAGD_RETRY_BACKOFF_MS | Integer | 5000 | 400 |
180+
| selector | FLAGD_SOURCE_SELECTOR | String | selector | sele |
181+
@caching
182+
Examples:
183+
| option | env | type | value | env-value |
184+
| cacheType | FLAGD_CACHE | CacheType | disabled | lru |
185+
| maxCacheSize | FLAGD_MAX_CACHE_SIZE | Integer | 1236 | 2345 |
186+
@offline
187+
Examples:
188+
| option | env | type | value | env-value |
189+
| offlineFlagSourcePath | FLAGD_OFFLINE_FLAG_SOURCE_PATH | String | path | lll |
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Feature: Flag evaluation with Caching
2+
3+
# This test suite contains scenarios to test the flag evaluation API.
4+
5+
Background:
6+
Given a provider is registered
7+
8+
Scenario: Resolves boolean details with caching
9+
When a boolean flag with key "boolean-flag" is evaluated with details and default value "false"
10+
Then the resolved boolean details value should be "true", the variant should be "on", and the reason should be "STATIC"
11+
Then the resolved boolean details value should be "true", the variant should be "on", and the reason should be "CACHED"
12+
13+
Scenario: Resolves string details with caching
14+
When a string flag with key "string-flag" is evaluated with details and default value "bye"
15+
Then the resolved string details value should be "hi", the variant should be "greeting", and the reason should be "STATIC"
16+
Then the resolved string details value should be "hi", the variant should be "greeting", and the reason should be "CACHED"
17+
18+
Scenario: Resolves integer details with caching
19+
When an integer flag with key "integer-flag" is evaluated with details and default value 1
20+
Then the resolved integer details value should be 10, the variant should be "ten", and the reason should be "STATIC"
21+
Then the resolved integer details value should be 10, the variant should be "ten", and the reason should be "CACHED"
22+
23+
Scenario: Resolves float details with caching
24+
When a float flag with key "float-flag" is evaluated with details and default value 0.1
25+
Then the resolved float details value should be 0.5, the variant should be "half", and the reason should be "STATIC"
26+
Then the resolved float details value should be 0.5, the variant should be "half", and the reason should be "CACHED"
27+
28+
Scenario: Resolves object details with caching
29+
When an object flag with key "object-flag" is evaluated with details and a null default value
30+
Then the resolved object details value should be contain fields "showImages", "title", and "imagesPerPage", with values "true", "Check out these pics!" and 100, respectively
31+
And the variant should be "template", and the reason should be "STATIC"
32+
Then the resolved object details value should be contain fields "showImages", "title", and "imagesPerPage", with values "true", "Check out these pics!" and 100, respectively
33+
And the variant should be "template", and the reason should be "CACHED"
34+
35+
Scenario: Flag change event with caching
36+
When a string flag with key "changing-flag" is evaluated with details
37+
When a PROVIDER_CONFIGURATION_CHANGED handler is added
38+
And a flag with key "changing-flag" is modified
39+
Then the returned reason should be "STATIC"
40+
Then the returned reason should be "CACHED"
41+
Then the PROVIDER_CONFIGURATION_CHANGED handler must run
42+
And the event details must indicate "changing-flag" was altered
43+
Then the returned reason should be "STATIC"
44+
Then the returned reason should be "CACHED"

0 commit comments

Comments
 (0)