public async Task RunCoroutine_YieldCustomWaitObjectWithNotify() { var scheduler = new InterleavedCoroutineScheduler(); var waitObject = new CustomWaitObjectWithNotifyCompletion(); var coroutine = new CustomWaitObjectCoroutine(waitObject); scheduler.Execute(coroutine); scheduler.Update(0); Assert.Equal(CoroutineStatus.Running, coroutine.Status); Task completionTask = Task.Run(async() => { await Task.Delay(500); waitObject.Complete(null); }); while (coroutine.Status == CoroutineStatus.Running) { scheduler.Update(0); await Task.Delay(10); } Assert.Equal(TaskStatus.RanToCompletion, completionTask.Status); Assert.Equal(CoroutineStatus.CompletedNormal, coroutine.Status); }
public void WaitForAnyWithCancel() { var scheduler = new InterleavedCoroutineScheduler(); var sub1 = new WaitForAnyWithCancelTestCoroutine(1); var sub2 = new WaitForAnyWithCancelTestCoroutine(2); var sub3 = new WaitForAnyWithCancelTestCoroutine(3); var coroutine = new WaitForAnyWithCancelTestCoroutine(sub1, sub2, sub3); scheduler.Execute(coroutine); Assert.Equal(CoroutineStatus.WaitingForStart, sub1.Status); scheduler.Update(0); Assert.Equal(CoroutineStatus.Running, sub1.Status); Assert.Equal(CoroutineStatus.Running, sub2.Status); Assert.Equal(CoroutineStatus.Running, sub3.Status); scheduler.Update(0); Assert.Equal(CoroutineStatus.CompletedNormal, sub1.Status); // Not immediatelly trigerred because WaitFor uses polling scheduler.Update(0); Assert.Equal(CoroutineStatus.Cancelled, sub2.Status); Assert.Equal(CoroutineStatus.Cancelled, sub3.Status); Assert.Equal(CoroutineStatus.CompletedNormal, coroutine.Status); }
public void CoroutineExecutionState() { var scheduler = new InterleavedCoroutineScheduler(); var coroutine = new ExecutionStateCoroutine(); scheduler.Execute(coroutine); scheduler.Update(0.1f); scheduler.Update(0); scheduler.Update(0.11f); }
public void CoroutineSpawner() { var waitObject = new CustomWaitObject(); var coroutine = new SpawnerCoroutine(); var scheduler = new InterleavedCoroutineScheduler(); scheduler.Execute(coroutine); scheduler.Update(0); Assert.Equal(coroutine, coroutine.Spawned.Spawner); }
public void CoroutineReturnValue_ForgotToSetResult() { var scheduler = new InterleavedCoroutineScheduler(); var coroutine = new ReturnValueCoroutine(1); scheduler.Execute(coroutine); scheduler.Update(0); Assert.Equal(CoroutineStatus.Running, coroutine.Status); scheduler.Update(0); Assert.Equal(CoroutineStatus.CompletedNormal, coroutine.Status); Assert.Throws <CoroutineException>(() => coroutine.Result); }
public void RunCoroutine_ExecuteThrowsException() { var scheduler = new InterleavedCoroutineScheduler(); var coroutine = new RunCoroutineExecuteThrowsException(); Assert.Equal(CoroutineStatus.WaitingForStart, coroutine.Status); // This throws exception on Execute as there are no yield statements // and calling that method actually executes it (it does not prepare enumeration) Assert.Throws <Exception>(() => scheduler.Execute(coroutine)); }
public void CoroutineExceptionPropagation() { var coroutine = new BaseCancelCoroutine(); var scheduler = new InterleavedCoroutineScheduler(); scheduler.Execute(coroutine); scheduler.Update(0); coroutine.Internal.Cancel(); scheduler.Update(0); Assert.Equal(CoroutineStatus.Cancelled, coroutine.Internal.Status); Assert.Equal(CoroutineStatus.CompletedWithException, coroutine.Status); Assert.IsType <AggregateException>(coroutine.Exception); }
public void CoroutineCancelTest() { var scheduler = new InterleavedCoroutineScheduler(); var coroutine = new CancelCoroutine(); scheduler.Execute(coroutine); Assert.Equal(0, coroutine.Iteration); scheduler.Update(0.0f); Assert.Equal(1, coroutine.Iteration); coroutine.Cancel(); scheduler.Update(0.0f); Assert.Equal(1, coroutine.Iteration); }
public void CoroutineReturnValue() { var scheduler = new InterleavedCoroutineScheduler(); var coroutine = new ReturnValueCoroutine(3); scheduler.Execute(coroutine); scheduler.Update(0); Assert.Equal(CoroutineStatus.Running, coroutine.Status); scheduler.Update(0); Assert.Equal(CoroutineStatus.Running, coroutine.Status); scheduler.Update(0); Assert.Equal(CoroutineStatus.CompletedNormal, coroutine.Status); Assert.Equal(2, coroutine.Result); }
public void Coroutine_WhenOnWaitObject() { var waitObject = new CustomWaitObject(); var coroutine = Coroutine.FromEnumerator(WaitForWaitObject(waitObject)); var scheduler = new InterleavedCoroutineScheduler(); scheduler.Execute(coroutine); scheduler.Update(0); waitObject.Exception = new Exception(); waitObject.IsComplete = true; scheduler.Update(0); Assert.Equal(CoroutineStatus.CompletedWithException, coroutine.Status); Assert.IsType <AggregateException>(coroutine.Exception); }
public void WaitForAsync() { var scheduler = new InterleavedCoroutineScheduler(); var coroutine = new AwaitCoroutine(); scheduler.Execute(coroutine); while (!coroutine.IsComplete) { scheduler.Update(0.0f); } Assert.Equal("TEST-DONE", coroutine.Result); }
public async Task WaitForExceptionPropagation() { var scheduler = new InterleavedCoroutineScheduler(); var coroutine = new WaitForExceptionPropagationCoroutine(); scheduler.Execute(coroutine); scheduler.Update(0); Assert.Equal(CoroutineStatus.Running, coroutine.Status); await Task.Delay(50); scheduler.Update(0); Assert.Equal(CoroutineStatus.CompletedWithException, coroutine.Status); Assert.IsType <AggregateException>(coroutine.Exception); }
public void RunCoroutine_ThrowsException() { var scheduler = new InterleavedCoroutineScheduler(); var coroutine = new RunCoroutineExecuteThrowsExceptionInCoroutine(1); Assert.Equal(CoroutineStatus.WaitingForStart, coroutine.Status); scheduler.Execute(coroutine); Assert.Equal(CoroutineStatus.Running, coroutine.Status); scheduler.Update(0); Assert.Equal(CoroutineStatus.Running, coroutine.Status); scheduler.Update(0); Assert.Equal(CoroutineStatus.CompletedWithException, coroutine.Status); Assert.IsType <Exception>(coroutine.Exception); }
public async Task WaitForAny() { var scheduler = new InterleavedCoroutineScheduler(); var coroutine = new WaitForAnyTestCoroutine(); scheduler.Execute(coroutine); scheduler.Update(0); await Task.Delay(50); // All async waiters still executing scheduler.Update(0.05f); Assert.Equal(CoroutineStatus.Running, coroutine.Status); await Task.Delay(150); // One should be done here scheduler.Update(0.150f); Assert.Equal(CoroutineStatus.CompletedNormal, coroutine.Status); }
public void WaitForTime() { var coroutine = new WaitCoroutine(1.0f); var scheduler = new InterleavedCoroutineScheduler(); scheduler.Execute(coroutine); scheduler.Update(0.1f); Assert.Equal(CoroutineStatus.Running, coroutine.Status); scheduler.Update(0.5f); scheduler.Update(0.2f); Assert.Equal(0, coroutine.Iteration); scheduler.Update(0.31f); Assert.Equal(1, coroutine.Iteration); //< Wait is trigerred scheduler.Update(0.01f); Assert.Equal(1, coroutine.Iteration); Assert.Equal(CoroutineStatus.CompletedNormal, coroutine.Status); }
private static void CoroutineUpdateTest() { const int LoopsNum = 10000000; { var scheduler = new InterleavedCoroutineScheduler(); var coroutine = new SimpleCoroutine(LoopsNum); scheduler.Execute(coroutine); DateTime start = DateTime.UtcNow; while (coroutine.Status != CoroutineStatus.CompletedNormal) { scheduler.Update(0); } DateTime end = DateTime.UtcNow; Console.WriteLine($"Time to process {LoopsNum:n0} coroutine updates by interleved scheduler: {(end - start).TotalSeconds} s"); } { var eventQueue = new EventQueue(); var scheduler = new EventCoroutineScheduler(eventQueue); var coroutine = new SimpleCoroutine(LoopsNum); scheduler.Execute(coroutine); DateTime start = DateTime.UtcNow; while (true) { if (!eventQueue.TryDequeue(out IEvent ev)) { break; } scheduler.Update((ICoroutineEvent)ev); scheduler.NewFrame(0); eventQueue.NewFrame(); } DateTime end = DateTime.UtcNow; Console.WriteLine($"Time to process {LoopsNum:n0} coroutine updates by event scheduler: {(end - start).TotalSeconds} s"); } }
public void RunCoroutine_WithYieldNull() { var scheduler = new InterleavedCoroutineScheduler(); var coroutine = new RunCoroutineWithYieldNull(); Assert.Equal(CoroutineStatus.WaitingForStart, coroutine.Status); scheduler.Execute(coroutine); Assert.Equal(CoroutineStatus.Running, coroutine.Status); Assert.Equal(0, coroutine.Iteration); scheduler.Update(0); Assert.Equal(1, coroutine.Iteration); scheduler.Update(0); Assert.Equal(2, coroutine.Iteration); scheduler.Update(0); Assert.Equal(3, coroutine.Iteration); Assert.Equal(CoroutineStatus.CompletedNormal, coroutine.Status); }
public void SubCoroutine_SpawnSequentialChildren() { var coroutine = new SubCoroutine( spawnParallel: false, executeImmediatelly: false, executeChildren: new SubCoroutine[] { new SubCoroutine(), new SubCoroutine(), new SubCoroutine() }); var scheduler = new InterleavedCoroutineScheduler(); scheduler.Execute(coroutine); for (int i = 0; i < 3; i++) { scheduler.Update(0); } Assert.Equal(CoroutineStatus.Running, coroutine.Status); scheduler.Update(0); Assert.Equal(CoroutineStatus.CompletedNormal, coroutine.Status); }
public void RunCoroutine_YieldCustomWaitObject() { var scheduler = new InterleavedCoroutineScheduler(); var waitObject = new CustomWaitObject(); var coroutine = new CustomWaitObjectCoroutine(waitObject); scheduler.Execute(coroutine); for (int i = 0; i < 10; i++) { scheduler.Update(0); } Assert.Equal(CoroutineStatus.Running, coroutine.Status); waitObject.Complete(null); scheduler.Update(0); Assert.Equal(CoroutineStatus.CompletedNormal, coroutine.Status); }
public void CoroutineCancelTest_OtherThread() { var scheduler = new InterleavedCoroutineScheduler(); var coroutine = new CancelCoroutine(); scheduler.Execute(coroutine); Task.Run(async() => { await Task.Delay(100); coroutine.Cancel(); }); while (coroutine.Status == CoroutineStatus.Running) { scheduler.Update(0); } Assert.Equal(CoroutineStatus.Cancelled, coroutine.Status); }
public void RunCoroutine_YieldCustomWaitObject_WithException() { var scheduler = new InterleavedCoroutineScheduler(); var waitObject = new CustomWaitObject(); var coroutine = new CustomWaitObjectCoroutine(waitObject); scheduler.Execute(coroutine); for (int i = 0; i < 10; i++) { scheduler.Update(0); } Assert.Equal(CoroutineStatus.Running, coroutine.Status); waitObject.Complete(new Exception()); scheduler.Update(0); Assert.Equal(CoroutineStatus.CompletedWithException, coroutine.Status); Assert.IsType <AggregateException>(coroutine.Exception); }
public void SubCoroutine_ExecuteChildrenImmediatelly() { var subCoroutines = new List <SubCoroutine>() { new SubCoroutine(), new SubCoroutine(), new SubCoroutine() }; var coroutine = new SubCoroutine( spawnParallel: true, executeImmediatelly: true, executeChildren: subCoroutines.ToArray()); var scheduler = new InterleavedCoroutineScheduler(); scheduler.Execute(coroutine); scheduler.Update(0); //< Executes ALL children with one frame Assert.Equal(CoroutineStatus.Running, coroutine.Status); scheduler.Update(0); //< All children complete Assert.Equal(CoroutineStatus.CompletedNormal, coroutine.Status); }
public void ManyWaitForSecondsAtTheSameTime() { var scheduler = new InterleavedCoroutineScheduler(); Random random = new Random(31321); List <WaitCoroutine> coroutines = new List <WaitCoroutine>(); for (int i = 0; i < 100; i++) { coroutines.Add(new WaitCoroutine((float)random.NextDouble() * 3.0f)); scheduler.Execute(coroutines[i]); } for (int i = 0; i < 100; i++) { scheduler.Update(0.031f); } for (int i = 0; i < 100; i++) { Assert.Equal(CoroutineStatus.CompletedNormal, coroutines[i].Status); } }
public async Task WaitForThread() { // This test may fail sometimes if threads are busy var scheduler = new InterleavedCoroutineScheduler(); var coroutine = Coroutine.FromEnumerator(CoroutineThread()); scheduler.Execute(coroutine); Assert.Equal(CoroutineStatus.Running, coroutine.Status); scheduler.Update(0); // Null is yielded Assert.Equal(CoroutineStatus.Running, coroutine.Status); scheduler.Update(0.0f); // Thread is yielded, 300 ms before it end await Task.Delay(75); scheduler.Update(0.075f); // Thread should not be over yet Assert.Equal(CoroutineStatus.Running, coroutine.Status); await Task.Delay(75); scheduler.Update(0.075f); Assert.Equal(CoroutineStatus.CompletedNormal, coroutine.Status); }
public void SubCoroutine_SpawnOneChild() { var subCoroutine = new SubCoroutine(); var coroutine = new SubCoroutine( false, false, subCoroutine); var scheduler = new InterleavedCoroutineScheduler(); scheduler.Execute(coroutine); Assert.Equal(CoroutineStatus.WaitingForStart, subCoroutine.Status); // Yield of child executes immediatelly. As child // has one yield null, we expect that after one update, // it is still running, second update it completes // and also completes parent scheduler.Update(0); Assert.Equal(CoroutineStatus.Running, coroutine.Status); Assert.Equal(CoroutineStatus.Running, subCoroutine.Status); scheduler.Update(0); Assert.Equal(CoroutineStatus.CompletedNormal, subCoroutine.Status); Assert.Equal(CoroutineStatus.CompletedNormal, coroutine.Status); }