public void ThrowsIfCompletedTwice()
        {
            var f = new Future <object>();

            try {
                f.Complete(5);
                f.Complete(10);
                Assert.Fail();
            } catch (InvalidOperationException) {
            }
        }
        public void CanCompleteFuture()
        {
            var f = new Future <object>();

            f.Complete();
            Assert.IsTrue(f.Completed);
        }
        public void CanCompleteWithNull()
        {
            var f = new Future <object>();

            f.Complete();
            Assert.AreEqual(null, f.Result);
        }
        public void FailedIsFalseIfFutureHasValue()
        {
            var f = new Future <object>();

            f.Complete(5);
            Assert.IsFalse(f.Failed);
        }
        public void CanGetResult()
        {
            var f = new Future <object>();

            f.Complete(5);
            Assert.AreEqual(5, f.Result);
        }
        public void InvokesOnCompletesWhenCompleted()
        {
            var    f = new Future <object>();
            object completeResult = null;

            f.RegisterOnComplete((_) => { completeResult = _.Error ?? _.Result; });
            f.Complete(5);
            Assert.AreEqual(5, completeResult);
        }
        public void IfCompletedDisposeHasNoEffect()
        {
            var f = new Future <object>();

            f.Complete(5);
            f.Dispose();
            Assert.AreEqual(5, f.Result);
            Assert.IsFalse(f.Disposed);
        }
        public void CanBindFutureToOtherFuture()
        {
            var a = new Future <object>();
            var b = new Future <object>();

            b.Bind(a);
            a.Complete(5);
            Assert.AreEqual(5, b.Result);
        }
        public void IfOnCompleteRegisteredAfterAlreadyCompletedCalledAnyway()
        {
            var    f = new Future <object>();
            object completeResult = null;

            f.Complete(5);
            f.RegisterOnComplete((_) => { completeResult = _.Error ?? _.Result; });
            Assert.AreEqual(5, completeResult);
        }
        public void CannotBeCompletedIfDisposedFirst()
        {
            var f = new Future <object>();

            f.Dispose();
            Assert.IsTrue(f.Disposed);
            f.Complete(5);
            Assert.IsTrue(f.Disposed);
            Assert.IsFalse(f.Completed);
        }
Example #11
0
        public static Future <HttpListenerContext> GetContextAsync(this HttpListener listener)
        {
            var f = new Future <HttpListenerContext>();

            listener.BeginGetContext((ar) => {
                try {
                    var result = listener.EndGetContext(ar);
                    f.Complete(result);
                } catch (FutureHandlerException) {
                    throw;
                } catch (Exception ex) {
                    f.Fail(ex);
                }
            }, null);
            return(f);
        }
        public void FutureWrapsExceptionIfOnCompleteHandlerThrows()
        {
            var f = new Future <object>();

            f.RegisterOnComplete((_) => {
                throw new Exception("pancakes");
            });

            try {
                f.Complete(1);
                Assert.Fail("Exception was swallowed");
            } catch (FutureHandlerException fhe) {
                Assert.IsInstanceOfType(typeof(Exception), fhe.InnerException);
                Assert.AreEqual("pancakes", fhe.InnerException.Message);
            }
        }
Example #13
0
        public Future <T> Dequeue()
        {
            var f = new Future <T>();

            lock (_Lock) {
                if (_Queue.Count > 0)
                {
                    f.Complete(_Queue.Dequeue());
                }
                else
                {
                    _WaitingFutures.Enqueue(f);
                }
            }
            return(f);
        }
Example #14
0
        public static Future <TcpClient> AcceptIncomingConnection(this TcpListener listener)
        {
            var f = new Future <TcpClient>();

            listener.BeginAcceptTcpClient((ar) => {
                try {
                    TcpClient result = listener.EndAcceptTcpClient(ar);
                    f.Complete(result);
                } catch (FutureHandlerException) {
                    throw;
                } catch (Exception ex) {
                    f.Fail(ex);
                }
            }, null);
            return(f);
        }
Example #15
0
        public static Future <TcpClient> ConnectTo(IPAddress address, int port)
        {
            var       f      = new Future <TcpClient>();
            TcpClient client = new TcpClient();

            client.BeginConnect(address, port, (ar) => {
                try {
                    client.EndConnect(ar);
                    f.Complete(client);
                } catch (FutureHandlerException) {
                    throw;
                } catch (Exception ex) {
                    f.Fail(ex);
                    client.Close();
                }
            }, null);
            return(f);
        }
        public void BindToProperty()
        {
            var tc = new TestClass();
            var ts = new TestStruct();

            var f = new Future <int>();

            f.Bind(() => tc.Property);

            try {
                f.Bind(() => ts.Property);
                Assert.Fail("Did not throw InvalidOperationException");
            } catch (InvalidOperationException) {
            }

            f.Complete(5);

            Assert.AreEqual(5, tc.Property);
            Assert.AreNotEqual(5, ts.Property);
        }
Example #17
0
            public void OnComplete(IFuture f)
            {
                bool completed = false;

                lock (State) {
                    if (State.Count == Trigger)
                    {
                        completed = true;
                        State.Clear();
                    }
                    else
                    {
                        State.Remove(f);
                    }
                }

                if (completed)
                {
                    Composite.Complete(f);
                }
            }
Example #18
0
        public static Future <Network.UdpPacket> AsyncReceive(this UdpClient udpClient)
        {
            var f = new Future <Network.UdpPacket>();

            try {
                udpClient.BeginReceive((ar) => {
                    IPEndPoint endpoint = default(IPEndPoint);
                    try {
                        var bytes = udpClient.EndReceive(ar, ref endpoint);
                        f.Complete(new Network.UdpPacket(bytes, endpoint));
                    } catch (FutureHandlerException) {
                        throw;
                    } catch (Exception ex) {
                        f.Fail(ex);
                    }
                }, null);
            } catch (Exception ex) {
                f.Fail(ex);
            }
            return(f);
        }
Example #19
0
        public static Future <int> AsyncSend(this UdpClient udpClient, byte[] datagram, int bytes, string hostname, int port)
        {
            var f = new Future <int>();

            try {
                udpClient.BeginSend(
                    datagram, bytes, hostname, port,
                    (ar) => {
                    try {
                        var bytesSent = udpClient.EndSend(ar);
                        f.Complete(bytesSent);
                    } catch (FutureHandlerException) {
                        throw;
                    } catch (Exception ex) {
                        f.Fail(ex);
                    }
                },
                    null
                    );
            } catch (Exception ex) {
                f.Fail(ex);
            }
            return(f);
        }
Example #20
0
 public void CompleteFuture(Future <T> future)
 {
     future.Complete(_Value);
 }
Example #21
0
 public override void Invoke()
 {
     Future.Complete(WorkItem());
 }