Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

fix: fixes issue where first flag listener callback was not triggered… #97

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/start-contract-test-service.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
cd contract-tests && dotnet bin/Debug/${TESTFRAMEWORK:-net6.0}/ContractTestService.dll
cd contract-tests && dotnet bin/Debug/${TESTFRAMEWORK:-net7.0}/ContractTestService.dll
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public void Init(Context context, FullDataSet data)
_dataStore.Init(context, data, true);

ImmutableDictionary<string, FeatureFlag> oldValues, newValues;
var contextKey = context.FullyQualifiedKey;
lock (_lastValuesLock)
{
_lastValues.TryGetValue(context.Key, out oldValues);
_lastValues.TryGetValue(contextKey, out oldValues);
var builder = ImmutableDictionary.CreateBuilder<string, FeatureFlag>();
foreach (var newEntry in data.Items)
{
Expand All @@ -65,7 +66,7 @@ public void Init(Context context, FullDataSet data)
}
}
newValues = builder.ToImmutable();
_lastValues = _lastValues.SetItem(context.Key, newValues);
_lastValues = _lastValues.SetItem(contextKey, newValues);
}

UpdateStatus(DataSourceState.Valid, null);
Expand Down
2 changes: 1 addition & 1 deletion src/LaunchDarkly.ClientSdk/LdClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace LaunchDarkly.Sdk.Client
/// <para>
/// Normally, the SDK uses the exact context that you have specified in the <see cref="Context"/>. However,
/// you can also tell the SDK to generate a randomized identifier and use this as the context's
/// <see cref="Context.Key"/>; see <see cref="ConfigurationBuilder.GenerateAnonymousKeys(bool)"/>.
/// <see cref="Context.FullyQualifiedKey"/>; see <see cref="ConfigurationBuilder.GenerateAnonymousKeys(bool)"/>.
tanderson-ld marked this conversation as resolved.
Show resolved Hide resolved
/// </para>
/// <para>
/// If you use more than one <see cref="ContextKind"/> in your evaluation contexts, and you request a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public class DataSourceUpdateSinkImplTest : BaseTest
private readonly FlagDataManager _store;
private readonly FlagTrackerImpl _flagTracker;
private readonly DataSourceUpdateSinkImpl _updateSink;
private readonly Context _basicUser = Context.New("user-key");
private readonly Context _otherUser = Context.New("other-key");

private readonly Context _basicUser = Context.NewMulti(Context.New(ContextKind.Of("user"), "user-key1"), Context.New(ContextKind.Of("custom-kind"), "custom-key1"));
private readonly Context _otherUser = Context.NewMulti(Context.New(ContextKind.Of("user"), "user-key2"), Context.New(ContextKind.Of("custom-kind"), "custom-key2"));
Copy link
Contributor Author

@tanderson-ld tanderson-ld Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: Using multi-contexts in the test here causes the tests to fail without the fix, so this is sufficient test coverage now.


public DataSourceUpdateSinkImplTest(ITestOutputHelper testOutput) : base(testOutput)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/LaunchDarkly.ClientSdk.Tests/LDClientEndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void IdentifySwitchesUserAndGetsFlagsSync(UpdateMode mode)
var success = client.Identify(_otherUser, TimeSpan.FromSeconds(5));
Assert.True(success);
Assert.True(client.Initialized);
Assert.Equal(_otherUser.Key, client.Context.Key); // don't compare entire user, because SDK may have added device/os attributes
Assert.Equal(_otherUser.FullyQualifiedKey, client.Context.FullyQualifiedKey); // don't compare entire user, because SDK may have added device/os attributes

var req2 = VerifyRequest(server.Recorder, mode);
Assert.NotEqual(user1RequestPath, req2.Path);
Expand Down Expand Up @@ -211,7 +211,7 @@ public async Task IdentifySwitchesUserAndGetsFlagsAsync(UpdateMode mode)
var success = await client.IdentifyAsync(_otherUser);
Assert.True(success);
Assert.True(client.Initialized);
Assert.Equal(_otherUser.Key, client.Context.Key); // don't compare entire user, because SDK may have added device/os attributes
Assert.Equal(_otherUser.FullyQualifiedKey, client.Context.FullyQualifiedKey); // don't compare entire user, because SDK may have added device/os attributes

