Skip to content

Commit 12d123f

Browse files
committed
TestTaskRunner Now to move forward only.
1 parent 23aaba6 commit 12d123f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

async-enumerable-dotnet-test/TestTaskRunnerTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,5 +322,23 @@ public async void RightTaskQueued_Absolute()
322322
Assert.False(ttr.HasTasks);
323323
}
324324
}
325+
326+
[Fact]
327+
public async void Time_Moves_Forward()
328+
{
329+
var ttr = new TestTaskRunner(1000);
330+
331+
var t1 = ttr.CreateLambdaTask<long>(tcs => tcs.SetResult(ttr.Now), 0, true);
332+
var t2 = ttr.CreateLambdaTask<long>(tcs => tcs.SetResult(ttr.Now), 500, true);
333+
var t3 = ttr.CreateLambdaTask<long>(tcs => tcs.SetResult(ttr.Now), 1000, true);
334+
var t4 = ttr.CreateLambdaTask<long>(tcs => tcs.SetResult(ttr.Now), 1500, true);
335+
336+
ttr.AdvanceTimeBy(500);
337+
338+
Assert.Equal(1000, await t1);
339+
Assert.Equal(1000, await t2);
340+
Assert.Equal(1000, await t3);
341+
Assert.Equal(1500, await t4);
342+
}
325343
}
326344
}

async-enumerable-dotnet/TestTaskRunner.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ public void AdvanceTimeBy(long milliseconds)
129129

130130
if (has)
131131
{
132-
Volatile.Write(ref _now, t.DueTime);
132+
// don't allow time to go back for past-scheduled work
133+
Volatile.Write(ref _now, Math.Max(Now, t.DueTime));
133134
t.Signal();
134135
}
135136
else

0 commit comments

Comments
 (0)