public static async Task NotifyOnThreadContextChangeWithOneEmptyContext()
{
bool expectThreadContextChange = false;
int expectedPreviousValue = 0;
int expectedCurrentValue = 1;
bool gotNotification = false;
bool expectNotification = false;
AsyncLocal<int> local = new AsyncLocal<int>(
args =>
{
gotNotification = true;
Assert.True(expectNotification);
expectNotification = false;
Assert.Equal(args.ThreadContextChanged, expectThreadContextChange);
Assert.Equal(args.PreviousValue, expectedPreviousValue);
Assert.Equal(args.CurrentValue, expectedCurrentValue);
});
ExecutionContext ec = ExecutionContext.Capture();
expectNotification = true;
expectedPreviousValue = 0;
expectedCurrentValue = 1;
local.Value = 1;
Assert.True(gotNotification);
gotNotification = false;
expectNotification = true;
expectedPreviousValue = 1;
expectedCurrentValue = 0;
expectThreadContextChange = true;
ExecutionContext.Run(
ec,
_ =>
{
Assert.True(gotNotification);
gotNotification = false;
Assert.Equal(local.Value, 0);
expectNotification = true;
expectedPreviousValue = 0;
expectedCurrentValue = 1;
expectThreadContextChange = true;
return;
},
null);
Assert.True(gotNotification);
gotNotification = false;
Assert.Equal(local.Value, 1);
expectNotification = true;
expectThreadContextChange = true;
expectedPreviousValue = local.Value;
expectedCurrentValue = 0;
return;
}