Пример #1
0
        public void TestWriteFuture()
        {
            DefaultWriteFuture future = new DefaultWriteFuture(null);

            Assert.IsFalse(future.Done);
            Assert.IsFalse(future.Written);

            TestThread thread = new TestThread(future);

            thread.Start();

            future.Written = true;
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsTrue(future.Written);

            future = new DefaultWriteFuture(null);
            thread = new TestThread(future);
            thread.Start();

            future.Exception = new Exception();
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsFalse(future.Written);
            Assert.IsTrue(future.Exception.GetType() == typeof(Exception));
        }
Пример #2
0
        public void TestAddListener()
        {
            DefaultCloseFuture future = new DefaultCloseFuture(null);

            Assert.IsFalse(future.Done);
            Assert.IsFalse(future.Closed);

            IoFuture f1 = null, f2 = null;

            future.Complete += (s, e) => f1 = e.Future;
            future.Complete += (s, e) => f2 = e.Future;

            TestThread thread = new TestThread(future);

            thread.Start();

            future.Closed = true;
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsTrue(future.Closed);

            Assert.AreSame(future, f1);
            Assert.AreSame(future, f2);
        }
Пример #3
0
        public void TestRemoveListener2()
        {
            DefaultCloseFuture future = new DefaultCloseFuture(null);

            Assert.IsFalse(future.Done);
            Assert.IsFalse(future.Closed);

            IoFuture f1 = null, f2 = null;
            EventHandler <IoFutureEventArgs> listener1 = (s, e) => f1 = e.Future;
            EventHandler <IoFutureEventArgs> listener2 = (s, e) => f2 = e.Future;

            future.Complete += listener1;
            future.Complete += listener2;
            future.Complete -= listener2;

            TestThread thread = new TestThread(future);

            thread.Start();

            future.Closed = true;
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsTrue(future.Closed);

            Assert.AreSame(future, f1);
            Assert.AreSame(null, f2);
        }
Пример #4
0
 public void testStaticAction1()
 {
     var testThread = new TestThread();
     testThread.Start();
     Thread.Sleep(1000);
     testThread.Quit();
     AssertNull("Error should be null", testThread.error);
     AssertEquals("Result should be OK", "OK", testThread.result);
 }
Пример #5
0
        public void testStaticAction1()
        {
            var testThread = new TestThread();

            testThread.Start();
            Thread.Sleep(1000);
            testThread.Quit();
            AssertNull("Error should be null", testThread.error);
            AssertEquals("Result should be OK", "OK", testThread.result);
        }
        // ReSharper disable once UnusedParameter.Local
        private TestThread CreateInsertThread(int mainCounter, ManualResetEvent signal)
        {
            var tt = TestThread.Start(th =>
            {
                for (int i = 0; i < 10; i++)
                {
                    _dict.TryAdd("" + i, "" + i);
                    _added++;
                }
                return(true);
            }, signal);

            return(tt);
        }
        // ReSharper disable once UnusedParameter.Local
        private TestThread CreateRemoveThread(int mainCounter, ManualResetEvent signal)
        {
            var tt = TestThread.Start(th =>
            {
                for (int i = 0; i < 10; i++)
                {
                    _dict.Remove("" + i);
                    _removed++;
                }
                return(true);
            }, signal);

            return(tt);
        }
        // ReSharper disable once UnusedParameter.Local
        private TestThread CreateReadThread(int mainCounter, ManualResetEvent signal)
        {
            var tt = TestThread.Start(th =>
            {
                var enumerator = _dict.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var result = enumerator.Current;
                    Assert.IsNotNull(result.Key);
                    _read++;
                }
                return(true);
            }, signal);

            return(tt);
        }
Пример #9
0
        public void TestCloseFuture()
        {
            DefaultCloseFuture future = new DefaultCloseFuture(null);
            Assert.IsFalse(future.Done);
            Assert.IsFalse(future.Closed);

            TestThread thread = new TestThread(future);
            thread.Start();

            future.Closed = true;
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsTrue(future.Closed);
        }
Пример #10
0
        public void TestConnectFuture()
        {
            DefaultConnectFuture future = new DefaultConnectFuture();

            Assert.IsFalse(future.Done);
            Assert.IsFalse(future.Connected);
            Assert.IsNull(future.Session);
            Assert.IsNull(future.Exception);

            TestThread thread = new TestThread(future);

            thread.Start();

            IoSession session = new DummySession();

            future.SetSession(session);
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsTrue(future.Connected);
            Assert.AreSame(session, future.Session);
            Assert.IsNull(future.Exception);

            future = new DefaultConnectFuture();
            thread = new TestThread(future);
            thread.Start();
            future.Exception = new IOException();
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsFalse(future.Connected);
            Assert.IsTrue(future.Exception is IOException);

            try
            {
                IoSession s = future.Session;
                Assert.Fail("IOException should be thrown.");
            }
            catch (Exception)
            {
                // Signifies a successful test execution
                Assert.IsTrue(true);
            }
        }
