Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Commit eeb6f2f

Browse files
authored
Support for internal async test harness and async tests (#432)
1 parent a8456b6 commit eeb6f2f

File tree

112 files changed

+2454
-2976
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+2454
-2976
lines changed

Source/Core/Configuration.cs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -517,17 +517,15 @@ protected Configuration()
517517
}
518518

519519
/// <summary>
520-
/// Creates a new configuration.
520+
/// Creates a new configuration with default values.
521521
/// </summary>
522-
/// <returns>Configuration</returns>
523522
public static Configuration Create()
524523
{
525524
return new Configuration();
526525
}
527526

528527
/// <summary>
529-
/// Updates the configuration with verbose output enabled
530-
/// and returns it.
528+
/// Updates the configuration with verbose output enabled or disabled.
531529
/// </summary>
532530
/// <param name="isVerbose">If true, then messages are logged.</param>
533531
public Configuration WithVerbosityEnabled(bool isVerbose = true)
@@ -537,8 +535,7 @@ public Configuration WithVerbosityEnabled(bool isVerbose = true)
537535
}
538536

539537
/// <summary>
540-
/// Updates the configuration with verbose output enabled
541-
/// and returns it.
538+
/// Updates the configuration with verbose output enabled or disabled.
542539
/// </summary>
543540
/// <param name="level">The verbosity level.</param>
544541
[Obsolete("WithVerbosityEnabled(int level) is deprecated; use WithVerbosityEnabled(bool isVerbose) instead.")]
@@ -549,49 +546,44 @@ public Configuration WithVerbosityEnabled(int level)
549546
}
550547

551548
/// <summary>
552-
/// Updates the configuration with debugging information enabled
553-
/// or disabled and returns it.
549+
/// Updates the configuration with the specified scheduling strategy.
554550
/// </summary>
555-
/// <param name="isEnabled">Is debugging enabled</param>
556-
/// <returns>Configuration</returns>
557-
public Configuration WithDebuggingEnabled(bool isEnabled = true)
551+
/// <param name="strategy">The scheduling strategy.</param>
552+
public Configuration WithStrategy(SchedulingStrategy strategy)
558553
{
559-
Debug.IsEnabled = isEnabled;
554+
this.SchedulingStrategy = strategy;
560555
return this;
561556
}
562557

563558
/// <summary>
564-
/// Updates the configuration with the scheduling strategy
565-
/// and returns it.
559+
/// Updates the configuration with the specified number of iterations to perform.
566560
/// </summary>
567-
/// <param name="strategy">SchedulingStrategy</param>
568-
/// <returns>Configuration</returns>
569-
public Configuration WithStrategy(SchedulingStrategy strategy)
561+
/// <param name="iterations">The number of iterations to perform.</param>
562+
public Configuration WithNumberOfIterations(int iterations)
570563
{
571-
this.SchedulingStrategy = strategy;
564+
this.SchedulingIterations = iterations;
572565
return this;
573566
}
574567

575568
/// <summary>
576-
/// Updates the configuration with the number of iterations
577-
/// and returns it.
569+
/// Updates the configuration with the specified number of scheduling steps
570+
/// to perform per iteration (for both fair and unfair schedulers).
578571
/// </summary>
579-
/// <param name="iterations">Number of iterations</param>
580-
/// <returns>Configuration</returns>
581-
public Configuration WithNumberOfIterations(int iterations)
572+
/// <param name="maxSteps">The scheduling steps to perform per iteration.</param>
573+
public Configuration WithMaxSteps(int maxSteps)
582574
{
583-
this.SchedulingIterations = iterations;
575+
this.MaxSchedulingSteps = maxSteps;
584576
return this;
585577
}
586578

587579
/// <summary>
588580
/// Indicates whether the requested C# version is supported for for rewriting.
589581
/// </summary>
590-
/// <param name="major">The major version required</param>
591-
/// <param name="minor">The minor version required</param>
582+
/// <param name="major">The required major version.</param>
583+
/// <param name="minor">The required minor version.</param>
592584
public bool IsRewriteCSharpVersion(int major, int minor)
593585
{
594-
// Return true if not set
586+
// Return true if not set.
595587
return this.RewriteCSharpVersion.Major == 0
596588
|| this.RewriteCSharpVersion >= new Version(major, minor);
597589
}

