view Implab.Test/AsyncTests.cs @ 11:6ec82bf68c8e promises

refactoring
author cin
date Tue, 05 Nov 2013 01:09:58 +0400
parents aa33d0bb8c0c
children b0feb5b9ad1c
line wrap: on
line source

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
using System.Threading;
using Implab.Parallels;

namespace Implab.Test
{
	[TestClass]
	public class AsyncTests
	{
		[TestMethod]
		public void ResolveTest ()
		{
			int res = -1;
			var p = new Promise<int> ();
			p.Then (x => res = x);
			p.Resolve (100);

			Assert.AreEqual (res, 100);
		}

        [TestMethod]
		public void RejectTest ()
		{
			int res = -1;
			Exception err = null;

			var p = new Promise<int> ();
			p.Then (x => res = x, e => err = e);
			p.Reject (new ApplicationException ("error"));

			Assert.AreEqual (res, -1);
			Assert.AreEqual (err.Message, "error");

		}

        [TestMethod]
		public void JoinSuccessTest ()
		{
			var p = new Promise<int> ();
			p.Resolve (100);
			Assert.AreEqual (p.Join (), 100);
		}

        [TestMethod]
		public void JoinFailTest ()
		{
			var p = new Promise<int> ();
			p.Reject (new ApplicationException ("failed"));

			try {
				p.Join ();
				throw new ApplicationException ("WRONG!");
			} catch (TargetInvocationException err) {
				Assert.AreEqual (err.InnerException.Message, "failed");
			} catch {
				Assert.Fail ("Got wrong excaption");
			}
		}

        [TestMethod]
		public void MapTest ()
		{
			var p = new Promise<int> ();

			var p2 = p.Map (x => x.ToString ());
			p.Resolve (100);

			Assert.AreEqual (p2.Join (), "100");
		}

        [TestMethod]
        public void FixErrorTest() {
            var p = new Promise<int>();

            var p2 = p.Error(e => 101);

            p.Reject(new Exception());

            Assert.AreEqual(p2.Join(), 101);
        }

        [TestMethod]
		public void ChainTest ()
		{
			var p1 = new Promise<int> ();

			var p3 = p1.Chain (x => {
				var p2 = new Promise<string> ();
				p2.Resolve (x.ToString ());
				return p2;
			});

			p1.Resolve (100);

			Assert.AreEqual (p3.Join (), "100");
		}

        [TestMethod]
		public void PoolTest ()
		{
			var pid = Thread.CurrentThread.ManagedThreadId;
			var p = AsyncPool.Invoke (() => Thread.CurrentThread.ManagedThreadId);

			Assert.AreNotEqual (pid, p.Join ());
		}

        [TestMethod]
        public void ComplexCase1Test() {
            var flags = new bool[3];

            // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map)

            var p = PromiseHelper
                .Sleep(200, "Alan")
                .Cancelled(() => flags[0] = true)
                .Chain(x =>
                    PromiseHelper
                        .Sleep(200, "Hi, " + x)
                        .Map( y => y )
                        .Cancelled(() => flags[1] = true)
                )
                .Cancelled(() => flags[2] = true);
            Thread.Sleep(300);
            p.Cancel();
            try {
                Assert.AreEqual(p.Join(), "Hi, Alan");
                Assert.Fail("Shouldn't get here");
            } catch(OperationCanceledException) {
            }

            Assert.IsFalse(flags[0]);
            Assert.IsTrue(flags[1]);
            Assert.IsTrue(flags[2]);
        }
	}
}