예제 #1
0
        public async Task Wait_is_Awaitable()
        {
            var expected = Guid.NewGuid();

            var completion = new Wait <object, Guid>();

            IWait wait = completion;

            Assert.AreEqual(Need.None, wait.Need, "at initial state");

            IFiber <object> fiber   = new Mock <IFiber <object> >(MockBehavior.Strict).Object;
            object          context = new object();

            IWait <object, Guid> typed = completion;

            typed.Wait(async(f, c, item) => {
                Assert.AreEqual(Need.Call, wait.Need, "inside callback state");

                Assert.AreEqual(fiber, f);
                Assert.AreEqual(context, c);

                Assert.AreEqual(expected, item.GetAwaiter().GetResult());
                Assert.AreEqual(expected, await item);
                return(null);
            });
            Assert.AreEqual(Need.Wait, wait.Need, "waiting state");

            IPost <Guid> post = completion;

            post.Post(expected);
            Assert.AreEqual(Need.Poll, wait.Need, "need to poll state");

            await typed.PollAsync(fiber, context);

            Assert.AreEqual(Need.Done, wait.Need, "done state");

            IAwaitable <Guid> awaitable = completion;
            var actual = await awaitable;

            Assert.AreEqual(expected, actual);
        }
예제 #2
0
        public static async Task Wait_is_Awaitable <ItemType, PostType>(PostType expected)
        {
            var completion = new Wait <object, ItemType>();

            IWait wait = completion;

            Assert.AreEqual(Need.None, wait.Need, "at initial state");

            IFiber <object> fiber   = new Mock <IFiber <object> >(MockBehavior.Strict).Object;
            object          context = new object();

            IWait <object, ItemType> typed = completion;

            typed.Wait(async(f, c, item, token) =>
            {
                Assert.AreEqual(Need.Call, wait.Need, "inside callback state");

                Assert.AreEqual(fiber, f);
                Assert.AreEqual(context, c);

                Assert.AreEqual(expected, item.GetAwaiter().GetResult());
                Assert.AreEqual(expected, await item);
                return(null);
            });
            Assert.AreEqual(Need.Wait, wait.Need, "waiting state");

            wait.Post(expected);
            Assert.AreEqual(Need.Poll, wait.Need, "need to poll state");

            await typed.PollAsync(fiber, context, CancellationToken.None);

            Assert.AreEqual(Need.Done, wait.Need, "done state");

            IAwaitable <ItemType> awaitable = completion;
            var actual = await awaitable;

            Assert.AreEqual(expected, actual);
        }