public GrainTaskScheduler(TaskScheduler outerScheduler, ExceptionSink exceptionSink) { _outerScheduler = outerScheduler; _exceptionSink = exceptionSink; _cancelSource = new CancellationTokenSource(); _cancelToken = _cancelSource.Token; }
public async Task IsolatedRequestsWaitForPrecedingReentrantsToClearOff() { var exceptions = new ExceptionSink(); var requests = new RequestRunner(TaskScheduler.Default, exceptions); long callCount = 0; requests.PerformAndForget(async() => { Interlocked.Increment(ref callCount); await Task.Delay(100); Interlocked.Decrement(ref callCount); }, RequestMode.Reentrant); await Task.Delay(15); bool clashed = await requests.Perform(() => { var c = Interlocked.Read(ref callCount); return(Task.FromResult(c > 0)); }, RequestMode.Isolated); await requests.WhenIdle(); exceptions.RethrowAll(); Assert.That(clashed, Is.False); }
public Stream(StreamKey key, StreamRegistry streamReg, IPlacementDispatcher disp, ExceptionSink exceptions) { Key = key; _streamReg = streamReg; _disp = disp; _exceptions = exceptions; _dSubscriptions = new ConcurrentDictionary <Guid, Subscription>(); }
public Subscription(SubKey key, Placement placement, Stream stream, IPlacementDispatcher disp, ExceptionSink exceptions, bool isImplicit) { Key = key; Placement = placement; Stream = stream; Dispatcher = disp; Exceptions = exceptions; IsImplicit = isImplicit; }
public StreamRegistry(IPlacementDispatcher disp, ExceptionSink exceptions, TypeMap typeMap) { _disp = disp; _exceptions = exceptions; _dStreams = new ConcurrentDictionary <StreamKey, Stream>(); _dImplicitSubTypes = new ConcurrentDictionary <string, ConcurrentBag <Type> >(); typeMap.AddTypeProcessor(DetectImplicitStreamSubs); }
public void Closing_DisallowsFurtherRequests() { var exceptions = new ExceptionSink(); var requests = new RequestRunner(TaskScheduler.Default, exceptions); requests.Close(() => Task.Delay(50)); Assert.That( () => requests.Perform(() => Task.CompletedTask), Throws.Exception.InstanceOf <RequestRunnerClosedException>()); }
public async Task Closing_IsAwaitable() { var exceptions = new ExceptionSink(); var requests = new RequestRunner(TaskScheduler.Default, exceptions); bool completed = false; await requests.Close(() => Task.Delay(500).ContinueWith(_ => completed = true)); Assert.That(completed); }
public async Task Closes_Immediately_WhenNoneWaiting() { var exceptions = new ExceptionSink(); var requests = new RequestRunner(TaskScheduler.Default, exceptions); bool closed = false; requests.Close(() => { closed = true; return(Task.CompletedTask); }); await requests.WhenIdle(); exceptions.RethrowAll(); Assert.That(closed, Is.True); }
public async Task WhenIdle_AwaitsCompletionOfDeactivation() { var exceptions = new ExceptionSink(); var requests = new RequestRunner(TaskScheduler.Default, exceptions); bool closed = false; requests.Close(async() => { await Task.Delay(100); closed = true; }); await requests.WhenIdle(); exceptions.RethrowAll(); Assert.That(closed, Is.True); }
public async Task IsolatedRequestsIsolatedFromEachOther() { var exceptions = new ExceptionSink(); var requests = new RequestRunner(TaskScheduler.Default, exceptions); List <int> callCounts = new List <int>(); int callCount = 0; await Enumerable.Range(0, 20) .Select(_ => requests.Perform(async() => { int c = Interlocked.Increment(ref callCount); callCounts.Add(c); await Task.Delay(15); Interlocked.Decrement(ref callCount); }, RequestMode.Isolated) ).WhenAll(); Assert.That(callCounts.All(c => c == 1)); }
public async Task Closing_Performed() { var exceptions = new ExceptionSink(); var requests = new RequestRunner(TaskScheduler.Default, exceptions); bool closed = false; requests.PerformAndForget(() => Task.Delay(50)); requests.Close(() => { closed = true; return(Task.CompletedTask); }); await requests.WhenIdle(); exceptions.RethrowAll(); Assert.That(closed, Is.True); }
public async Task IsolatedRequestsRespectedByReentrantFollowers() { var exceptions = new ExceptionSink(); var requests = new RequestRunner(TaskScheduler.Default, exceptions); bool isolatedExecuting = false; var tIsolated = requests.Perform(async() => { isolatedExecuting = true; await Task.Delay(100); isolatedExecuting = false; }, RequestMode.Isolated); var clashed = await requests.Perform(async() => { await Task.Delay(15); return(isolatedExecuting); }, RequestMode.Reentrant); await tIsolated; Assert.That(clashed, Is.False); }
public void SetUp() { _placement = new Placement(new AbstractKey(typeof(ITestGrain), Guid.NewGuid()), typeof(TestGrain)); _grain = Substitute.For <Grain>(); //integration testing could even be automatically done by substituting mocks for realities. //_grainFac = Substitute.For<Func<IActivationDispatcher, Grain>>(); //_grainFac(Arg.Any<IActivationDispatcher>()).Returns(_grain); var exceptionSink = new ExceptionSink(); _runner = new RequestRunner(new GrainTaskScheduler(new FixtureScheduler(), exceptionSink), exceptionSink); _ctx = Substitute.For <IGrainContext>(); _ctx.Grain.Returns(_grain); _ctxFac = Substitute.For <Func <Task <IGrainContext> > >(); _ctxFac().Returns(_ctx); _disp = new ActivationDispatcher(_runner, _ctxFac); _fn = Substitute.For <Func <IGrainContext, Task <bool> > >(); }
public async Task Closes_OnlyAfter_PreviouslyScheduledPerformances() { var exceptions = new ExceptionSink(); var requests = new RequestRunner(TaskScheduler.Default, exceptions); int calls = 0; Enumerable.Range(0, 100) .Select(_ => requests.Perform(async() => { Interlocked.Increment(ref calls); await Task.Delay(30); Interlocked.Decrement(ref calls); })) .ToArray(); requests.Close(() => { Assert.That(calls, Is.EqualTo(0)); return(Task.CompletedTask); }); await requests.WhenIdle(); exceptions.RethrowAll(); }