ln.types/test/PromiseTests.cs

114 lines
2.9 KiB
C#

// /**
// * 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<int>((resolve, reject) =>
{
resolve(2);
})
.Then<double>(
(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<int>[] promises = new Promise<int>[5];
for (int n = 0; n < promises.Length; n++)
{
promises[n] = new Promise<int>((resolve, reject) =>
{
ThreadPool.QueueUserWorkItem((state) =>
{
Thread.Sleep(100 * n);
resolve(n);
});
});
}
lock (promises)
{
Promise<int>
.All(promises)
.Then((value) =>
{
lock(promises)
Monitor.PulseAll(promises);
});
if (!Monitor.Wait(promises, 1500))
throw new TimeoutException();
}
}
[Test()]
public void PromiseRace()
{
Promise<int>[] promises = new Promise<int>[5];
for (int n = 0; n < promises.Length; n++)
{
promises[n] = new Promise<int>((resolve, reject) =>
{
ThreadPool.QueueUserWorkItem((state) =>
{
Thread.Sleep(100 * n);
resolve(n);
});
});
}
lock (promises)
{
Promise<int>
.Race(promises)
.Then((value) =>
{
lock (promises)
Monitor.PulseAll(promises);
});
if (!Monitor.Wait(promises, 1500))
throw new TimeoutException();
}
}
}
}