public void TimerExpiry_Basic() { TimerExpiry te = new TimerExpiry(200); int t = te.RemainingTime(); Assert.That(t, Is.GreaterThan(140), "Timer is less than 140ms (should be close to 200ms), remaining={0}", t); int[] rea = new int[1000]; int c = 0; // Note, you should put your delays in loops like this one, as the OS doesn't guarantee that you will // actually wait this long. Normally, this kind of loop is exactly the type you want, as you wait for // another event to occur, and if it doesn't occur, you keep waiting until the timeout is zero. // // On some systems, a signal may cause a timeout to abort early also. int re = te.RemainingTime(); do { rea[c++] = re; Thread.Sleep(re); re = te.RemainingTime(); } while (re > 0); Assert.That(te.RemainingTime(), Is.EqualTo(0)); for (int i = 0; i < c; i++) { Console.WriteLine("Wait {0}: {1}", i, rea[i]); } }
public void TimerExpiry_Reset() { TimerExpiry te = new TimerExpiry(1000); System.Threading.Thread.Sleep(350); Assert.IsTrue(te.RemainingTime() > 500); te.Reset(); System.Threading.Thread.Sleep(350); Assert.IsTrue(te.RemainingTime() > 500); te.Reset(); System.Threading.Thread.Sleep(350); Assert.IsTrue(te.RemainingTime() > 500); te.Reset(); System.Threading.Thread.Sleep(350); Assert.IsTrue(te.RemainingTime() > 500); te.Reset(); System.Threading.Thread.Sleep(350); Assert.IsTrue(te.RemainingTime() > 500); te.Reset(); System.Threading.Thread.Sleep(350); Assert.IsTrue(te.RemainingTime() > 500); }
public void TimerExpiry_Basic() { TimerExpiry te = new TimerExpiry(200); int t = te.RemainingTime(); Assert.IsTrue(t > 140, "Timer is less than 140ms (should be close to 200ms), remaining=" + t.ToString()); int[] rea = new int[1000]; int c = 0; // Note, you should put your delays in loops like this one, as // the OS doesn't guarantee that you will actually wait this // long. Normally, this kind of loop is exactly the type you // want, as you wait for another event to occur, and if it // doesn't occur, you keep waiting until the timeout is zero. // // On some systems, a signal may cause a timeout to abort // early also. int re = te.RemainingTime(); do { rea[c++] = re; System.Threading.Thread.Sleep((int)re); re = te.RemainingTime(); } while (re > 0); Assert.AreEqual(0, te.RemainingTime()); for (int i = 0; i < c; i++) { System.Diagnostics.Trace.WriteLine("Wait " + i.ToString() + ": " + rea[i].ToString()); } }
public void TimerExpiry_Negative() { TimerExpiry te = new TimerExpiry(-1); Assert.AreEqual(-1, te.RemainingTime()); System.Threading.Thread.Sleep(100); Assert.AreEqual(-1, te.RemainingTime()); TimerExpiry te2 = new TimerExpiry(100); Assert.IsTrue(te2.RemainingTime() > 0); te2.Timeout = -1; Assert.AreEqual(-1, te.RemainingTime()); System.Threading.Thread.Sleep(400); Assert.AreEqual(-1, te.RemainingTime()); }
public void TimerExpiry_Reset2() { TimerExpiry te = new TimerExpiry(1000); Thread.Sleep(350); Assert.That(te.RemainingTime(), Is.GreaterThan(500)); Thread.Sleep(350); Assert.That(te.RemainingTime(), Is.LessThan(500)); te.Reset(); Thread.Sleep(350); Assert.That(te.RemainingTime(), Is.GreaterThan(500)); Thread.Sleep(350); Assert.That(te.RemainingTime(), Is.LessThan(500)); }
public void TimerExpiry_Reset2() { TimerExpiry te = new TimerExpiry(1000); System.Threading.Thread.Sleep(350); Assert.IsTrue(te.RemainingTime() > 500); System.Threading.Thread.Sleep(350); Assert.IsTrue(te.RemainingTime() < 500); te.Reset(); System.Threading.Thread.Sleep(350); Assert.IsTrue(te.RemainingTime() > 500); System.Threading.Thread.Sleep(350); Assert.IsTrue(te.RemainingTime() < 500); }
public void TimerExpiry_Negative() { TimerExpiry te = new TimerExpiry(-1); Assert.That(te.RemainingTime(), Is.EqualTo(Timeout.Infinite)); Thread.Sleep(100); Assert.That(te.RemainingTime(), Is.EqualTo(Timeout.Infinite)); TimerExpiry te2 = new TimerExpiry(100); Assert.That(te2.RemainingTime() > 0, Is.True); te2.Timeout = -1; Assert.That(te.RemainingTime(), Is.EqualTo(Timeout.Infinite)); Thread.Sleep(400); Assert.That(te.RemainingTime(), Is.EqualTo(Timeout.Infinite)); }
public void TimerExpiry_Zero() { TimerExpiry te = new TimerExpiry(0); Assert.That(te.Expired, Is.True); Assert.That(te.RemainingTime(), Is.EqualTo(0)); }
public void TimerExpiry_Zero() { TimerExpiry te = new TimerExpiry(0); Assert.IsTrue(te.Expired); Assert.AreEqual(0, te.RemainingTime()); }
bool ISerialBufferStreamData.WaitForWrite(int count, int timeout) { if (count == 0) { return(true); } lock (m_WriteLock) { if (count > m_WriteBuffer.Capacity) { return(false); } } m_AbortWriteEvent.Reset(); TimerExpiry timer = new TimerExpiry(timeout); do { lock (m_WriteLock) { if (m_WriteBuffer.Free >= count) { return(true); } m_WriteBufferNotFullEvent.Reset(); } // This manual reset event is always set every time data is removed from the buffer WaitHandle[] handles = new WaitHandle[] { m_DeviceDead, m_AbortWriteEvent, m_WriteBufferNotFullEvent }; int triggered = WaitHandle.WaitAny(handles, timer.RemainingTime()); switch (triggered) { case WaitHandle.WaitTimeout: break; case 0: // The internal thread died. return(false); case 1: // Someone aborted the wait. return(false); case 2: // Data is available to write. return(true); } } while (!timer.Expired); return(false); }
bool ISerialBufferStreamData.WaitForRead(int count, int timeout) { if (count == 0) { return(true); } lock (m_ReadLock) { if (count > m_ReadBuffer.Capacity) { return(false); } } m_AbortReadEvent.Reset(); TimerExpiry timer = new TimerExpiry(timeout); do { lock (m_ReadLock) { if (m_ReadBuffer.Length >= count) { return(true); } m_ReadEvent.Reset(); } WaitHandle[] handles = new WaitHandle[] { m_AbortReadEvent, m_ReadEvent, m_DeviceDead, }; int triggered = WaitHandle.WaitAny(handles, timer.RemainingTime()); switch (triggered) { case WaitHandle.WaitTimeout: break; case 0: // Someone aborted the wait. return(false); case 1: // Data is available to read. return(true); case 2: // Monitoring thread died. No point waiting any longer. return(false); } } while (!timer.Expired); return(false); }
bool ISerialBufferStreamData.WaitForWrite(int count, int timeout) { if (count == 0) { return(true); } lock (m_WriteLock) { if (count > m_WriteBuffer.Capacity) { return(false); } } m_AbortWriteEvent.Reset(); TimerExpiry timer = new TimerExpiry(timeout); do { lock (m_WriteLock) { if (m_WriteBuffer.Free >= count) { return(true); } m_WriteBufferNotFullEvent.Reset(); } int triggered = WaitHandle.WaitAny(m_BufferStreamWaitForWriteCountHandles, timer.RemainingTime()); switch (triggered) { case WaitHandle.WaitTimeout: break; case 0: // The internal thread died. return(false); case 1: // Someone aborted the wait. return(false); case 2: // Data is available to write. Loop to the beginning to see if there is now enough data to write. break; } } while (!timer.Expired); return(false); }