Skip to content

Commit

Permalink
chore: add new test
Browse files Browse the repository at this point in the history
  • Loading branch information
iancooper committed Jan 8, 2025
1 parent 31d194d commit 165def3
Showing 1 changed file with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,50 @@ static async Task MultiTask()
await Task.Yield();
}

[Fact]
public async Task Run_Delegate_Via_Run_Thread_Runs()
{
var runner = new EventRunner();
runner.OnMessagePublished += MessagePublishedHandler;

s_runnerCalled = false;
BrighterAsyncContext.Run(async () =>
{
await runner.PublishAsync(runner.Report);
});

//let callback run on thread pool
await Task.Delay(1000);

s_runnerCalled.Should().BeTrue();

runner.OnMessagePublished -= MessagePublishedHandler;
}

private static bool s_runnerCalled = false;

static void MessagePublishedHandler(bool called, int value)
{
value.Should().Be(17);
s_runnerCalled = true;
}

internal class EventRunner
{
public event Action<bool, int> OnMessagePublished;

public async Task PublishAsync(Action<int> callBack)
{
await Task.Yield();
callBack(17);
}

public void Report(int value)
{
Task.Run(() => OnMessagePublished?.Invoke(true, value));
}
}

[Fact]
public void Current_WithoutAsyncContext_IsNull()
{
Expand Down

0 comments on commit 165def3

Please sign in to comment.