-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Emil Koutanov
committed
Jan 28, 2024
1 parent
0ad39bf
commit 7fad999
Showing
6 changed files
with
115 additions
and
24 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"cSpell.words": [ | ||
"Schedulable" | ||
] | ||
} |
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 |
---|---|---|
@@ -1,15 +1,92 @@ | ||
using System.Diagnostics; | ||
|
||
namespace Actors.Tests; | ||
|
||
static class IEnumerableExtensions | ||
{ | ||
public static void AssertNoError(this PausingActor[] actors) | ||
{ | ||
foreach (var actor in actors) | ||
{ | ||
actor.AssertNoError(); | ||
} | ||
} | ||
} | ||
|
||
[TestClass] | ||
public class TroupeTest | ||
{ | ||
[TestMethod] | ||
public void TestMembers() | ||
{ | ||
PausingActor[] actors = [new PausingActor(), new PausingActor()]; | ||
var troupe = Troupe.Of(actors); | ||
CollectionAssert.AreEqual(actors, troupe.Members); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestDrainAny() | ||
{ | ||
// PausingActor[] actors = [new PausingActor(), new PausingActor()]; | ||
// IEnumerable<PausingActor> x = actors.AsEnumerable(); | ||
// var troupe = Troupe<PausingActor>.Of(x); | ||
PausingActor[] actors = [new PausingActor(), new PausingActor()]; | ||
var troupe = Troupe.Of(actors); | ||
|
||
var m0 = new Barrier(); | ||
var m1 = new Barrier(); | ||
|
||
actors[0].Send(m0); | ||
actors[1].Send(m1); | ||
|
||
var drain = troupe.DrainAny(); | ||
|
||
// no drain task is completed if all actors are still in Perform() | ||
await m0.Entered.Task; | ||
await m1.Entered.Task; | ||
Assert.IsTrue(actors[0].Scheduled); | ||
Assert.IsTrue(actors[1].Scheduled); | ||
Assert.IsFalse(drain.IsCompleted); | ||
|
||
// allow one actor to complete and await drainage | ||
m0.Resume.SetResult(); | ||
await drain; | ||
Assert.IsFalse(actors[0].Scheduled); | ||
|
||
// complete the second actor and let it drain | ||
m1.Resume.SetResult(); | ||
await actors[1].Drain(); | ||
Assert.IsFalse(actors[1].Scheduled); | ||
|
||
actors.AssertNoError(); | ||
} | ||
[TestMethod] | ||
public async Task TestDrainAll() | ||
{ | ||
PausingActor[] actors = [new PausingActor(), new PausingActor()]; | ||
var troupe = Troupe.Of(actors); | ||
|
||
var m0 = new Barrier(); | ||
var m1 = new Barrier(); | ||
|
||
actors[0].Send(m0); | ||
actors[1].Send(m1); | ||
|
||
var drain = troupe.DrainAll(); | ||
|
||
// no drain task is completed if all actors are still in Perform() | ||
await m0.Entered.Task; | ||
await m1.Entered.Task; | ||
Assert.IsTrue(actors[0].Scheduled); | ||
Assert.IsTrue(actors[1].Scheduled); | ||
Assert.IsFalse(drain.IsCompleted); | ||
|
||
// allow one actor to complete and drain it... troupe drain will still not be completed | ||
m0.Resume.SetResult(); | ||
await actors[0].Drain(); | ||
Assert.IsFalse(actors[0].Scheduled); | ||
Assert.IsFalse(drain.IsCompleted); | ||
|
||
// complete the second actor and await troupe drainage | ||
m1.Resume.SetResult(); | ||
await drain; | ||
Assert.IsFalse(actors[1].Scheduled); | ||
|
||
actors.AssertNoError(); | ||
} | ||
} |
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,8 @@ | ||
namespace Actors; | ||
|
||
public interface ISchedulable | ||
{ | ||
bool Scheduled { get; } | ||
|
||
Task Drain(); | ||
} |
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