public virtual void Sync_Timeout() { int expected = 3; using (INamedChannel <int> c = new mq.Channel <int>("test-sync" + Salt)) { Task.Factory.StartNew(() => { Thread.Sleep(Timeblok * 20); c.Write(expected); Thread.Sleep(Timeblok * 20); c.Write(expected); }); Assert.AreEqual(expected, c.Read(Timeblok * 40)); try { Assert.AreEqual(expected, c.Read(Timeblok / 2)); Assert.Fail("Expected exception."); } catch (Exception e) { Assert.IsInstanceOfType(e, typeof(TimeoutException)); } } }
public virtual void ActiveEnumeratorTest() { int count = 100; int[] expected = Enumerable.Range(0, count).ToArray(); List <string> actual = new List <string>(); using (IChannel <int> c = new mq.Channel <int>("test-ActiveEnumeratorTest1" + Salt)) using (IChannel <string> w = new mq.Channel <string>("test-ActiveEnumeratorTest2" + Salt)) { w.ActiveEnumerate(p => actual.Add(p)).StoppedEvent += e => { Assert.IsInstanceOfType(e, typeof(ChannelDrainedException)); c.Write(0); }; Task.Factory.StartNew(() => { for (int i = 0; i < count; i++) { w.Write(i.ToString()); } w.Close(); }); c.Read(); Assert.AreEqual(count, actual.Count); CollectionAssert.AreEquivalent(expected.Select(p => p.ToString()).ToArray(), actual); } }
public virtual void Enumerate() { int count = 100; int[] expected = Enumerable.Range(0, count).ToArray(); List <int> actual = new List <int>(); using (IChannel <int> c = new mq.Channel <int>("test-Enumerate1" + Salt)) using (IChannel <int> w = new mq.Channel <int>("test-Enumerate2" + Salt)) { Task.Factory.StartNew(() => { for (int i = 0; i < count; i++) { c.Write(i); } c.Close(); }); Task.Factory.StartNew(() => { foreach (int item in c.Enumerate()) { actual.Add(item); } w.Write(count); w.Close(); }); Assert.AreEqual(count, w.Read()); w.WaitReadable.WaitOne(); CollectionAssert.AreEquivalent(expected, actual); } }
public virtual void Acc() { int a = 1, b = 2; int expected = a + b; using (IChannel <int> c = new mq.Channel <int>("test-Acc" + Salt)) { Task.Factory.StartNew(() => { Thread.Sleep(Timeblok); c.Write(a); }); Task.Factory.StartNew(() => { Thread.Sleep(Timeblok); c.Write(b); }); Assert.AreEqual(expected, c.Read() + c.Read()); } }
public virtual void SyncWrite() { int expected = 3; using (IChannel <int> c = new mq.Channel <int>("test-SyncWrite1" + Salt)) using (IChannel <int> w = new mq.Channel <int>("test-SyncWrite2" + Salt)) { Task.Factory.StartNew(() => { Thread.Sleep(Timeblok); c.Read(); c.Read(); c.Write(expected); w.Write(0); }); c.Write(0); c.Write(1); w.Read(); Assert.AreEqual(expected, c.Read()); } }
public virtual void Sync() { int expected = 3; using (INamedChannel <int> c = new mq.Channel <int>("test-sync" + Salt)) { Task.Factory.StartNew(() => { Thread.Sleep(Timeblok); c.Write(expected); }); Assert.AreEqual(expected, c.Read()); } }
public virtual void SyncDiff() { int expected = 3; INamedChannel <int> c = new mq.Channel <int>("test-sync" + Salt); { Task.Factory.StartNew(() => { INamedChannel <int> w = new mq.Channel <int>(c.Name); Thread.Sleep(Timeblok); w.Write(expected); }); Assert.AreEqual(expected, c.Read()); } }
public virtual void InputAdapter() { int expected = 3; using (IChannel <string> c = new mq.Channel <string>("test-InputAdapter" + Salt)) using (IChannelWriter <int> ca = new FuncChannelInputAdapter <int, string>(c, p => p.ToString())) { Task.Factory.StartNew(() => { Thread.Sleep(Timeblok); ca.Write(expected); }); Assert.AreEqual(expected.ToString(), c.Read()); } }
public virtual void ConcurrentReaders() { int count = 2000; int r0 = 0; int r1 = 0; using (IChannel <int> c = new mq.Channel <int>("test-ConcurrentReaders1" + Salt)) using (IChannel <int> w0 = new mq.Channel <int>("test-ConcurrentReaders2" + Salt)) using (IChannel <int> w1 = new mq.Channel <int>("test-ConcurrentReaders3" + Salt)) { Task.Factory.StartNew(() => { foreach (int item in c.Enumerate()) { r0++; } w0.Write(r0); }); Task.Factory.StartNew(() => { foreach (int item in c.Enumerate()) { r1++; } w1.Write(r1); }); for (int i = 0; i < count; i++) { c.Write(i); } c.Close(); Assert.AreEqual(count, w0.Read() + w1.Read()); Assert.IsTrue(r0 > 0); Assert.IsTrue(r1 > 0); //Assert.AreEqual(0.5, r0 / (double)count, 0.2); //Assert.AreEqual(0.5, r1 / (double)count, 0.2); } }
public virtual void SubscribedReaders() { int count = 10; int r0 = 0; int r1 = 0; using (ISubscribableChannel <int> c = new mq.SubscribableChannel <int>("test-SubscribedReaders1" + Salt)) using (IChannel <int> w0 = new mq.Channel <int>("test-SubscribedReaders2" + Salt)) using (IChannel <int> w1 = new mq.Channel <int>("test-SubscribedReaders3" + Salt)) { Task.Factory.StartNew((object rd) => { foreach (int item in ((IChannelReader <int>)rd).Enumerate()) { r0++; } w0.Write(r0); }, c.Subscribe()); Task.Factory.StartNew((object rd) => { foreach (int item in ((IChannelReader <int>)rd).Enumerate()) { r1++; } w1.Write(r1); }, c.Subscribe()); for (int i = 0; i < count; i++) { c.Write(i); } c.Close(); Assert.AreEqual(2 * count, w0.Read() + w1.Read()); Assert.AreEqual(count, r0); Assert.AreEqual(count, r1); } }