Source/Core/IO/Logging/MachineLogger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public virtual void OnPop(MachineId machineId, string currStateName, string rest
232232
/// <param name="restoredStateName">The name of the state being re-entered, if any</param>
233233
public virtual string FormatOnPopString(MachineId machineId, string currStateName, string restoredStateName)
234234
{
235-
var curStateName = string.IsNullOrEmpty(currStateName) ? "[not recorded]" : currStateName;
235+
currStateName = string.IsNullOrEmpty(currStateName) ? "[not recorded]" : currStateName;
236236
var reenteredStateName = restoredStateName ?? string.Empty;
237237
return $"<PopLog> Machine '{machineId}' popped state '{currStateName}' and reentered state '{reenteredStateName}'.";
238238
}
@@ -341,7 +341,7 @@ public virtual void OnWait(MachineId machineId, string currStateName, params Typ
341341
/// <param name="eventTypes">The types of the events being waited for, if any.</param>
342342
public virtual string FormatOnWaitString(MachineId machineId, string currStateName, params Type[] eventTypes)
343343
{
344-
string eventNames = string.Empty;
344+
string eventNames;
345345
if (eventTypes.Length == 0)
346346
{
347347
eventNames = "'<missing>'";

Source/Core/Properties/InternalsVisibleTo.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
"d7bc9fd67a08d3fa122120a469158da22a652af4508571ac9b16c6a05d2b3b6d7004ac76be85c3" +
6969
"ca3d55f6ae823cd287a2810243f2bd6be5f4ba7b016c80da954371e591b10c97b0938f721c7149" +
7070
"3bc97f9e")]
71+
[assembly: InternalsVisibleTo("PSharpTestLauncher,PublicKey=" +
72+
"0024000004800000940000000602000000240000525341310004000001000100d7971281941569" +
73+
"53fd8af100ac5ecaf1d96fab578562b91133663d6ccbf0b313d037a830a20d7af1ce02a6641d71" +
74+
"d7bc9fd67a08d3fa122120a469158da22a652af4508571ac9b16c6a05d2b3b6d7004ac76be85c3" +
75+
"ca3d55f6ae823cd287a2810243f2bd6be5f4ba7b016c80da954371e591b10c97b0938f721c7149" +
76+
"3bc97f9e")]
7177

7278
// Tests
7379
[assembly: InternalsVisibleTo("Microsoft.PSharp.Core.Tests,PublicKey=" +
@@ -106,9 +112,3 @@
106112
"d7bc9fd67a08d3fa122120a469158da22a652af4508571ac9b16c6a05d2b3b6d7004ac76be85c3" +
107113
"ca3d55f6ae823cd287a2810243f2bd6be5f4ba7b016c80da954371e591b10c97b0938f721c7149" +
108114
"3bc97f9e")]
109-
[assembly: InternalsVisibleTo("Microsoft.PSharp.Tests.Launcher,PublicKey=" +
110-
"0024000004800000940000000602000000240000525341310004000001000100d7971281941569" +
111-
"53fd8af100ac5ecaf1d96fab578562b91133663d6ccbf0b313d037a830a20d7af1ce02a6641d71" +
112-
"d7bc9fd67a08d3fa122120a469158da22a652af4508571ac9b16c6a05d2b3b6d7004ac76be85c3" +
113-
"ca3d55f6ae823cd287a2810243f2bd6be5f4ba7b016c80da954371e591b10c97b0938f721c7149" +
114-
"3bc97f9e")]

Source/Core/Runtime/MachineRuntime.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,6 @@ internal virtual void NotifyDefaultHandlerFired(Machine machine)
577577
// Override to implement the notification.
578578
}
579579

580-
/// <summary>
581-
/// Logs the specified text.
582-
/// </summary>
583-
protected internal virtual void Log(string format, params object[] args)
584-
{
585-
this.Logger.WriteLine(format, args);
586-
}
587-
588580
/// <summary>
589581
/// Installs the specified <see cref="ILogger"/>.
590582
/// </summary>

Source/Core/Runtime/Machines/AsyncMachine.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System;
77
using System.ComponentModel;
88

9+
using Microsoft.PSharp.IO;
10+
911
namespace Microsoft.PSharp.Runtime
1012
{
1113
/// <summary>
@@ -31,6 +33,11 @@ public abstract class AsyncMachine
3133
/// </summary>
3234
protected internal Guid OperationGroupId { get; set; }
3335

36+
/// <summary>
37+
/// The logger installed to the P# runtime.
38+
/// </summary>
39+
protected ILogger Logger => this.Runtime.Logger;
40+
3441
/// <summary>
3542
/// Initializes this machine.
3643
/// </summary>

Source/Core/Runtime/Machines/Machine.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ public abstract class Machine : AsyncMachine
111111
/// </summary>
112112
private bool OnExceptionRequestedGracefulHalt;
113113

114-
/// <summary>
115-
/// The logger installed to the P# runtime.
116-
/// </summary>
117-
protected ILogger Logger => this.Runtime.Logger;
118-
119114
/// <summary>
120115
/// Gets the <see cref="Type"/> of the current state.
121116
/// </summary>
@@ -856,14 +851,12 @@ private async Task ExecuteAction(CachedAction cachedAction)
856851
if (innerException is ExecutionCanceledException)
857852
{
858853
this.IsHalted = true;
859-
Debug.WriteLine("<Exception> ExecutionCanceledException was " +
860-
$"thrown from Machine '{this.Id}'.");
854+
Debug.WriteLine($"<Exception> ExecutionCanceledException was thrown from Machine '{this.Id}'.");
861855
}
862856
else if (innerException is TaskSchedulerException)
863857
{
864858
this.IsHalted = true;
865-
Debug.WriteLine("<Exception> TaskSchedulerException was " +
866-
$"thrown from Machine '{this.Id}'.");
859+
Debug.WriteLine($"<Exception> TaskSchedulerException was thrown from Machine '{this.Id}'.");
867860
}
868861
else if (this.OnExceptionRequestedGracefulHalt)
869862
{
@@ -1539,7 +1532,7 @@ private bool OnExceptionHandler(string methodName, Exception ex)
15391532
{
15401533
if (ex is ExecutionCanceledException)
15411534
{
1542-
// internal exception, used by PsharpTester
1535+
// Internal exception, used during testing.
15431536
return false;
15441537
}
15451538

Source/Core/Runtime/Specifications/Monitor.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System;
77
using System.Collections.Concurrent;
88
using System.Collections.Generic;
9-
using System.Diagnostics;
109
using System.Linq;
1110
using System.Linq.Expressions;
1211
using System.Reflection;
@@ -195,7 +194,7 @@ protected void Goto(Type s)
195194
{
196195
// If the state is not a state of the monitor, then report an error and exit.
197196
this.Assert(StateTypeMap[this.GetType()].Any(val => val.DeclaringType.Equals(s.DeclaringType) && val.Name.Equals(s.Name)),
198-
"Monitor '{0}' is trying to transition to non-existing state '{1}'.", this.Id, s.Name);
197+
"Monitor '{0}' is trying to transition to non-existing state '{1}'.", this.GetType().Name, s.Name);
199198
this.Raise(new GotoStateEvent(s));
200199
}
201200

@@ -297,7 +296,7 @@ private void HandleEvent(Event e)
297296
/// <summary>
298297
/// Invokes an action.
299298
/// </summary>
300-
[DebuggerStepThrough]
299+
[System.Diagnostics.DebuggerStepThrough]
301300
private void Do(string actionName)
302301
{
303302
MethodInfo action = this.ActionMap[actionName];
@@ -308,7 +307,7 @@ private void Do(string actionName)
308307
/// <summary>
309308
/// Executes the on entry function of the current state.
310309
/// </summary>
311-
[DebuggerStepThrough]
310+
[System.Diagnostics.DebuggerStepThrough]
312311
private void ExecuteCurrentStateOnEntry()
313312
{
314313
this.Runtime.NotifyEnteredState(this);
@@ -330,7 +329,7 @@ private void ExecuteCurrentStateOnEntry()
330329
/// <summary>
331330
/// Executes the on exit function of the current state.
332331
/// </summary>
333-
[DebuggerStepThrough]
332+
[System.Diagnostics.DebuggerStepThrough]
334333
private void ExecuteCurrentStateOnExit(string eventHandlerExitActionName)
335334
{
336335
this.Runtime.NotifyExitedState(this);
@@ -360,7 +359,7 @@ private void ExecuteCurrentStateOnExit(string eventHandlerExitActionName)
360359
/// <summary>
361360
/// Executes the specified action.
362361
/// </summary>
363-
[DebuggerStepThrough]
362+
[System.Diagnostics.DebuggerStepThrough]
364363
private void ExecuteAction(MethodInfo action)
365364
{
366365
try
@@ -598,7 +597,7 @@ internal void InitializeStateInformation()
598597
(state.IsCold && !state.IsHot) ||
599598
(!state.IsCold && state.IsHot) ||
600599
(!state.IsCold && !state.IsHot),
601-
"State '{0}' of monitor '{1}' cannot be both cold and hot.", type.FullName, this.Id);
600+
"State '{0}' of monitor '{1}' cannot be both cold and hot.", type.FullName, this.GetType().Name);
602601

603602
StateMap[monitorType].Add(state);
604603
}
@@ -655,8 +654,8 @@ internal void InitializeStateInformation()
655654
}
656655

657656
var initialStates = StateMap[monitorType].Where(state => state.IsStart).ToList();
658-
this.Assert(initialStates.Count != 0, "Monitor '{0}' must declare a start state.", this.Id);
659-
this.Assert(initialStates.Count == 1, "Monitor '{0}' can not declare more than one start states.", this.Id);
657+
this.Assert(initialStates.Count != 0, "Monitor '{0}' must declare a start state.", this.GetType().Name);
658+
this.Assert(initialStates.Count == 1, "Monitor '{0}' can not declare more than one start states.", this.GetType().Name);
660659

661660
this.ConfigureStateTransitions(initialStates.Single());
662661
this.State = initialStates.Single();
@@ -750,7 +749,7 @@ private void ReportUnhandledException(Exception ex, string actionName)
750749
{
751750
var state = this.CurrentState is null ? "<unknown>" : this.CurrentStateName;
752751
this.Runtime.WrapAndThrowException(ex, $"Exception '{ex.GetType()}' was thrown " +
753-
$"in monitor '{this.Id}', state '{state}', action '{actionName}', " +
752+
$"in monitor '{this.GetType().Name}', state '{state}', action '{actionName}', " +
754753
$"'{ex.Source}':\n" +
755754
$" {ex.Message}\n" +
756755
$"The stack trace is:\n{ex.StackTrace}");
@@ -761,7 +760,7 @@ private void ReportUnhandledException(Exception ex, string actionName)
761760
/// </summary>
762761
internal HashSet<string> GetAllStates()
763762
{
764-
this.Assert(StateMap.ContainsKey(this.GetType()), "Monitor '{0}' hasn't populated its states yet.", this.Id);
763+
this.Assert(StateMap.ContainsKey(this.GetType()), "Monitor '{0}' hasn't populated its states yet.", this.GetType().Name);
765764

766765
var allStates = new HashSet<string>();
767766
foreach (var state in StateMap[this.GetType()])
@@ -777,7 +776,7 @@ internal HashSet<string> GetAllStates()
777776
/// </summary>
778777
internal HashSet<Tuple<string, string>> GetAllStateEventPairs()
779778
{
780-
this.Assert(StateMap.ContainsKey(this.GetType()), "Monitor '{0}' hasn't populated its states yet.", this.Id);
779+
this.Assert(StateMap.ContainsKey(this.GetType()), "Monitor '{0}' hasn't populated its states yet.", this.GetType().Name);
781780

782781
var pairs = new HashSet<Tuple<string, string>>();
783782
foreach (var state in StateMap[this.GetType()])

Source/Core/Utilities/Profiler.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ public void StartMeasuringExecutionTime()
2828
/// </summary>
2929
public void StopMeasuringExecutionTime()
3030
{
31-
this.StopWatch.Stop();
31+
if (this.StopWatch != null)
32+
{
33+
this.StopWatch.Stop();
34+
}
3235
}
3336

3437
/// <summary>
3538
/// Returns profilling results.
3639
/// </summary>
37-
/// <returns>Seconds</returns>
38-
public double Results()
39-
{
40-
return this.StopWatch.Elapsed.TotalSeconds;
41-
}
40+
public double Results() =>
41+
this.StopWatch != null ? this.StopWatch.Elapsed.TotalSeconds : 0;
4242
}
4343
}

Source/SharedObjects/SharedCounter/MockSharedCounter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public MockSharedCounter(int value, SystematicTestingRuntime runtime)
2929
{
3030
this.Runtime = runtime;
3131
this.CounterMachine = this.Runtime.CreateMachine(typeof(SharedCounterMachine));
32-
var currentMachine = this.Runtime.GetCurrentMachine();
32+
var currentMachine = this.Runtime.GetExecutingMachine<Machine>();
3333
this.Runtime.SendEvent(this.CounterMachine, SharedCounterEvent.SetEvent(currentMachine.Id, value));
3434
currentMachine.Receive(typeof(SharedCounterResponseEvent)).Wait();
3535
}
@@ -55,7 +55,7 @@ public void Decrement()
5555
/// </summary>
5656
public int GetValue()
5757
{
58-
var currentMachine = this.Runtime.GetCurrentMachine();
58+
var currentMachine = this.Runtime.GetExecutingMachine<Machine>();
5959
this.Runtime.SendEvent(this.CounterMachine, SharedCounterEvent.GetEvent(currentMachine.Id));
6060
var response = currentMachine.Receive(typeof(SharedCounterResponseEvent)).Result;
6161
return (response as SharedCounterResponseEvent).Value;
@@ -66,7 +66,7 @@ public int GetValue()
6666
/// </summary>
6767
public int Add(int value)
6868
{
69-
var currentMachine = this.Runtime.GetCurrentMachine();
69+
var currentMachine = this.Runtime.GetExecutingMachine<Machine>();
7070
this.Runtime.SendEvent(this.CounterMachine, SharedCounterEvent.AddEvent(currentMachine.Id, value));
7171
var response = currentMachine.Receive(typeof(SharedCounterResponseEvent)).Result;
7272
return (response as SharedCounterResponseEvent).Value;
@@ -77,7 +77,7 @@ public int Add(int value)
7777
/// </summary>
7878
public int Exchange(int value)
7979
{
80-
var currentMachine = this.Runtime.GetCurrentMachine();
80+
var currentMachine = this.Runtime.GetExecutingMachine<Machine>();
8181
this.Runtime.SendEvent(this.CounterMachine, SharedCounterEvent.SetEvent(currentMachine.Id, value));
8282
var response = currentMachine.Receive(typeof(SharedCounterResponseEvent)).Result;
8383
return (response as SharedCounterResponseEvent).Value;
@@ -88,7 +88,7 @@ public int Exchange(int value)
8888
/// </summary>
8989
public int CompareExchange(int value, int comparand)
9090
{
91-
var currentMachine = this.Runtime.GetCurrentMachine();
91+
var currentMachine = this.Runtime.GetExecutingMachine<Machine>();
9292
this.Runtime.SendEvent(this.CounterMachine, SharedCounterEvent.CasEvent(currentMachine.Id, value, comparand));
9393
var response = currentMachine.Receive(typeof(SharedCounterResponseEvent)).Result;
9494
return (response as SharedCounterResponseEvent).Value;

0 commit comments

Comments
 (0)