Пример #11
0
        public void TestCloseFuture()
        {
            DefaultCloseFuture future = new DefaultCloseFuture(null);

            Assert.IsFalse(future.Done);
            Assert.IsFalse(future.Closed);

            TestThread thread = new TestThread(future);

            thread.Start();

            future.Closed = true;
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsTrue(future.Closed);
        }
Пример #12
0
        public void TestConnectFuture()
        {
            DefaultConnectFuture future = new DefaultConnectFuture();
            Assert.IsFalse(future.Done);
            Assert.IsFalse(future.Connected);
            Assert.IsNull(future.Session);
            Assert.IsNull(future.Exception);

            TestThread thread = new TestThread(future);
            thread.Start();

            IoSession session = new DummySession();

            future.SetSession(session);
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsTrue(future.Connected);
            Assert.AreSame(session, future.Session);
            Assert.IsNull(future.Exception);

            future = new DefaultConnectFuture();
            thread = new TestThread(future);
            thread.Start();
            future.Exception = new IOException();
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsFalse(future.Connected);
            Assert.IsTrue(future.Exception is IOException);

            try
            {
                IoSession s = future.Session;
                Assert.Fail("IOException should be thrown.");
            }
            catch (Exception)
            {
                // Signifies a successful test execution
                Assert.IsTrue(true);
            }
        }
        // ReSharper disable once UnusedParameter.Local
        private TestThread CreateValuesKeysThread(int mainCounter, ManualResetEvent signal)
        {
            var tt = TestThread.Start(th =>
            {
                // ReSharper disable once UnusedVariable
                foreach (var value in _dict.Values)
                {
                }
                foreach (var key in _dict.Keys)
                {
                    // ReSharper disable once UnusedVariable
                    var result = _dict.ContainsKey(key);
                    var value  = _dict[key];
                    // ReSharper disable once SpecifyACultureInStringConversionExplicitly
                    _dict[key] = new Random(100).Next(1000).ToString() + value;
                }
                return(true);
            }, signal);

            return(tt);
        }
        public void ShouldHandleBlockingAtTheEndOfGrantedInterval()
        {
            var indicator = false;

            using (var master = new MasterTimeSource {
                Quantum = TimeInterval.FromTicks(10), AdvanceImmediately = true
            })
                using (var timeSlave = new SlaveTimeSource()
                {
                    Quantum = TimeInterval.FromTicks(10), AdvanceImmediately = true
                })
                    using (var timeSink = new MoreComplicatedTimeSink("A"))
                    {
                        var testerThread = new TestThread(() =>
                        {
                            this.Trace();
                            timeSink.ExecuteOnDispatcherThread((ts, ti) =>
                            {
                                this.Trace();
                                Assert.AreEqual(10, ti.Ticks);

                                ts.TimeHandle.ReportBackAndBreak(TimeInterval.Empty);
                            });

                            this.Trace();
                            // here we sleep to make sure that master won't go further
                            Thread.Sleep(5000);

                            this.Trace();
                            Assert.AreEqual(10, master.ElapsedVirtualTime.Ticks);
                            Assert.AreEqual(10, timeSlave.ElapsedVirtualTime.Ticks);
                            indicator = true;

                            this.Trace();
                            timeSink.ExecuteOnDispatcherThread((ts, ti) =>
                            {
                                this.Trace();
                                Assert.AreEqual(0, ti.Ticks);
                                ts.TimeHandle.ReportBackAndContinue(TimeInterval.Empty);
                            });

                            timeSink.ExecuteOnDispatcherThread((ts, ti) =>
                            {
                                this.Trace();
                                Assert.AreEqual(10, ti.Ticks);
                                ts.TimeHandle.ReportBackAndContinue(TimeInterval.Empty);
                            });
                        })
                        {
                            Name = "tester thread"
                        };
                        testerThread.Start();

                        master.RegisterSink(timeSlave);
                        timeSlave.RegisterSink(timeSink);

                        // just to pass the first syncpoint
                        master.Run(1);
                        testerThread.CheckExceptions();

                        this.Trace();
                        master.Run(1);
                        testerThread.CheckExceptions();
                        this.Trace();
                        Assert.IsTrue(indicator);
                        Assert.AreEqual(10, master.ElapsedVirtualTime.Ticks);
                        Assert.AreEqual(10, timeSlave.ElapsedVirtualTime.Ticks);

                        master.Run(1);
                        Assert.AreEqual(20, master.ElapsedVirtualTime.Ticks);
                        Assert.AreEqual(20, timeSlave.ElapsedVirtualTime.Ticks);
                        testerThread.CheckExceptions();
                    }
        }
