public void ChannelTests() { Channel c0 = new Channel(); bool e_event_fired = false; c0.EnqueueEvent += delegate(object o, EventArgs arg) { e_event_fired = true; }; c0.Enqueue(0); bool c_event_fired = false; c0.CloseEvent += delegate(object o, EventArgs arg) { c_event_fired = true; }; c0.Close(); Assert.IsTrue(c_event_fired, "CloseEvent"); c0 = new Channel(); c0.CloseAfterEnqueue(); c_event_fired = false; c0.CloseEvent += delegate(object o, EventArgs arg) { c_event_fired = true; }; c0.Enqueue(1); //This should close the channel: Assert.IsTrue(c_event_fired, "CloseEvent on Enqueue"); Assert.IsTrue(c0.Closed, "Closed"); c0 = new Channel(1); c_event_fired = false; c0.CloseEvent += delegate(object o, EventArgs arg) { c_event_fired = true; }; c0.Enqueue(1); //This should close the channel: Assert.IsTrue(c_event_fired, "CloseEvent on Enqueue"); Assert.IsTrue(c0.Closed, "Closed"); //Try with different starting values: Random r = new Random(); int en_count; for (int i = 0; i < 100; i++) { int max_enqueues = r.Next(1, 1000); c0 = new Channel(max_enqueues); c_event_fired = false; en_count = 0; c0.CloseEvent += delegate(object o, EventArgs arg) { c_event_fired = true; }; c0.EnqueueEvent += delegate(object o, EventArgs arg) { en_count++; }; for (int j = 0; j < max_enqueues; j++) { c0.Enqueue(j); } Assert.IsTrue(c_event_fired, "CloseEvent on Enqueue"); Assert.AreEqual(en_count, max_enqueues, "EnqueueEvent count"); Assert.IsTrue(c0.Closed, "Closed"); try { c0.Enqueue(null); Assert.IsTrue(false, "Enqueue after close didn't fail"); } catch { Assert.IsTrue(true, "Enqueue after close Got exception"); } } }
public void ChannelTests() { Channel c0 = new Channel(); bool e_event_fired = false; c0.EnqueueEvent += delegate(object o, EventArgs arg) { e_event_fired = true; }; c0.Enqueue(0); bool c_event_fired = false; c0.CloseEvent += delegate(object o, EventArgs arg) { c_event_fired = true; }; c0.Close(); Assert.IsTrue(c_event_fired, "CloseEvent"); c0 = new Channel(); c0.CloseAfterEnqueue(); c_event_fired = false; c0.CloseEvent += delegate(object o, EventArgs arg) { c_event_fired = true; }; c0.Enqueue(1); //This should close the channel: Assert.IsTrue(c_event_fired, "CloseEvent on Enqueue"); Assert.IsTrue(c0.Closed, "Closed"); c0 = new Channel(1); c_event_fired = false; c0.CloseEvent += delegate(object o, EventArgs arg) { c_event_fired = true; }; c0.Enqueue(1); //This should close the channel: Assert.IsTrue(c_event_fired, "CloseEvent on Enqueue"); Assert.IsTrue(c0.Closed, "Closed"); //Try with different starting values: Random r = new Random(); int en_count; for(int i = 0; i < 100; i++) { int max_enqueues = r.Next(1, 1000); c0 = new Channel(max_enqueues); c_event_fired = false; en_count = 0; c0.CloseEvent += delegate(object o, EventArgs arg) { c_event_fired = true; }; c0.EnqueueEvent += delegate(object o, EventArgs arg) { en_count++; }; for(int j = 0; j < max_enqueues; j++) { c0.Enqueue(j); } Assert.IsTrue(c_event_fired, "CloseEvent on Enqueue"); Assert.AreEqual(en_count, max_enqueues, "EnqueueEvent count"); Assert.IsTrue(c0.Closed, "Closed"); try { c0.Enqueue(null); Assert.IsTrue(false, "Enqueue after close didn't fail"); } catch { Assert.IsTrue(true, "Enqueue after close Got exception"); } } }
override protected void SeekTAs(DateTime now) { if(Interlocked.Exchange(ref _ongoing, 1) == 1) { return; } Channel chan = new Channel(); EventHandler handler = delegate(object o, EventArgs ea) { if(!chan.Closed && chan.Count < 8) { return; } List<TransportAddress> tas = new List<TransportAddress>(); while(chan.Count > 0) { AHAddress addr = null; try { IDictionary dict = (IDictionary) chan.Dequeue(); byte[] baddr = (byte[]) dict["value"]; addr = new AHAddress(MemBlock.Reference(baddr)); } catch { continue; } tas.Add(new SubringTransportAddress(addr, _shared_namespace)); } if(tas.Count > 0) { CheckAndUpdateRemoteTAs(tas); } if(chan.Closed) { Interlocked.Exchange(ref _ongoing, 0); } }; if(_steady_state == 0) { chan.EnqueueEvent += handler; } chan.CloseEvent += handler; try { _dht.AsyncGet(_private_dht_key, chan); } catch(DhtException) { chan.Close(); } }