public void BackgroundDisabledState()
        {
            var testData     = TestData.DataSource();
            var backgrounder = new MockBackgroundModeManager();
            var config       = BasicConfig()
                               .BackgroundModeManager(backgrounder)
                               .DataSource(testData)
                               .EnableBackgroundUpdating(false)
                               .Build();

            using (var client = TestUtil.CreateClient(config, BasicUser))
            {
                Assert.True(client.DataSourceStatusProvider.WaitFor(DataSourceState.Valid, TimeSpan.FromSeconds(5)));

                var statuses = new EventSink <DataSourceStatus>();
                client.DataSourceStatusProvider.StatusChanged += statuses.Add;

                backgrounder.UpdateBackgroundMode(true);

                var newStatus1 = statuses.ExpectValue();
                Assert.Equal(DataSourceState.BackgroundDisabled, newStatus1.State);

                backgrounder.UpdateBackgroundMode(false);

                var newStatus2 = statuses.ExpectValue();
                Assert.Equal(DataSourceState.Initializing, newStatus2.State);

                var newStatus3 = statuses.ExpectValue();
                Assert.Equal(DataSourceState.Valid, newStatus3.State);
            }
        }
Ejemplo n.º 2
0
        public async Task BackgroundModePollingCanBeDisabledAsync()
        {
            var mockBackgroundModeManager = new MockBackgroundModeManager();
            var backgroundInterval        = TimeSpan.FromMilliseconds(50);
            var hackyUpdateDelay          = TimeSpan.FromMilliseconds(200);

            using (var server = HttpServer.Start(Handlers.Switchable(out var switchable)))
            {
                switchable.Target = SetupResponse(_flagData1, UpdateMode.Streaming);

                var config = BaseConfig(builder => builder
                                        .BackgroundModeManager(mockBackgroundModeManager)
                                        .EnableBackgroundUpdating(false)
                                        .DataSource(Components.StreamingDataSource().BackgroundPollInterval(backgroundInterval))
                                        .ServiceEndpoints(Components.ServiceEndpoints().Streaming(server.Uri).Polling(server.Uri))
                                        );

                using (var client = await TestUtil.CreateClientAsync(config, _user))
                {
                    VerifyFlagValues(client, _flagData1);

                    // The SDK should *not* hit this polling endpoint, but we're providing some data there so we can
                    // detect whether it does.
                    switchable.Target = SetupResponse(_flagData2, UpdateMode.Polling);
                    mockBackgroundModeManager.UpdateBackgroundMode(true);

                    await Task.Delay(hackyUpdateDelay);

                    VerifyFlagValues(client, _flagData1);  // we should *not* have done a poll

                    var receivedChangeSignal = new SemaphoreSlim(0, 1);
                    client.FlagTracker.FlagValueChanged += (sender, args) =>
                    {
                        receivedChangeSignal.Release();
                    };

                    // Now switch back to streaming
                    switchable.Target = SetupResponse(_flagData2, UpdateMode.Streaming);
                    mockBackgroundModeManager.UpdateBackgroundMode(false);

                    Assert.True(await receivedChangeSignal.WaitAsync(TimeSpan.FromSeconds(5)));
                    VerifyFlagValues(client, _flagData2);
                }
            }
        }
Ejemplo n.º 3
0
        public async Task BackgroundModeForcesPollingAsync()
        {
            var mockBackgroundModeManager = new MockBackgroundModeManager();
            var backgroundInterval        = TimeSpan.FromMilliseconds(50);

            using (var server = HttpServer.Start(Handlers.Switchable(out var switchable)))
            {
                switchable.Target = SetupResponse(_flagData1, UpdateMode.Streaming);

                var config = BaseConfig(builder => builder
                                        .BackgroundModeManager(mockBackgroundModeManager)
                                        .DataSource(Components.StreamingDataSource().BackgroundPollingIntervalWithoutMinimum(backgroundInterval))
                                        .ServiceEndpoints(Components.ServiceEndpoints().Streaming(server.Uri).Polling(server.Uri))
                                        );

                using (var client = await TestUtil.CreateClientAsync(config, _user))
                {
                    VerifyFlagValues(client, _flagData1);

                    // Set it up so that when the client switches to background mode and does a polling request, it will
                    // receive _flagData2, and we will be notified of that via a change event. SetupResponse will only
                    // configure the polling endpoint, so if the client makes a streaming request here it'll fail.
                    switchable.Target = SetupResponse(_flagData2, UpdateMode.Polling);
                    var receivedChangeSignal = new SemaphoreSlim(0, 1);
                    client.FlagTracker.FlagValueChanged += (sender, args) =>
                    {
                        receivedChangeSignal.Release();
                    };

                    mockBackgroundModeManager.UpdateBackgroundMode(true);

                    Assert.True(await receivedChangeSignal.WaitAsync(TimeSpan.FromSeconds(5)));
                    VerifyFlagValues(client, _flagData2);

                    // Now switch back to streaming
                    switchable.Target = SetupResponse(_flagData1, UpdateMode.Streaming);
                    mockBackgroundModeManager.UpdateBackgroundMode(false);

                    Assert.True(await receivedChangeSignal.WaitAsync(TimeSpan.FromSeconds(5)));
                    VerifyFlagValues(client, _flagData1);
                }
            }
        }
        public void DiagnosticPeriodicEventsAreNotSentWhenInBackground()
        {
            var mockBackgroundModeManager = new MockBackgroundModeManager();
            var config = BasicConfig()
                         .BackgroundModeManager(mockBackgroundModeManager)
                         .Events(Components.SendEvents()
                                 .EventSender(_testEventSender)
                                 .DiagnosticRecordingIntervalNoMinimum(TimeSpan.FromMilliseconds(50)))
                         .Build();

            using (var client = TestUtil.CreateClient(config, BasicUser))
            {
                mockBackgroundModeManager.UpdateBackgroundMode(true);

                // We will probably still get some periodic events before this mode change is picked
                // up asynchronously, but we should stop getting them soon.
                Assertions.AssertEventually(TimeSpan.FromMilliseconds(400), TimeSpan.FromMilliseconds(10),
                                            () => !_testEventSender.Calls.TryTake(out _, TimeSpan.FromMilliseconds(100)));

                mockBackgroundModeManager.UpdateBackgroundMode(false);

                _testEventSender.RequirePayload();
            }
        }