// /** // * File: PromiseTests.cs // * Author: haraldwolff // * // * This file and it's content is copyrighted by the Author and / or copyright holder. // * Any use wihtout proper permission is illegal and may lead to legal actions. // * // * // **/ using NUnit.Framework; using System; using System.Threading; namespace ln.types.test { [TestFixture()] public class PromiseTests { [Test()] public void TestCase() { object l = new object(); lock (l) { new Promise((resolve, reject) => { resolve(2); }) .Then( (value, resolve, reject) => { resolve(value); }) .Then((value) => { Console.WriteLine("Double Value: {0}",value); }) .Finally(() => { lock (l) { Monitor.PulseAll(l); } }); if (!Monitor.Wait(l, 500)) throw new TimeoutException(); } } [Test()] public void PromiseAll() { Promise[] promises = new Promise[5]; for (int n = 0; n < promises.Length; n++) { promises[n] = new Promise((resolve, reject) => { ThreadPool.QueueUserWorkItem((state) => { Thread.Sleep(100 * n); resolve(n); }); }); } lock (promises) { Promise .All(promises) .Then((value) => { lock(promises) Monitor.PulseAll(promises); }); if (!Monitor.Wait(promises, 1500)) throw new TimeoutException(); } } [Test()] public void PromiseRace() { Promise[] promises = new Promise[5]; for (int n = 0; n < promises.Length; n++) { promises[n] = new Promise((resolve, reject) => { ThreadPool.QueueUserWorkItem((state) => { Thread.Sleep(100 * n); resolve(n); }); }); } lock (promises) { Promise .Race(promises) .Then((value) => { lock (promises) Monitor.PulseAll(promises); }); if (!Monitor.Wait(promises, 1500)) throw new TimeoutException(); } } } }