public async Task BackgroundModePollingCanBeDisabledAsync() { var mockBackgroundModeManager = new MockBackgroundModeManager(); var backgroundInterval = TimeSpan.FromMilliseconds(50); var hackyUpdateDelay = TimeSpan.FromMilliseconds(200); ClearCachedFlags(_user); await WithServerAsync(async server => { var config = BaseConfig(server, UpdateMode.Streaming, builder => builder .BackgroundModeManager(mockBackgroundModeManager) .EnableBackgroundUpdating(false) .BackgroundPollingInterval(backgroundInterval) .PersistFlagValues(false)); SetupResponse(server, _flagData1, UpdateMode.Streaming); 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. SetupResponse(server, _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.FlagChanged += (sender, args) => { receivedChangeSignal.Release(); }; // Now switch back to streaming SetupResponse(server, _flagData2, UpdateMode.Streaming); mockBackgroundModeManager.UpdateBackgroundMode(false); await receivedChangeSignal.WaitAsync(); VerifyFlagValues(client, _flagData2); } }); }
public async Task BackgroundModeForcesPollingAsync() { var mockBackgroundModeManager = new MockBackgroundModeManager(); var backgroundInterval = TimeSpan.FromMilliseconds(50); ClearCachedFlags(_user); await WithServerAsync(async server => { var config = BaseConfig(server, UpdateMode.Streaming, builder => builder .BackgroundModeManager(mockBackgroundModeManager) .BackgroundPollingIntervalWithoutMinimum(backgroundInterval) .PersistFlagValues(false)); SetupResponse(server, _flagData1, UpdateMode.Streaming); 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. SetupResponse(server, _flagData2, UpdateMode.Polling); var receivedChangeSignal = new SemaphoreSlim(0, 1); client.FlagChanged += (sender, args) => { receivedChangeSignal.Release(); }; mockBackgroundModeManager.UpdateBackgroundMode(true); await receivedChangeSignal.WaitAsync(); VerifyFlagValues(client, _flagData2); // Now switch back to streaming SetupResponse(server, _flagData1, UpdateMode.Streaming); mockBackgroundModeManager.UpdateBackgroundMode(false); await receivedChangeSignal.WaitAsync(); VerifyFlagValues(client, _flagData1); } }); }