Skip to content

Commit e27d351

Browse files
authored
fix: Setup config correct when passing a Uri (fixes #71) (#83)
Signed-off-by: bjoneill <[email protected]>
1 parent b749549 commit e27d351

File tree

4 files changed

+159
-42
lines changed

4 files changed

+159
-42
lines changed

Diff for: src/OpenFeature.Contrib.Providers.Flagd/FlagdConfig.cs

+23-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ internal FlagdConfig()
7676
}
7777
}
7878

79+
internal FlagdConfig(Uri url)
80+
{
81+
if (url == null)
82+
{
83+
throw new ArgumentNullException(nameof(url));
84+
}
85+
86+
_host = url.Host;
87+
_port = url.Port.ToString();
88+
_useTLS = url.Scheme.ToLower().Equals("https");
89+
_cert = Environment.GetEnvironmentVariable(EnvCertPart) ?? "";
90+
_socketPath = url.Scheme.ToLower().Equals("unix") ? url.GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Scheme, UriFormat.UriEscaped) : "";
91+
var cacheStr = Environment.GetEnvironmentVariable(EnvVarCache) ?? "";
92+
93+
if (cacheStr.ToUpper().Equals("LRU"))
94+
{
95+
_cache = true;
96+
_maxCacheSize = int.Parse(Environment.GetEnvironmentVariable(EnvVarMaxCacheSize) ?? $"{CacheSizeDefault}");
97+
_maxEventStreamRetries = int.Parse(Environment.GetEnvironmentVariable(EnvVarMaxEventStreamRetries) ?? "3");
98+
}
99+
}
100+
79101
internal Uri GetUri()
80102
{
81103
Uri uri;
@@ -97,4 +119,4 @@ internal Uri GetUri()
97119
return uri;
98120
}
99121
}
100-
}
122+
}

Diff for: src/OpenFeature.Contrib.Providers.Flagd/FlagdProvider.cs

+8-26
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,22 @@ public sealed class FlagdProvider : FeatureProvider
5050
/// FLAGD_MAX_CACHE_SIZE - The maximum size of the cache (default="10")
5151
/// FLAGD_MAX_EVENT_STREAM_RETRIES - The maximum amount of retries for establishing the EventStream
5252
/// </summary>
53-
public FlagdProvider()
53+
public FlagdProvider() : this(new FlagdConfig())
5454
{
55-
_config = new FlagdConfig();
56-
57-
_client = BuildClientForPlatform(_config.GetUri());
58-
59-
_mtx = new System.Threading.Mutex();
60-
61-
if (_config.CacheEnabled)
62-
{
63-
_cache = new LRUCache<string, object>(_config.MaxCacheSize);
64-
Task.Run(async () =>
65-
{
66-
await HandleEvents();
67-
});
68-
}
6955
}
7056

7157
/// <summary>
72-
/// Constructor of the provider.
58+
/// Constructor of the provider. This constructor uses the value of the following
59+
/// environment variables to initialise its client:
60+
/// FLAGD_FLAGD_SERVER_CERT_PATH - The path to the client certificate (default="")
61+
/// FLAGD_CACHE - Enable or disable the cache (default="false")
62+
/// FLAGD_MAX_CACHE_SIZE - The maximum size of the cache (default="10")
63+
/// FLAGD_MAX_EVENT_STREAM_RETRIES - The maximum amount of retries for establishing the EventStream
7364
/// <param name="url">The URL of the flagD server</param>
7465
/// <exception cref="ArgumentNullException">if no url is provided.</exception>
7566
/// </summary>
76-
public FlagdProvider(Uri url)
67+
public FlagdProvider(Uri url) : this(new FlagdConfig(url))
7768
{
78-
if (url == null)
79-
{
80-
throw new ArgumentNullException(nameof(url));
81-
}
82-
83-
_mtx = new System.Threading.Mutex();
84-
85-
_client = BuildClientForPlatform(url);
8669
}
8770

8871
/// <summary>
@@ -610,4 +593,3 @@ private Service.ServiceClient BuildClientForPlatform(Uri url)
610593
}
611594
}
612595
}
613-

Diff for: test/OpenFeature.Contrib.Providers.Flagd.Test/FlagdConfigTest.cs

+90-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Xunit;
23

