Exemple #1
0
        private async Task IdentifyCompletesOnlyWhenNewFlagsAreAvailable(Func <LdClient, User, Task> identifyTask)
        {
            var userA = User.WithKey("a");
            var userB = User.WithKey("b");

            var flagKey    = "flag";
            var userAFlags = new DataSetBuilder()
                             .Add(flagKey, 1, LdValue.Of("a-value"), 0).Build();
            var userBFlags = new DataSetBuilder()
                             .Add(flagKey, 2, LdValue.Of("b-value"), 1).Build();

            var startedIdentifyUserB   = new SemaphoreSlim(0, 1);
            var canFinishIdentifyUserB = new SemaphoreSlim(0, 1);
            var finishedIdentifyUserB  = new SemaphoreSlim(0, 1);

            var dataSourceFactory = new MockDataSourceFactoryFromLambda((ctx, updates, user, bg) =>
                                                                        new MockDataSourceFromLambda(user, async() =>
            {
                switch (user.Key)
                {
                case "a":
                    updates.Init(user, userAFlags);
                    break;

                case "b":
                    startedIdentifyUserB.Release();
                    await canFinishIdentifyUserB.WaitAsync();
                    updates.Init(user, userBFlags);
                    break;
                }
            }));

            var config = BasicConfig()
                         .DataSource(dataSourceFactory)
                         .Build();

            using (var client = await LdClient.InitAsync(config, userA))
            {
                Assert.True(client.Initialized);
                Assert.Equal("a-value", client.StringVariation(flagKey, null));

                var identifyUserBTask = Task.Run(async() =>
                {
                    await identifyTask(client, userB);
                    finishedIdentifyUserB.Release();
                });

                await startedIdentifyUserB.WaitAsync();

                Assert.False(client.Initialized);
                Assert.Null(client.StringVariation(flagKey, null));

                canFinishIdentifyUserB.Release();
                await finishedIdentifyUserB.WaitAsync();

                Assert.True(client.Initialized);
                Assert.Equal("b-value", client.StringVariation(flagKey, null));
            }
        }
Exemple #2
0
 // Calls LdClient.Init, but then sets LdClient.Instance to null so other tests can
 // instantiate their own independent clients. Application code cannot do this because
 // the LdClient.Instance setter has internal scope.
 public static async Task <LdClient> CreateClientAsync(Configuration config, User user)
 {
     return(await WithClientLockAsync(async() =>
     {
         ClearClient();
         LdClient client = await LdClient.InitAsync(config, user);
         client.DetachInstance();
         return client;
     }));
 }
Exemple #3
0
        public async void InitPassesUserToDataSource()
        {
            MockPollingProcessor stub = new MockPollingProcessor(DataSetBuilder.Empty);

            var config = BasicConfig()
                         .DataSource(stub.AsFactory())
                         .Build();

            using (var client = await LdClient.InitAsync(config, BasicUser))
            {
                var actualUser = client.User; // may have been transformed e.g. to add device/OS properties
                Assert.Equal(BasicUser.Key, actualUser.Key);
                Assert.Equal(actualUser, stub.ReceivedUser);
            }
        }
Exemple #4
0
        public async void InitWithKeylessAnonUserPassesGeneratedUserToDataSource()
        {
            MockPollingProcessor stub = new MockPollingProcessor(DataSetBuilder.Empty);

            var config = BasicConfig()
                         .DataSource(stub.AsFactory())
                         .Build();

            using (var client = await LdClient.InitAsync(config, KeylessAnonUser))
            {
                AssertHelpers.UsersEqualExcludingAutoProperties(
                    User.Builder(KeylessAnonUser).Key(stub.ReceivedUser.Key).Build(),
                    stub.ReceivedUser);
            }
        }
Exemple #5
0
        public async void IdentifyPassesUserToDataSource()
        {
            MockPollingProcessor stub = new MockPollingProcessor(DataSetBuilder.Empty);
            User newUser = User.WithKey("new-user");

            var config = BasicConfig()
                         .DataSource(stub.AsFactory())
                         .Build();

            using (var client = await LdClient.InitAsync(config, BasicUser))
            {
                AssertHelpers.UsersEqualExcludingAutoProperties(BasicUser, client.User);
                Assert.Equal(client.User, stub.ReceivedUser);

                await client.IdentifyAsync(newUser);

                AssertHelpers.UsersEqualExcludingAutoProperties(newUser, client.User);
                Assert.Equal(client.User, stub.ReceivedUser);
            }
        }