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)); }
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 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); }
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); }
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); } }
static void Main(string[] args) { TestThread test_thread; int id; // Set process timers granularity to 1ms TimeBeginPeriod(1); // Initialize log Log.Instance.Init("./", "log", Append); // Set log level to init value (INFO_LEVEL + DEBUG_LEVEL + CATCH_LEVEL) Log.Instance.Level = 7; // Enable log to file Log.Instance.ToFile = true; // Write some init log Log.Instance.Info("Main: started"); Log.Instance.Debug("Main: Wait 100 ms and start asynch test thread"); Thread.Sleep(100); // Create test thread that will write some async log test_thread = new TestThread(); // Write some debug log for (id = 0; id < 20; id++) { Log.Instance.Debug("Main: write some debug [" + id + "]"); Thread.Sleep(10); } Log.Instance.Info("Main: let's try to log a catch exception"); try { Log.Instance.Info("Main: creating a null string and trying to access to it"); string str = null; str = str.Substring(0); Log.Instance.Error("Main: if you see this line in log, something gone wrong"); } catch (Exception ex) { Log.Instance.Catch("Main: " + ex.Message); } // Wait test thread end test_thread.Join(); // Let's disable now log to file Log.Instance.Info("Main: Let's disable now log to file"); // ToFile is ASYNCRONOUS!! Thread.Sleep(10); Log.Instance.ToFile = false; Thread.Sleep(10); Log.Instance.Error("Main: this message will not appear in log file"); Thread.Sleep(10); // Let's enable now log to file Log.Instance.ToFile = true; // ToFile is ASYNCRONOUS!! Thread.Sleep(10); Log.Instance.Info("Main: Let's enable again log to file"); // ToFile is ASYNCRONOUS!! Thread.Sleep(10); Log.Instance.Info("Main: end"); // Close log Log.Instance.End(); Console.WriteLine("... Press any key to close window ..."); Console.ReadKey(); }
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 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 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(); } }