Пример #1
0
        public void ThrowsIfCompletedTwice()
        {
            var f = new Future <object>();

            try {
                f.Complete(5);
                f.Complete(10);
                Assert.Fail();
            } catch (InvalidOperationException) {
            }
        }
Пример #2
0
        public void CanCompleteFuture()
        {
            var f = new Future <object>();

            f.Complete();
            Assert.IsTrue(f.Completed);
        }
Пример #3
0
        public void CanCompleteWithNull()
        {
            var f = new Future <object>();

            f.Complete();
            Assert.AreEqual(null, f.Result);
        }
Пример #4
0
        public void FailedIsFalseIfFutureHasValue()
        {
            var f = new Future <object>();

            f.Complete(5);
            Assert.IsFalse(f.Failed);
        }
Пример #5
0
        public void CanGetResult()
        {
            var f = new Future <object>();

            f.Complete(5);
            Assert.AreEqual(5, f.Result);
        }
Пример #6
0
        public void InvokesOnCompletesWhenCompleted()
        {
            var    f = new Future <object>();
            object completeResult = null;

            f.RegisterOnComplete((_) => { completeResult = _.Error ?? _.Result; });
            f.Complete(5);
            Assert.AreEqual(5, completeResult);
        }
Пример #7
0
        public void IfCompletedDisposeHasNoEffect()
        {
            var f = new Future <object>();

            f.Complete(5);
            f.Dispose();
            Assert.AreEqual(5, f.Result);
            Assert.IsFalse(f.Disposed);
        }
Пример #8
0
        public void CanBindFutureToOtherFuture()
        {
            var a = new Future <object>();
            var b = new Future <object>();

            b.Bind(a);
            a.Complete(5);
            Assert.AreEqual(5, b.Result);
        }
Пример #9
0
        public void IfOnCompleteRegisteredAfterAlreadyCompletedCalledAnyway()
        {
            var    f = new Future <object>();
            object completeResult = null;

            f.Complete(5);
            f.RegisterOnComplete((_) => { completeResult = _.Error ?? _.Result; });
            Assert.AreEqual(5, completeResult);
        }
Пример #10
0
        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);
        }
Пример #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);
        }
Пример #12
0
        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);
            }
        }
Пример #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);
        }
Пример #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);
        }
Пример #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);
        }
Пример #16
0
        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);
        }
Пример #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);
                }
            }
Пример #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);
        }
Пример #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);
        }
Пример #20
0
 public void CompleteFuture(Future <T> future)
 {
     future.Complete(_Value);
 }
Пример #21
0
 public override void Invoke()
 {
     Future.Complete(WorkItem());
 }