Пример #15
0
        public void TestWriteFuture()
        {
            DefaultWriteFuture future = new DefaultWriteFuture(null);
            Assert.IsFalse(future.Done);
            Assert.IsFalse(future.Written);

            TestThread thread = new TestThread(future);
            thread.Start();

            future.Written = true;
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsTrue(future.Written);

            future = new DefaultWriteFuture(null);
            thread = new TestThread(future);
            thread.Start();

            future.Exception = new Exception();
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsFalse(future.Written);
            Assert.IsTrue(future.Exception.GetType() == typeof(Exception));
        }
Пример #16
0
        public void TestRemoveListener2()
        {
            DefaultCloseFuture future = new DefaultCloseFuture(null);
            Assert.IsFalse(future.Done);
            Assert.IsFalse(future.Closed);

            IoFuture f1 = null, f2 = null;
            EventHandler<IoFutureEventArgs> listener1 = (s, e) => f1 = e.Future;
            EventHandler<IoFutureEventArgs> listener2 = (s, e) => f2 = e.Future;

            future.Complete += listener1;
            future.Complete += listener2;
            future.Complete -= listener2;

            TestThread thread = new TestThread(future);
            thread.Start();

            future.Closed = true;
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsTrue(future.Closed);

            Assert.AreSame(future, f1);
            Assert.AreSame(null, f2);
        }
        public void ShouldHandleTwoBlockingSinks()
        {
            var indicator = false;

            using (var master = new MasterTimeSource {
                Quantum = TimeInterval.FromTicks(10), AdvanceImmediately = true
            })
                using (var timeSlave = new SlaveTimeSource()
                {
                    Quantum = TimeInterval.FromTicks(10), AdvanceImmediately = true
                })
                    using (var timeSinkA2 = new MoreComplicatedTimeSink("A"))
                        using (var timeSinkB2 = new MoreComplicatedTimeSink("B"))
                        {
                            var ttt = new TestThread(() =>
                            {
                                Parallel(
                                    () =>
                                {
                                    timeSinkA2.ExecuteOnDispatcherThread((sts, ti) =>
                                    {
                                        this.Trace();
                                        Assert.AreEqual(10, ti.Ticks);
                                        var timeUsed = TimeInterval.FromTicks(4);
                                        var timeLeft = ti - timeUsed;
                                        sts.TimeHandle.ReportBackAndBreak(timeLeft);
                                    });
                                },

                                    () =>
                                {
                                    timeSinkB2.ExecuteOnDispatcherThread((sts, ti) =>
                                    {
                                        this.Trace();
                                        Assert.AreEqual(10, ti.Ticks);
                                        var timeUsed = TimeInterval.FromTicks(6);
                                        var timeLeft = ti - timeUsed;
                                        sts.TimeHandle.ReportBackAndBreak(timeLeft);
                                    });
                                }
                                    );

                                // here we sleep to make sure that master won't go further
                                this.Trace();
                                Thread.Sleep(5000);

                                this.Trace();
                                Assert.AreEqual(4, master.ElapsedVirtualTime.Ticks);
                                Assert.AreEqual(4, timeSlave.ElapsedVirtualTime.Ticks);

                                Parallel(
                                    () =>
                                {
                                    timeSinkA2.ExecuteOnDispatcherThread((sts, ti) =>
                                    {
                                        this.Trace();
                                        Assert.AreEqual(6, ti.Ticks);
                                        var timeUsed = TimeInterval.FromTicks(4);
                                        var timeLeft = ti - timeUsed;
                                        sts.TimeHandle.ReportBackAndBreak(timeLeft);
                                    });
                                },

                                    () =>
                                {
                                    timeSinkB2.ExecuteOnDispatcherThread((sts, ti) =>
                                    {
                                        this.Trace();
                                        Assert.AreEqual(4, ti.Ticks);
                                        sts.TimeHandle.ReportBackAndBreak(ti);
                                    });
                                }
                                    );

                                // here we sleep to make sure that master won't go further
                                this.Trace();
                                Thread.Sleep(5000);

                                this.Trace();
                                Assert.AreEqual(6, master.ElapsedVirtualTime.Ticks);
                                Assert.AreEqual(6, timeSlave.ElapsedVirtualTime.Ticks);

                                Parallel(
                                    () =>
                                {
                                    timeSinkA2.ExecuteOnDispatcherThread((sts, ti) =>
                                    {
                                        this.Trace();
                                        Assert.AreEqual(2, ti.Ticks);
                                        sts.TimeHandle.ReportBackAndBreak(ti);
                                    });
                                },

                                    () =>
                                {
                                    timeSinkB2.ExecuteOnDispatcherThread((sts, ti) =>
                                    {
                                        this.Trace();
                                        Assert.AreEqual(4, ti.Ticks);
                                        sts.TimeHandle.ReportBackAndContinue(TimeInterval.Empty);
                                    });
                                }
                                    );

                                // here we sleep to make sure that master won't go further
                                this.Trace();
                                Thread.Sleep(5000);

                                this.Trace();
                                Assert.AreEqual(8, master.ElapsedVirtualTime.Ticks);
                                Assert.AreEqual(8, timeSlave.ElapsedVirtualTime.Ticks);

                                Parallel(
                                    () =>
                                {
                                    timeSinkA2.ExecuteOnDispatcherThread((sts, ti) =>
                                    {
                                        this.Trace();
                                        Assert.AreEqual(2, ti.Ticks);
                                        indicator = true;
                                        sts.TimeHandle.ReportBackAndContinue(TimeInterval.Empty);
                                    });
                                },

                                    () =>
                                {
                                    timeSinkB2.ExecuteOnDispatcherThread((sts, ti) =>
                                    {
                                        this.Trace();
                                        Assert.Fail();
                                    }, false);           // do not wait for finish
                                    Thread.Sleep(10000); // wait for 10s and check if Fail() is called
                                }
                                    );
                            })
                            {
                                Name = "tester thread"
                            };
                            ttt.Start();

                            master.RegisterSink(timeSlave);
                            timeSlave.RegisterSink(timeSinkA2);
                            timeSlave.RegisterSink(timeSinkB2);

                            // just to pass the first syncpoint
                            master.Run(1);

                            master.Run(1);
                            Assert.IsTrue(indicator);

                            this.Trace();
                            Assert.AreEqual(10, master.ElapsedVirtualTime.Ticks);
                            Assert.AreEqual(10, timeSlave.ElapsedVirtualTime.Ticks);

                            ttt.Join();
                        }
        }
