CancelationTests.cs
148 lines
| 3.8 KiB
| text/x-csharp
|
CSharpLexer
/ Implab.Test / CancelationTests.cs
|
|
r145 | using System; | ||
| using Implab.Parallels; | ||||
| #if MONO | ||||
| using NUnit.Framework; | ||||
| using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; | ||||
| using TestMethodAttribute = NUnit.Framework.TestAttribute; | ||||
| #else | ||||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||||
| #endif | ||||
| namespace Implab.Test { | ||||
| [TestClass] | ||||
| public class CancelationTests { | ||||
| [TestMethod] | ||||
| public void PromiseCancelTest() { | ||||
| var p = new Promise(); | ||||
| bool requested = false; | ||||
| var reason = new Exception("Test"); | ||||
| // request cancelation | ||||
| p.Cancel(reason); | ||||
| Assert.IsTrue(p.IsCancellationRequested); | ||||
| Assert.AreSame(reason, p.CancellationReason); | ||||
| Assert.IsFalse(p.IsCancelled); | ||||
| p.CancellationRequested(r => { | ||||
| Assert.AreSame(reason, r); | ||||
| requested = true; | ||||
| }); | ||||
| Assert.IsTrue(requested); | ||||
| // cancel the promise | ||||
| Assert.IsTrue(p.CancelOperationIfRequested()); | ||||
| Assert.IsTrue(p.IsCancelled); | ||||
|
|
r242 | Assert.AreSame(reason, p.RejectReason); | ||
|
|
r145 | } | ||
| [TestMethod] | ||||
| public void CancelActionBeforeStartTask() { | ||||
| bool run = false; | ||||
| var task = new ActionTask(() => { | ||||
| run = true; | ||||
|
|
r149 | }, null, null, true); | ||
|
|
r145 | |||
| // request cancelation | ||||
| task.Cancel(); | ||||
| Assert.IsTrue(task.IsCancelled); | ||||
| task.Resolve(); | ||||
| Assert.IsFalse(run); | ||||
| } | ||||
| [TestMethod] | ||||
| public void CancelActionAfterTaskStarted() { | ||||
| var finish = new Signal(); | ||||
| var started = new Signal(); | ||||
| var task = new ActionTask(() => { | ||||
| started.Set(); | ||||
| finish.Wait(); | ||||
|
|
r149 | }, null, null, true); | ||
|
|
r145 | |||
| AsyncPool.RunThread(() => { | ||||
| task.Resolve(); | ||||
| }); | ||||
| started.Wait(1000); | ||||
| task.Cancel(); | ||||
| Assert.IsTrue(task.IsCancellationRequested); | ||||
| Assert.IsFalse(task.IsCancelled); | ||||
|
|
r242 | Assert.IsFalse(task.IsFulfilled); | ||
|
|
r145 | |||
| finish.Set(); | ||||
| task.Join(1000); | ||||
| } | ||||
| [TestMethod] | ||||
| public void CancelTaskChainFromBottom() { | ||||
|
|
r149 | var started = new Signal(); | ||
|
|
r145 | var check1 = new Signal(); | ||
| var requested = false; | ||||
| var p1 = AsyncPool.RunThread(token => { | ||||
| token.CancellationRequested(reason => requested = true); | ||||
|
|
r149 | started.Set(); | ||
|
|
r145 | check1.Wait(); | ||
| token.CancelOperationIfRequested(); | ||||
| }); | ||||
|
|
r149 | started.Wait(); | ||
|
|
r145 | var p2 = p1.Then(() => { | ||
| }); | ||||
| Assert.IsFalse(p1.IsResolved); | ||||
| Assert.IsFalse(p2.IsResolved); | ||||
| p2.Cancel(); | ||||
| Assert.IsFalse(p2.IsCancelled); | ||||
| Assert.IsFalse(p1.IsCancelled); | ||||
| Assert.IsTrue(requested); | ||||
| check1.Set(); | ||||
| try { | ||||
| p2.Join(1000); | ||||
| Assert.Fail("The chain isn't cancelled"); | ||||
| } catch(OperationCanceledException){ | ||||
| } | ||||
| Assert.IsTrue(p1.IsCancelled); | ||||
| Assert.IsTrue(p2.IsCancelled); | ||||
| } | ||||
| [TestMethod] | ||||
| public void CancellableAsyncTask() { | ||||
| var finish = new Signal(); | ||||
| var started = new Signal(); | ||||
| var p = AsyncPool.RunThread(token => { | ||||
| token.CancellationRequested(r => finish.Set()); | ||||
| started.Set(); | ||||
| finish.Wait(); | ||||
| Assert.IsTrue(token.CancelOperationIfRequested()); | ||||
| }); | ||||
| started.Wait(1000); | ||||
| Assert.IsFalse(p.IsResolved); | ||||
| p.Cancel(); | ||||
| try { | ||||
| p.Join(1000); | ||||
| } catch (OperationCanceledException) { | ||||
| } | ||||
| } | ||||
| } | ||||
| } | ||||
