This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed race condition in periodic timers (#438)
- Loading branch information
Showing
3 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// ------------------------------------------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.PSharp.Timers; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.PSharp.Core.Tests | ||
{ | ||
public class TimerStressTest : BaseTest | ||
{ | ||
public TimerStressTest(ITestOutputHelper output) | ||
: base(output) | ||
{ | ||
} | ||
|
||
private class SetupEvent : Event | ||
{ | ||
public TaskCompletionSource<bool> Tcs; | ||
|
||
public SetupEvent(TaskCompletionSource<bool> tcs) | ||
{ | ||
this.Tcs = tcs; | ||
} | ||
} | ||
|
||
private class T1 : Machine | ||
{ | ||
private TaskCompletionSource<bool> Tcs; | ||
|
||
[Start] | ||
[OnEntry(nameof(InitOnEntry))] | ||
[OnEventDoAction(typeof(TimerElapsedEvent), nameof(HandleTimeout))] | ||
private class Init : MachineState | ||
{ | ||
} | ||
|
||
private void InitOnEntry() | ||
{ | ||
this.Tcs = (this.ReceivedEvent as SetupEvent).Tcs; | ||
|
||
// Start a regular timer. | ||
this.StartTimer(TimeSpan.FromTicks(1)); | ||
} | ||
|
||
private void HandleTimeout() | ||
{ | ||
this.Tcs.SetResult(true); | ||
this.Raise(new Halt()); | ||
} | ||
} | ||
|
||
[Fact(Timeout= 6000)] | ||
public async Task TestTimerLifetime() | ||
{ | ||
await this.RunAsync(async r => | ||
{ | ||
int numTimers = 1000; | ||
var awaiters = new Task[numTimers]; | ||
for (int i = 0; i < numTimers; i++) | ||
{ | ||
var tcs = new TaskCompletionSource<bool>(); | ||
r.CreateMachine(typeof(T1), new SetupEvent(tcs)); | ||
awaiters[i] = tcs.Task; | ||
} | ||
|
||
Task task = Task.WhenAll(awaiters); | ||
await WaitAsync(task); | ||
}); | ||
} | ||
|
||
private class T2 : Machine | ||
{ | ||
private TaskCompletionSource<bool> Tcs; | ||
private int Counter; | ||
|
||
[Start] | ||
[OnEntry(nameof(InitOnEntry))] | ||
[OnEventDoAction(typeof(TimerElapsedEvent), nameof(HandleTimeout))] | ||
private class Init : MachineState | ||
{ | ||
} | ||
|
||
private void InitOnEntry() | ||
{ | ||
this.Tcs = (this.ReceivedEvent as SetupEvent).Tcs; | ||
this.Counter = 0; | ||
|
||
// Start a periodic timer. | ||
this.StartPeriodicTimer(TimeSpan.FromTicks(1), TimeSpan.FromTicks(1)); | ||
} | ||
|
||
private void HandleTimeout() | ||
{ | ||
this.Counter++; | ||
if (this.Counter == 10) | ||
{ | ||
this.Tcs.SetResult(true); | ||
this.Raise(new Halt()); | ||
} | ||
} | ||
} | ||
|
||
[Fact(Timeout = 6000)] | ||
public async Task TestPeriodicTimerLifetime() | ||
{ | ||
await this.RunAsync(async r => | ||
{ | ||
int numTimers = 1000; | ||
var awaiters = new Task[numTimers]; | ||
for (int i = 0; i < numTimers; i++) | ||
{ | ||
var tcs = new TaskCompletionSource<bool>(); | ||
r.CreateMachine(typeof(T2), new SetupEvent(tcs)); | ||
awaiters[i] = tcs.Task; | ||
} | ||
|
||
Task task = Task.WhenAll(awaiters); | ||
await WaitAsync(task); | ||
}); | ||
} | ||
} | ||
} |
File renamed without changes.