Пример #18
0
        public void TestLateAddListener()
        {
            DefaultCloseFuture future = new DefaultCloseFuture(null);
            Assert.IsFalse(future.Done);
            Assert.IsFalse(future.Closed);

            TestThread thread = new TestThread(future);
            thread.Start();

            future.Closed = true;
            thread.Join();

            Assert.IsTrue(thread.success);
            Assert.IsTrue(future.Done);
            Assert.IsTrue(future.Closed);

            IoFuture f1 = null;
            future.Complete += (s, e) => f1 = e.Future;
            Assert.AreSame(future, f1);
        }
        public void ShouldUpdateExecutedTimeAfterBlocking()
        {
            var indicator        = false;
            var firstTime        = true;
            var threadToSinkSync = new ManualResetEvent(false);
            var sinkToThreadSync = new ManualResetEvent(false);

            using (var master = new MasterTimeSource {
                Quantum = TimeInterval.FromTicks(10), AdvanceImmediately = true
            })
                using (var timeSlave = new SlaveTimeSource()
                {
                    Quantum = TimeInterval.FromTicks(10), AdvanceImmediately = true
                })
                {
                    var timeSink = new SimpleTimeSink(1.0, (sts, th, ti) =>
                    {
                        if (firstTime)
                        {
                            Assert.AreEqual(10, ti.Ticks);
                            firstTime    = false;
                            var timeUsed = TimeInterval.FromTicks(ti.Ticks / 2);
                            var timeLeft = ti - timeUsed;

                            sts.ElapsedVirtualTime += timeUsed;

                            th.ReportBackAndBreak(timeLeft);
                            sinkToThreadSync.Set();
                            threadToSinkSync.WaitOne();
                        }
                        else
                        {
                            Assert.AreEqual(5, ti.Ticks);
                            sts.ElapsedVirtualTime += ti;
                            th.ReportBackAndContinue(TimeInterval.Empty);
                        }

                        return(false);
                    });

                    var testerThread = new TestThread(() =>
                    {
                        // wait for the pause
                        sinkToThreadSync.WaitOne();

                        // here we sleep to make sure that master won't go further
                        Thread.Sleep(5000);

                        Assert.AreEqual(5, master.ElapsedVirtualTime.Ticks);
                        Assert.AreEqual(5, timeSlave.ElapsedVirtualTime.Ticks);
                        indicator = true;

                        threadToSinkSync.Set();
                    });
                    testerThread.Start();

                    master.RegisterSink(timeSlave);
                    timeSlave.RegisterSink(timeSink);

                    // just to pass the first syncpoint
                    master.Run(1);
                    testerThread.CheckExceptions();

                    master.Run(1);
                    Assert.IsTrue(indicator);
                    testerThread.CheckExceptions();

                    Assert.AreEqual(10, master.ElapsedVirtualTime.Ticks);
                    Assert.AreEqual(10, timeSlave.ElapsedVirtualTime.Ticks);
                    Assert.AreEqual(10, timeSink.ElapsedVirtualTime.Ticks);
                }
        }