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

Commit

Permalink
chore: Expect event sink to match predicate.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed May 28, 2024
1 parent 881c064 commit b4f44f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
23 changes: 22 additions & 1 deletion pkgs/sdk/server/test/AssertHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using LaunchDarkly.Logging;
using System;
using LaunchDarkly.Logging;
using LaunchDarkly.TestHelpers;
using Xunit;
using Xunit.Sdk;

Expand Down Expand Up @@ -42,5 +44,24 @@ private static void ThrowLogMatchException(LogCapture logCapture, bool shouldHav
text,
level,
logCapture.ToString()));

public static void ExpectPredicate<T>(EventSink<T> sink, Predicate<T> predicate, string message, TimeSpan timeout)
{
while (true)
{
var startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
var value = sink.ExpectValue();
if (predicate(value))
{
break;
}

if (DateTimeOffset.Now.ToUnixTimeMilliseconds() - startTime > timeout.TotalMilliseconds)
{
// XUnit 2.5+ adds Assert.Fail.
Assert.True(false, message);
}
}
}
}
}
13 changes: 8 additions & 5 deletions pkgs/sdk/server/test/Internal/DataSources/FileDataSourceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using YamlDotNet.Serialization;
using Xunit;
using Xunit.Abstractions;

using static LaunchDarkly.Sdk.Server.Subsystems.DataStoreTypes;
using static LaunchDarkly.Sdk.Server.TestUtils;
using static LaunchDarkly.TestHelpers.JsonAssertions;
Expand All @@ -24,7 +23,9 @@ public class FileDataSourceTest : BaseTest
private readonly FileDataSourceBuilder factory = FileData.DataSource();
private readonly Context user = Context.New("key");

public FileDataSourceTest(ITestOutputHelper testOutput) : base(testOutput) { }
public FileDataSourceTest(ITestOutputHelper testOutput) : base(testOutput)
{
}

private IDataSource MakeDataSource() =>
factory.Build(BasicContext.WithDataSourceUpdates(_updateSink));
Expand Down Expand Up @@ -148,9 +149,11 @@ public void ModifiedFileIsReloadedIfAutoUpdateIsOn()

file.SetContentFromPath(TestUtils.TestFilePath("segment-only.json"));

var newData = _updateSink.Inits.ExpectValue(TimeSpan.FromSeconds(5));

AssertJsonEqual(DataSetAsJson(ExpectedDataSetForSegmentOnlyFile(2)), DataSetAsJson(newData));
var expectedDataSet = DataSetAsJson(ExpectedDataSetForSegmentOnlyFile(2));
AssertHelpers.ExpectPredicate(_updateSink.Inits, actual =>
expectedDataSet.Equals(DataSetAsJson(actual)),
"Did not receive expected update from the file data source.",
TimeSpan.FromSeconds(5));
}
}
}
Expand Down

0 comments on commit b4f44f1

Please sign in to comment.