var req2 = VerifyRequest(server.Recorder, mode);
Assert.NotEqual(user1RequestPath, req2.Path);
Expand Down
8 changes: 4 additions & 4 deletions tests/LaunchDarkly.ClientSdk.Tests/LdClientEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void TrackSendsCustomEvent()
e => {
CustomEvent ce = Assert.IsType<CustomEvent>(e);
Assert.Equal("eventkey", ce.EventKey);
Assert.Equal(user.Key, ce.Context.Key);
Assert.Equal(user.FullyQualifiedKey, ce.Context.FullyQualifiedKey);
Assert.Equal(LdValue.Null, ce.Data);
Assert.Null(ce.MetricValue);
Assert.NotEqual(0, ce.Timestamp.Value);
Expand All @@ -68,7 +68,7 @@ public void TrackWithDataSendsCustomEvent()
e => {
CustomEvent ce = Assert.IsType<CustomEvent>(e);
Assert.Equal("eventkey", ce.EventKey);
Assert.Equal(user.Key, ce.Context.Key);
Assert.Equal(user.FullyQualifiedKey, ce.Context.FullyQualifiedKey);
Assert.Equal(data, ce.Data);
Assert.Null(ce.MetricValue);
Assert.NotEqual(0, ce.Timestamp.Value);
Expand All @@ -89,7 +89,7 @@ public void TrackWithMetricValueSendsCustomEvent()
e => {
CustomEvent ce = Assert.IsType<CustomEvent>(e);
Assert.Equal("eventkey", ce.EventKey);
Assert.Equal(user.Key, ce.Context.Key);
Assert.Equal(user.FullyQualifiedKey, ce.Context.FullyQualifiedKey);
Assert.Equal(data, ce.Data);
Assert.Equal(metricValue, ce.MetricValue);
Assert.NotEqual(0, ce.Timestamp.Value);
Expand Down Expand Up @@ -336,7 +336,7 @@ public void VariationSendsFeatureEventWithReasonForUnknownFlagWhenClientIsNotIni
private void CheckIdentifyEvent(object e, Context c)
{
IdentifyEvent ie = Assert.IsType<IdentifyEvent>(e);
Assert.Equal(c.Key, ie.Context.Key);
Assert.Equal(c.FullyQualifiedKey, ie.Context.FullyQualifiedKey);
Assert.NotEqual(0, ie.Timestamp.Value);
}
}
Expand Down
30 changes: 15 additions & 15 deletions tests/LaunchDarkly.ClientSdk.Tests/LdClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task InitWithAnonUserAddsRandomizedKey()

using (var client = await TestUtil.CreateClientAsync(config, AnonUser))
{
key1 = client.Context.Key;
key1 = client.Context.FullyQualifiedKey;
Assert.NotNull(key1);
Assert.NotEqual("", key1);
AssertHelpers.ContextsEqual(
Expand All @@ -77,7 +77,7 @@ public async Task InitWithAnonUserAddsRandomizedKey()
// Starting again should generate a new key, since we've turned off persistence
using (var client = await TestUtil.CreateClientAsync(config, AnonUser))
{
var key2 = client.Context.Key;
var key2 = client.Context.FullyQualifiedKey;
Assert.NotNull(key2);
Assert.NotEqual("", key2);
Assert.NotEqual(key1, key2);
Expand Down Expand Up @@ -112,7 +112,7 @@ public async Task InitWithAnonUserCanReusePreviousRandomizedKey()

using (var client = await TestUtil.CreateClientAsync(config, AnonUser))
{
key1 = client.Context.Key;
key1 = client.Context.FullyQualifiedKey;
Assert.NotNull(key1);
Assert.NotEqual("", key1);
AssertHelpers.ContextsEqual(
Expand All @@ -123,7 +123,7 @@ public async Task InitWithAnonUserCanReusePreviousRandomizedKey()
// Starting again should reuse the persisted key
using (var client = await TestUtil.CreateClientAsync(config, AnonUser))
{
Assert.Equal(key1, client.Context.Key);
Assert.Equal(key1, client.Context.FullyQualifiedKey);
AssertHelpers.ContextsEqual(
Context.BuilderFromContext(AnonUser).Key(key1).Build(),
client.Context);
Expand All @@ -147,7 +147,7 @@ public async void InitWithAnonUserPassesGeneratedUserToDataSource()
Assert.NotEqual(AnonUser, receivedContext);
Assert.Equal(client.Context, receivedContext);
AssertHelpers.ContextsEqual(
Context.BuilderFromContext(AnonUser).Key(receivedContext.Key).Build(),
Context.BuilderFromContext(AnonUser).Key(receivedContext.FullyQualifiedKey).Build(),
receivedContext);
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ public async void InitWithAutoEnvAttributesDisabledNoAddedContexts()
Assert.NotEqual(AnonUser, receivedContext);
Assert.Equal(client.Context, receivedContext);
AssertHelpers.ContextsEqual(
Context.BuilderFromContext(AnonUser).Key(receivedContext.Key).Build(),
Context.BuilderFromContext(AnonUser).Key(receivedContext.FullyQualifiedKey).Build(),
receivedContext);
}
}
Expand All @@ -200,7 +200,7 @@ public void IdentifyUpdatesTheUser()
var updatedUser = Context.New("some new key");
var success = client.Identify(updatedUser, TimeSpan.FromSeconds(1));
Assert.True(success);
Assert.Equal(client.Context.Key, updatedUser.Key); // don't compare entire user, because SDK may have added device/os attributes
Assert.Equal(client.Context.FullyQualifiedKey, updatedUser.FullyQualifiedKey); // don't compare entire user, because SDK may have added device/os attributes
}
}

Expand Down Expand Up @@ -230,7 +230,7 @@ private async Task IdentifyCompletesOnlyWhenNewFlagsAreAvailable(Func<LdClient,
var dataSourceFactory = MockComponents.ComponentConfigurerFromLambda<IDataSource>(ctx =>
new MockDataSourceFromLambda(ctx.CurrentContext, async () =>
{
switch (ctx.CurrentContext.Key)
switch (ctx.CurrentContext.FullyQualifiedKey)
{
case "a":
ctx.DataSourceUpdateSink.Init(ctx.CurrentContext, userAFlags);
Expand Down Expand Up @@ -308,7 +308,7 @@ public async Task IdentifyWithAnonUserAddsRandomizedKey()
{
await client.IdentifyAsync(AnonUser);

key1 = client.Context.Key;
key1 = client.Context.FullyQualifiedKey;
Assert.NotNull(key1);
Assert.NotEqual("", key1);
AssertHelpers.ContextsEqual(
Expand All @@ -317,7 +317,7 @@ public async Task IdentifyWithAnonUserAddsRandomizedKey()

var anonUser2 = TestUtil.BuildAutoContext().Name("other").Build();
await client.IdentifyAsync(anonUser2);
var key2 = client.Context.Key;
var key2 = client.Context.FullyQualifiedKey;
Assert.Equal(key1, key2); // Even though persistence is disabled, the key is stable during the lifetime of the SDK client.
AssertHelpers.ContextsEqual(
Context.BuilderFromContext(anonUser2).Key(key2).Build(),
Expand All @@ -328,7 +328,7 @@ public async Task IdentifyWithAnonUserAddsRandomizedKey()
{
await client.IdentifyAsync(AnonUser);

var key3 = client.Context.Key;
var key3 = client.Context.FullyQualifiedKey;
Assert.NotNull(key3);
Assert.NotEqual("", key3);
Assert.NotEqual(key1, key3); // The previously generated key was discarded with the previous client.
Expand Down Expand Up @@ -365,7 +365,7 @@ public async Task IdentifyWithAnonUserCanReusePersistedRandomizedKey()
{
await client.IdentifyAsync(AnonUser);

key1 = client.Context.Key;
key1 = client.Context.FullyQualifiedKey;
Assert.NotNull(key1);
Assert.NotEqual("", key1);
AssertHelpers.ContextsEqual(
Expand All @@ -377,7 +377,7 @@ public async Task IdentifyWithAnonUserCanReusePersistedRandomizedKey()
{
await client.IdentifyAsync(AnonUser);

var key2 = client.Context.Key;
var key2 = client.Context.FullyQualifiedKey;
Assert.Equal(key1, key2);
AssertHelpers.ContextsEqual(
Context.BuilderFromContext(AnonUser).Key(key2).Build(),
Expand All @@ -404,7 +404,7 @@ public async void IdentifyWithAnonUserPassesGeneratedUserToDataSource()
Assert.NotEqual(AnonUser, receivedContext);
Assert.Equal(client.Context, receivedContext);
AssertHelpers.ContextsEqual(
Context.BuilderFromContext(AnonUser).Key(client.Context.Key).Build(),
Context.BuilderFromContext(AnonUser).Key(client.Context.FullyQualifiedKey).Build(),
receivedContext);
}
}
Expand Down Expand Up @@ -448,7 +448,7 @@ public async void IdentifyWithAutoEnvAttributesDisabledNoAddedContexts()
Assert.NotEqual(AnonUser, receivedContext);
Assert.Equal(client.Context, receivedContext);
AssertHelpers.ContextsEqual(
Context.BuilderFromContext(AnonUser).Key(receivedContext.Key).Build(),
Context.BuilderFromContext(AnonUser).Key(receivedContext.FullyQualifiedKey).Build(),
receivedContext);
}
}
Expand Down
Loading