diff Implab.Test/PromiseHelper.cs @ 249:d82909310094 v3

Implab.Test moved to xunit Complete set of PromiseHelpers (Then, Catch, Finally) Removed obsolete types ICancellable, ICancellationToken
author cin
date Wed, 31 Jan 2018 11:28:38 +0300
parents 4d9830a9bbb8
children
line wrap: on
line diff
--- a/Implab.Test/PromiseHelper.cs	Tue Jan 30 01:37:17 2018 +0300
+++ b/Implab.Test/PromiseHelper.cs	Wed Jan 31 11:28:38 2018 +0300
@@ -1,21 +1,39 @@
-using Implab.Parallels;
+using Implab;
+using System;
 using System.Threading;
 
 namespace Implab.Test {
     static class PromiseHelper {
-        public static IPromise<T> Sleep<T>(int timeout, T retVal) {
-            return AsyncPool.Invoke((ct) => {
-                ct.CancellationRequested(ct.CancelOperation);
-                Thread.Sleep(timeout);
-                return retVal;
+        public static IPromise<T> Sleep<T>(int timeout, T retVal, CancellationToken ct = default(CancellationToken)) {
+
+            Timer timer = null;
+
+            return Promise.Create<T>((d) => {
+                timer = new Timer(x => {
+                    d.Resolve(retVal);
+                }, null, timeout, Timeout.Infinite);
+
+                if(ct.CanBeCanceled)
+                    ct.Register(d.Cancel);
+
+            }).Finally(() => {
+                Safe.Dispose(timer);
             });
         }
 
-        public static IPromise Sleep(int timeout) {
-            return AsyncPool.Invoke((ct) => {
-                ct.CancellationRequested(ct.CancelOperation);
-                Thread.Sleep(timeout);
-                return 0;
+        public static IPromise Sleep(int timeout, CancellationToken ct = default(CancellationToken)) {
+            Timer timer = null;
+
+            return Promise.Create((d) => {
+                timer = new Timer(x => {
+                    d.Resolve();
+                }, null, timeout, Timeout.Infinite);
+
+                if(ct.CanBeCanceled)
+                    ct.Register(d.Cancel);
+
+            }).Finally(() => {
+                Safe.Dispose(timer);
             });
         }
     }