34
namespace OpenFeature.Contrib.Providers.Flagd.Test
@@ -11,36 +12,36 @@ public void TestFlagdConfigDefault()
1112
var config = new FlagdConfig();
1213

1314
Assert.False(config.CacheEnabled);
14-
Assert.Equal(new System.Uri("http://localhost:8013"), config.GetUri());
15+
Assert.Equal(new Uri("http://localhost:8013"), config.GetUri());
1516
}
1617

1718
[Fact]
1819
public void TestFlagdConfigUseTLS()
1920
{
2021
CleanEnvVars();
21-
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarTLS, "true");
22+
Environment.SetEnvironmentVariable(FlagdConfig.EnvVarTLS, "true");
2223

2324
var config = new FlagdConfig();
2425

25-
Assert.Equal(new System.Uri("https://localhost:8013"), config.GetUri());
26+
Assert.Equal(new Uri("https://localhost:8013"), config.GetUri());
2627
}
2728

2829
[Fact]
2930
public void TestFlagdConfigUnixSocket()
3031
{
3132
CleanEnvVars();
32-
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarSocketPath, "tmp.sock");
33+
Environment.SetEnvironmentVariable(FlagdConfig.EnvVarSocketPath, "tmp.sock");
3334

3435
var config = new FlagdConfig();
3536

36-
Assert.Equal(new System.Uri("unix://tmp.sock/"), config.GetUri());
37+
Assert.Equal(new Uri("unix://tmp.sock/"), config.GetUri());
3738
}
3839

3940
[Fact]
4041
public void TestFlagdConfigEnabledCacheDefaultCacheSize()
4142
{
4243
CleanEnvVars();
43-
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarCache, "LRU");
44+
Environment.SetEnvironmentVariable(FlagdConfig.EnvVarCache, "LRU");
4445

4546
var config = new FlagdConfig();
4647

@@ -52,8 +53,8 @@ public void TestFlagdConfigEnabledCacheDefaultCacheSize()
5253
public void TestFlagdConfigEnabledCacheApplyCacheSize()
5354
{
5455
CleanEnvVars();
55-
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarCache, "LRU");
56-
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarMaxCacheSize, "20");
56+
Environment.SetEnvironmentVariable(FlagdConfig.EnvVarCache, "LRU");
57+
Environment.SetEnvironmentVariable(FlagdConfig.EnvVarMaxCacheSize, "20");
5758

5859
var config = new FlagdConfig();
5960

@@ -65,7 +66,7 @@ public void TestFlagdConfigEnabledCacheApplyCacheSize()
6566
public void TestFlagdConfigSetCertificatePath()
6667
{
6768
CleanEnvVars();
68-
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvCertPart, "/cert/path");
69+
Environment.SetEnvironmentVariable(FlagdConfig.EnvCertPart, "/cert/path");
6970

7071
var config = new FlagdConfig();
7172

@@ -80,13 +81,87 @@ public void TestFlagdConfigSetCertificatePath()
8081
Assert.False(config.UseCertificate);
8182
}
8283

