Skip to content

Commit

Permalink
Add unit test of Periodic flushing (#135)
Browse files Browse the repository at this point in the history
Co-authored-by: Jens Henneberg <[email protected]>
  • Loading branch information
daniel-statsig and jenshenneberg authored Apr 9, 2024
1 parent 37d5e1d commit 9384bc3
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions dotnet-statsig-tests/Common/EventLoggerTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Statsig;
Expand All @@ -19,6 +20,8 @@ public class EventLoggerTest : IAsyncLifetime, IResponseProvider
private WireMockServer _server;
private int _flushedEventCount;
private EventLogger _logger;
private CountdownEvent _onLogCountdown;
private static int ThresholdSeconds = 2;

public Task InitializeAsync()
{
Expand All @@ -30,8 +33,8 @@ public Task InitializeAsync()

var sdkDetails = SDKDetails.GetClientSDKDetails();
var dispatcher = new RequestDispatcher("a-key", new StatsigOptions(apiUrlBase: _server.Urls[0]), sdkDetails, "my-session");
_logger = new EventLogger(dispatcher, sdkDetails, 1, 999);

_logger = new EventLogger(dispatcher, sdkDetails, maxQueueLength: 3, maxThresholdSecs: ThresholdSeconds);
_onLogCountdown = new CountdownEvent(1);
return Task.CompletedTask;
}

Expand All @@ -41,13 +44,24 @@ public Task DisposeAsync()
return Task.CompletedTask;
}

[Fact]
public async void TestPeriodicScheduling()
{
_logger.Enqueue(new EventLog { EventName = "one" });
_logger.Enqueue(new EventLog { EventName = "two" });
_onLogCountdown.Wait(TimeSpan.FromSeconds(ThresholdSeconds * 2));

Assert.Equal(2, _flushedEventCount);

await _logger.Shutdown();
}

[Fact]
public async Task TestShutdownWithInFlightLogs()
{
_logger.Enqueue(new EventLog { EventName = "one" });
_logger.Enqueue(new EventLog { EventName = "two" });
await _logger.Shutdown();

Assert.Equal(2, _flushedEventCount);
}

Expand All @@ -67,9 +81,13 @@ IWireMockServerSettings settings
var events = ((JArray)body["events"])?.ToObject<List<JObject>>();
_flushedEventCount += events?.Count ?? 0;

return await Response.Create()
var result = await Response.Create()
.WithStatusCode(200)
.WithDelay(TimeSpan.FromMilliseconds(100))
.ProvideResponseAsync(requestMessage, settings);

_onLogCountdown.Signal();

return result;
}
}
}

0 comments on commit 9384bc3

Please sign in to comment.