Skip to content

Commit

Permalink
Remove flaky cancellation tests and warn about FakeScheduler
Browse files Browse the repository at this point in the history
It's just not right yet.
  • Loading branch information
jskeet committed Nov 25, 2016
1 parent b3dad61 commit aa237eb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 56 deletions.
17 changes: 3 additions & 14 deletions test/Google.Api.Gax.Tests/FakeSchedulerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,15 @@ namespace Google.Api.Gax.Tests
{
public class FakeSchedulerTest
{
// Note: no tests for cancellation at the moment as they're fiendishly difficult to write in a robust way.
// We need to revisit FakeScheduler yet again...

[Fact]
public async Task SimpleDelay()
{
var scheduler = new FakeScheduler();
await scheduler.RunAsync(() => scheduler.Delay(TimeSpan.FromTicks(1000)));
Assert.Equal(1000, scheduler.Clock.GetCurrentDateTimeUtc().Ticks);
}

[Fact]
public async Task Cancellation()
{
var scheduler = new FakeScheduler();
await scheduler.RunAsync(async () =>
{
var cts = new CancellationTokenSource();
scheduler.ScheduleCancellation(TimeSpan.FromTicks(500), cts);
var task = scheduler.Delay(TimeSpan.FromTicks(1000), cts.Token);
await Assert.ThrowsAsync<TaskCanceledException>(() => scheduler.Delay(TimeSpan.FromTicks(1000), cts.Token));
});
Assert.Equal(500, scheduler.Clock.GetCurrentDateTimeUtc().Ticks);
}
}
}
31 changes: 3 additions & 28 deletions test/Google.Api.Gax.Tests/PollingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace Google.Api.Gax.Tests
{
public class PollingTest
{
// Note: no tests for cancellation at the moment as they're fiendishly difficult to write in a robust way.
// We need to revisit FakeScheduler yet again...

[Fact]
public void PollToCompletion_Success()
{
Expand All @@ -41,20 +44,6 @@ public void PollToCompletion_Timeout()
});
}

[Fact]
public void PollToCompletion_Cancellation()
{
var cts = new CancellationTokenSource();
var pollSource = new PollSource(TimeSpan.FromSeconds(4), 5);
var pollSettings = new PollSettings(Expiration.FromTimeout(TimeSpan.FromSeconds(5)), TimeSpan.FromSeconds(2));
pollSource.Scheduler.ScheduleCancellation(TimeSpan.FromSeconds(3), cts);
pollSource.Scheduler.Run(() =>
{
Assert.Throws<TaskCanceledException>(() => pollSource.PollRepeatedly(pollSettings, cts.Token));
Assert.Equal(TimeSpan.FromSeconds(3), pollSource.RunningTime);
});
}

[Fact]
public async Task PollToCompletionAsync_Success()
{
Expand All @@ -81,20 +70,6 @@ await pollSource.Scheduler.RunAsync(async () =>
});
}

[Fact]
public async Task PollToCompletionAsync_Cancellation()
{
var cts = new CancellationTokenSource();
var pollSource = new PollSource(TimeSpan.FromSeconds(4), 5);
var pollSettings = new PollSettings(Expiration.FromTimeout(TimeSpan.FromSeconds(5)), TimeSpan.FromSeconds(2));
pollSource.Scheduler.ScheduleCancellation(TimeSpan.FromSeconds(3), cts);
await pollSource.Scheduler.RunAsync(async () =>
{
await Assert.ThrowsAsync<TaskCanceledException>(() => pollSource.PollRepeatedlyAsync(pollSettings, cts.Token));
Assert.Equal(TimeSpan.FromSeconds(3), pollSource.RunningTime);
});
}

private class PollSource
{
public FakeScheduler Scheduler { get; } = new FakeScheduler();
Expand Down
20 changes: 6 additions & 14 deletions testing/Google.Api.Gax.Testing/FakeScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
namespace Google.Api.Gax.Testing
{
/// <summary>
/// Fake implementation of <see cref="IScheduler" />, designed to work with
/// Experimental - please read reamarks. Fake implementation of <see cref="IScheduler" />, designed to work with
/// <see cref="FakeClock"/>.
/// </summary>
/// <remarks>
/// This class is currently hard to use robustly, and cancellation in particular is hard.
/// The API and behavior may change in future versions without a major version bump - in other words,
/// this is not considered part of the stable API of the package.
/// </remarks>
public sealed class FakeScheduler : IScheduler
{
/// <summary>
Expand Down Expand Up @@ -57,19 +62,6 @@ public FakeScheduler() : this(new FakeClock(0L))
public Task Delay(TimeSpan delay, CancellationToken cancellationToken = default(CancellationToken)) =>
AddTimer(Clock.GetCurrentDateTimeUtc() + delay, cancellationToken);

/// <summary>
/// Schedules tokens from the given source to be cancelled in the future. Use of this method
/// ensures that any tasks depending on tokens from this source will be cancelled before the clock advances further.
/// </summary>
/// <param name="delay">The delay in cancelling (i.e. when we should cancel)</param>
/// <param name="cts">The cancellation token source to cancel.</param>
public void ScheduleCancellation(TimeSpan delay, CancellationTokenSource cts)
{
// We need to execute synchronously so that any tasks will really be cancelled before
// the clock advances again.
Delay(delay).ContinueWith(_ => cts.Cancel(), TaskContinuationOptions.ExecuteSynchronously);
}

/// <summary>
/// Specialization of <see cref="Run{T}(Func{T})"/> for tasks, to prevent a common usage error.
/// </summary>
Expand Down

0 comments on commit aa237eb

Please sign in to comment.