static async Task Run(IActorSystem system) { var rwx = system.ActorOf <ReaderWriterLock>("rw-x"); await rwx.Ask <int>(new Read()); // warm-up var writes = new List <Task> { rwx.Tell(new Write { Value = 1, Delay = TimeSpan.FromMilliseconds(1400) }), rwx.Tell(new Write { Value = 2, Delay = TimeSpan.FromMilliseconds(600) }), }; var cts = new CancellationTokenSource(); var reads = new List <int>(); Console.Write("\nReads: "); var indicator = ConsolePosition.Current(); Task.Run(async() => { while (!cts.Token.IsCancellationRequested) { reads.Add(await rwx.Ask(new Read())); indicator.Write(reads.Count); } }, cts.Token).Ignore(); await Task.WhenAll(writes); cts.Cancel(); Debug.Assert(reads.Count > writes.Count * 100, "Should actually serve reads in parallel, while there are slow sequential writes in flight"); Debug.Assert(reads.OrderBy(x => x).SequenceEqual(reads), "All readers should see consistently incrementing sequence, despite that 2nd write is faster. Writes are queued"); Debug.Assert(reads.Distinct().SequenceEqual(new[] { 1, 2 }), "Should see all changes of the write sequence"); }
public override Task OnActivate() { Console.Write("\nWrites: "); indicator = ConsolePosition.Current(); return(base.OnActivate()); }
void On(Activate _) { Console.Write("\nWrites: "); indicator = ConsolePosition.Current(); }