84+
[Fact]
85+
public void TestFlagdConfigFromUriHttp()
86+
{
87+
CleanEnvVars();
88+
var config = new FlagdConfig(new Uri("http://domain:8123"));
89+
90+
Assert.False(config.CacheEnabled);
91+
Assert.Equal(new Uri("http://domain:8123"), config.GetUri());
92+
}
93+
94+
[Fact]
95+
public void TestFlagdConfigFromUriHttps()
96+
{
97+
CleanEnvVars();
98+
var config = new FlagdConfig(new Uri("https://domain:8123"));
99+
100+
Assert.False(config.CacheEnabled);
101+
Assert.Equal(new Uri("https://domain:8123"), config.GetUri());
102+
}
103+
104+
[Fact]
105+
public void TestFlagdConfigFromUriUnix()
106+
{
107+
CleanEnvVars();
108+
var config = new FlagdConfig(new Uri("unix:///var/run/tmp.sock"));
109+
110+
Assert.False(config.CacheEnabled);
111+
Assert.Equal(new Uri("unix:///var/run/tmp.sock"), config.GetUri());
112+
}
113+
114+
[Fact]
115+
public void TestFlagdConfigFromUriSetCertificatePath()
116+
{
117+
CleanEnvVars();
118+
Environment.SetEnvironmentVariable(FlagdConfig.EnvCertPart, "/cert/path");
119+
120+
var config = new FlagdConfig(new Uri("http://localhost:8013"));
121+
122+
Assert.Equal("/cert/path", config.CertificatePath);
123+
Assert.True(config.UseCertificate);
124+
125+
CleanEnvVars();
126+
127+
config = new FlagdConfig(new Uri("http://localhost:8013"));
128+
129+
Assert.Equal("", config.CertificatePath);
130+
Assert.False(config.UseCertificate);
131+
}
132+
133+
[Fact]
134+
public void TestFlagdConfigFromUriEnabledCacheDefaultCacheSize()
135+
{
136+
CleanEnvVars();
137+
Environment.SetEnvironmentVariable(FlagdConfig.EnvVarCache, "LRU");
138+
139+
var config = new FlagdConfig(new Uri("http://localhost:8013"));
140+
141+
Assert.True(config.CacheEnabled);
142+
Assert.Equal(FlagdConfig.CacheSizeDefault, config.MaxCacheSize);
143+
}
144+
145+
[Fact]
146+
public void TestFlagdConfigFromUriEnabledCacheApplyCacheSize()
147+
{
148+
CleanEnvVars();
149+
Environment.SetEnvironmentVariable(FlagdConfig.EnvVarCache, "LRU");
150+
Environment.SetEnvironmentVariable(FlagdConfig.EnvVarMaxCacheSize, "20");
151+
152+
var config = new FlagdConfig(new Uri("http://localhost:8013"));
153+
154+
Assert.True(config.CacheEnabled);
155+
Assert.Equal(20, config.MaxCacheSize);
156+
}
157+
83158
private void CleanEnvVars()
84159
{
85-
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarTLS, "");
86-
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarSocketPath, "");
87-
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarCache, "");
88-
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarMaxCacheSize, "");
89-
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvCertPart, "");
160+
Environment.SetEnvironmentVariable(FlagdConfig.EnvVarTLS, "");
161+
Environment.SetEnvironmentVariable(FlagdConfig.EnvVarSocketPath, "");
162+
Environment.SetEnvironmentVariable(FlagdConfig.EnvVarCache, "");
163+
Environment.SetEnvironmentVariable(FlagdConfig.EnvVarMaxCacheSize, "");
164+
Environment.SetEnvironmentVariable(FlagdConfig.EnvCertPart, "");
90165
}
91166
}
92-
}
167+
}

Diff for: test/OpenFeature.Contrib.Providers.Flagd.Test/FlagdProviderTest.cs

+38
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,44 @@ public void TestGetProviderWithConfig()
117117
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarMaxCacheSize, "");
118118
}
119119

120+
[Fact]
121+
public void TestGetProviderWithUri()
122+
{
123+
// Set env variables (should be used by the constructor)
124+
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvCertPart, "");
125+
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarCache, "LRU");
126+
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarMaxCacheSize, "20");
127+
128+
// Set env variables (should be ignored by the constructor)
129+
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarHost, "localhost111");
130+
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarPort, "5001");
131+
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarTLS, "false");
132+
133+
// Create provider, which ignores the env vars and uses the config
134+
var flagdProvider = new FlagdProvider(new Uri("https://localhost:8013"));
135+
136+
// Client should no be nil
137+
var client = flagdProvider.GetClient();
138+
Assert.NotNull(client);
139+
140+
// Retrieve config for assertions
141+
var config = flagdProvider.GetConfig();
142+
143+
// Assert
144+
Assert.Equal("", config.CertificatePath);
145+
Assert.Equal(new Uri("https://localhost:8013"), config.GetUri());
146+
Assert.True(config.CacheEnabled);
147+
Assert.Equal(20, config.MaxCacheSize);
148+
149+
// Cleanup
150+
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvCertPart, "");
151+
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarHost, "");
152+
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarPort, "");
153+
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarTLS, "");
154+
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarCache, "");
155+
System.Environment.SetEnvironmentVariable(FlagdConfig.EnvVarMaxCacheSize, "");
156+
}
157+
120158
[Fact]
121159
public void TestResolveBooleanValue()
122160
{

0 commit comments

Comments
 (0)