コード例 #1
0
        public void CancellationAbortsPromise()
        {
            var test = new TestDef();

            var cancel = TimeSpan.MinValue;
            var result = false;

            test.AddScript("m:m", async env => {
                var promise = new SimFuture <bool>(5000, env.Token);
                try {
                    result = await promise.Task;
                } catch (TaskCanceledException) {
                    cancel = env.Time;
                }
            });

            test.Run(async plan => {
                plan.StartServices();
                await plan.Delay(1000.Sec());
                await plan.StopServices(grace: 1.Sec());
            });


            Assert.IsFalse(result);
            Assert.AreEqual(TimeSpan.FromMilliseconds(1000), cancel, nameof(cancel));
        }
コード例 #2
0
 void HandOut(SimPacket msg)
 {
     _ackNumber = msg.NextSeqNumber;
     if (_pendingRead != null)
     {
         _pendingRead.SetResult(msg);
         _pendingRead = null;
     }
     else
     {
         _readBuffer.Enqueue(msg);
     }
 }
コード例 #3
0
        void AddEstablishedConnection(SimConn conn)
        {
            var ready = new ClientConn(conn);

            if (_poll != null)
            {
                _poll.SetResult(ready);
                _poll = null;
            }
            else
            {
                _incoming.Enqueue(ready);
            }
        }
コード例 #4
0
        public Task <IConn> Accept()
        {
            if (_incoming.TryDequeue(out var conn))
            {
                return(Task.FromResult <IConn>(conn));
            }

            if (_poll != null)
            {
                throw new IOException("There is a wait already");
            }

            _poll = _proc.Promise <IConn>(Timeout.InfiniteTimeSpan, _proc.Token);
            return(_poll.Task);
        }
コード例 #5
0
        public async Task <SimPacket> Read(TimeSpan timeout)
        {
            if (_closed)
            {
                throw new IOException("Socket closed");
            }

            if (_readBuffer.TryDequeue(out var tuple))
            {
                if (tuple.Flag == SimFlag.Reset)
                {
                    Close("RESET");
                    throw new IOException("Connection reset");
                }

                if (NextIsFin())
                {
                    Close("FIN");
                }

                return(tuple);
            }

            _pendingRead = _proc.Promise <SimPacket>(timeout, _proc.Token);



            var msg = await _pendingRead.Task;

            if (msg.Flag == SimFlag.Reset)
            {
                Close("RESET");
                throw new IOException("Connection reset");
            }

            if (NextIsFin())
            {
                Close("FIN");
            }

            return(msg);
        }
コード例 #6
0
        public void SettingResultSyncCompletesPromise()
        {
            var test = new TestDef()
            {
                MaxSteps = 100,
            };

            var  completed = TimeSpan.MinValue;
            bool result    = false;

            test.RunScript(async env => {
                var promise = new SimFuture <bool>(5000);
                promise.SetResult(true);
                result    = await promise.Task;
                completed = env.Time;
            });

            Assert.IsTrue(result);
            Assert.AreEqual(TimeSpan.Zero, completed);
        }
コード例 #7
0
        public void SettingErrorSyncCompletesPromise()
        {
            var test = new TestDef();

            var failed = TimeSpan.MinValue;
            var result = false;

            test.RunScript(async env => {
                var promise = new SimFuture <bool>(5000);
                promise.SetError(new IOException());
                try {
                    result = await promise.Task;
                } catch (Exception) {
                    failed = env.Time;
                }
            });


            Assert.IsFalse(result);
            Assert.AreEqual(TimeSpan.Zero, failed);
        }
コード例 #8
0
        public void CompletionSourceTimesOut()
        {
            var test = new TestDef()
            {
                MaxSteps = 100,
            };

            TimeSpan timedOut;

            test.RunScript(async env => {
                var promise = new SimFuture <bool>(5000);
                try {
                    await promise.Task;
                } catch (TimeoutException) {
                    timedOut = env.Time;
                }
            });


            Assert.AreEqual(TimeSpan.FromSeconds(5), timedOut);
        }
コード例 #9
0
        public SimFutureTask(TimeSpan ts,
                             CancellationToken token,
                             SimFuture <T> source) : base(() => {
            if (token.IsCancellationRequested)
            {
                throw new TaskCanceledException();
            }

            if (!source.Completed)
            {
                throw new TimeoutException($"Promise timed out after {Moment.Print(ts)}");
            }

            if (source.Error != null)
            {
                throw source.Error;
            }

            return(source.Result);
        }) {
            Deadline = ts;
            _token   = token;
            _source  = source;
        }