public void TakeWaitTest() { var queue = new TimeoutNotifyQueue <int>(10); queue.Offer(1, 1, 1); var watch = Stopwatch.StartNew(); queue.Take(); watch.Stop(); var elapsed = Convert.ToInt32(watch.Elapsed.TotalMilliseconds) / 1000; Assert.IsTrue(elapsed >= 0); Assert.IsTrue(elapsed <= 2); var are = new AutoResetEvent(false); new Task(() => { watch = Stopwatch.StartNew(); var i = queue.Take(); Assert.AreEqual(10, i.Key); watch.Stop(); elapsed = Convert.ToInt32(watch.Elapsed.TotalMilliseconds) / 1000; Assert.IsTrue(elapsed >= 1); Assert.IsTrue(elapsed <= 3); are.Set(); }).Start(); //total 3 secs System.Threading.Thread.Sleep(2000); queue.Offer(10, 10, 1); are.WaitOne(); }
public void OfferCauseSequenceChangedTest() { var queue = new TimeoutNotifyQueue <int>(10); queue.Offer(2, 2, 3); new Task(() => { queue.Offer(1, 1, 1); }).Start(); var node = queue.Take(); Assert.AreEqual(1, node.Key); node = queue.Take(); Assert.AreEqual(2, node.Key); }
public void RemoveCauseSequenceChangedTest() { var queue = new TimeoutNotifyQueue <int>(10); queue.Offer(2, 2, 5); queue.Offer(3, 3, 1); new Task(() => { int val = 0; queue.TryRemove(2, out val); }).Start(); var watch = Stopwatch.StartNew(); var node = queue.Take(); watch.Stop(); var elapsed = Convert.ToInt32(watch.Elapsed.TotalMilliseconds) / 1000; Assert.AreEqual(1, elapsed); }
public void TakeTest() { var queue = new TimeoutNotifyQueue <int>(10); for (int i = 1; i <= 10; i++) { Assert.IsTrue(queue.Offer(i, i, 0)); } System.Threading.Thread.Sleep(100); int j = 1; while (true) { if (queue.Count > 0) { var node = queue.Take(); do { Assert.AreEqual(j, node.Key); j++; node = node.Next; }while (node != null); } else { break; } } Assert.AreEqual(0, queue.Count); Assert.AreEqual(11, j); for (int i = 1; i <= 10; i++) { Assert.IsTrue(queue.Offer(i, i, 0)); } int val = 0; Assert.IsTrue(queue.TryRemove(7, out val)); j = 1; while (true) { if (queue.Count > 0) { var node = queue.Take(); do { Assert.AreEqual(j, node.Key); if (++j == 7) { j++; } node = node.Next; }while (node != null); } else { break; } } Assert.AreEqual(0, queue.Count); Assert.AreEqual(11, j); }
public void TakeTest() { var queue = new TimeoutNotifyQueue<int>(10); for (int i = 1; i <= 10; i++) { Assert.IsTrue(queue.Offer(i, i, 0)); } System.Threading.Thread.Sleep(100); int j = 1; while (true) { if (queue.Count > 0) { var node = queue.Take(); do { Assert.AreEqual(j, node.Key); j++; node = node.Next; } while (node != null); } else break; } Assert.AreEqual(0, queue.Count); Assert.AreEqual(11, j); for (int i = 1; i <= 10; i++) { Assert.IsTrue(queue.Offer(i, i, 0)); } int val = 0; Assert.IsTrue(queue.TryRemove(7, out val)); j = 1; while (true) { if (queue.Count > 0) { var node = queue.Take(); do { Assert.AreEqual(j, node.Key); if (++j == 7) j++; node = node.Next; } while (node != null); } else break; } Assert.AreEqual(0, queue.Count); Assert.AreEqual(11, j); }
public void RemoveCauseSequenceChangedTest() { var queue = new TimeoutNotifyQueue<int>(10); queue.Offer(2, 2, 5); queue.Offer(3, 3, 1); new Task(() => { int val = 0; queue.TryRemove(2, out val); }).Start(); var watch = Stopwatch.StartNew(); var node = queue.Take(); watch.Stop(); var elapsed = Convert.ToInt32(watch.Elapsed.TotalMilliseconds) / 1000; Assert.AreEqual(1, elapsed); }
public void OfferCauseSequenceChangedTest() { var queue = new TimeoutNotifyQueue<int>(10); queue.Offer(2, 2, 3); new Task(() => { queue.Offer(1, 1, 1); }).Start(); var node = queue.Take(); Assert.AreEqual(1, node.Key); node = queue.Take(); Assert.AreEqual(2, node.Key); }
public void TakeWaitTest() { var queue = new TimeoutNotifyQueue<int>(10); queue.Offer(1, 1, 1); var watch = Stopwatch.StartNew(); queue.Take(); watch.Stop(); var elapsed = Convert.ToInt32(watch.Elapsed.TotalMilliseconds) / 1000; Assert.IsTrue(elapsed >= 0); Assert.IsTrue(elapsed <= 2); var are = new AutoResetEvent(false); new Task(() => { watch = Stopwatch.StartNew(); var i = queue.Take(); Assert.AreEqual(10, i.Key); watch.Stop(); elapsed = Convert.ToInt32(watch.Elapsed.TotalMilliseconds) / 1000; Assert.IsTrue(elapsed >= 1); Assert.IsTrue(elapsed <= 3); are.Set(); }).Start(); //total 3 secs System.Threading.Thread.Sleep(2000); queue.Offer(10, 10, 1); are.WaitOne(); }