public void testForSimpleOutput() { try { // artificial listening socket for this test Socket server = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // bind to system-assigned port server.Bind(new IPEndPoint(IPAddress.Loopback, 0)); server.Listen(1); string target = "tcp://127.0.0.1:" + ((IPEndPoint)server.LocalEndPoint).Port.ToString(); try { Channel ch = new Channel(target, new Options(null), null, null, LogEventArgs.LogLevel.LOW); Socket accepted = server.Accept(); IList<byte[]> buffers = new List<byte[]>(); buffers.Add(new byte[] { 10, 11, 12, 13 }); ProgressCallback callback = new ProgressCallback(); int messageHeaderSize = 4; ch.post(123, 0, buffers, messageHeaderSize, callback); ch.doSomeWork(false, true); // the buffers are short and the whole can be expected // to be pushed in a single operation Assert.IsTrue(callback.progressCalled); Assert.IsTrue(callback.sentBytes == callback.totalByteCount); Assert.IsFalse(callback.cancelledCalled); // read the data on the receiving end byte[] buf = new byte[1024]; int readBytes = accepted.Receive(buf); Assert.IsTrue(readBytes == 20); // message id Assert.IsTrue(buf[0] == 123); Assert.IsTrue(buf[1] == 0); Assert.IsTrue(buf[2] == 0); Assert.IsTrue(buf[3] == 0); // frame number (-1 -> it is the only one) Assert.IsTrue(buf[4] == 0xFF); Assert.IsTrue(buf[5] == 0xFF); Assert.IsTrue(buf[6] == 0xFF); Assert.IsTrue(buf[7] == 0xFF); // size of message header Assert.IsTrue(buf[8] == 4); Assert.IsTrue(buf[9] == 0); Assert.IsTrue(buf[10] == 0); Assert.IsTrue(buf[11] == 0); // frame payload Assert.IsTrue(buf[12] == 4); Assert.IsTrue(buf[13] == 0); Assert.IsTrue(buf[14] == 0); Assert.IsTrue(buf[15] == 0); // payload Assert.IsTrue(buf[16] == 10); Assert.IsTrue(buf[17] == 11); Assert.IsTrue(buf[18] == 12); Assert.IsTrue(buf[19] == 13); ch.close(); accepted.Close(); server.Close(); } catch(Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); Assert.Fail("should never reach this point"); } } catch(Exception) { Assert.Fail("should never reach this point"); } }
public virtual void testForNodeInsertion() { Channel ch = null; try { ch = new Channel("null", new Options(null), null, null, LogEventArgs.LogLevel.LOW); } catch(YAMIIOException) { Assert.Fail("should never reach this point"); } catch(SocketException) { Assert.Fail("should never reach this point"); } Assert.IsTrue(ch.OutgoingFrames.Count == 0); // first node byte[] buf = new byte[123]; List<byte[]> buffers = new List<byte[]>(); buffers.Add(buf); ch.post(0, 0, buffers, 0, null); IList<OutgoingFrame> frames = ch.OutgoingFrames; Assert.IsTrue(frames.Count == 1); Assert.IsTrue(frames[0].payload.Length == 123); // second node, added after 1st buf = new byte[456]; buffers.Clear(); buffers.Add(buf); ch.post(1, 0, buffers, 0, null); frames = ch.OutgoingFrames; Assert.IsTrue(frames.Count == 2); Assert.IsTrue(frames[0].payload.Length == 123); Assert.IsTrue(frames[1].payload.Length == 456); // third node, at the end buf = new byte[789]; buffers.Clear(); buffers.Add(buf); ch.post(2, 0, buffers, 0, null); frames = ch.OutgoingFrames; Assert.IsTrue(frames.Count == 3); Assert.IsTrue(frames[0].payload.Length == 123); Assert.IsTrue(frames[1].payload.Length == 456); Assert.IsTrue(frames[2].payload.Length == 789); // three buffers inserted after first node buffers.Clear(); buffers.Add(new byte[1]); buffers.Add(new byte[2]); buffers.Add(new byte[3]); ch.post(3, 3, buffers, 0, null); frames = ch.OutgoingFrames; Assert.IsTrue(frames.Count == 6); Assert.IsTrue(frames[0].payload.Length == 123); Assert.IsTrue(frames[1].payload.Length == 1); Assert.IsTrue(frames[2].payload.Length == 2); Assert.IsTrue(frames[3].payload.Length == 3); Assert.IsTrue(frames[4].payload.Length == 456); Assert.IsTrue(frames[5].payload.Length == 789); // two new nodes after the previous three buffers.Clear(); buffers.Add(new byte[4]); buffers.Add(new byte[5]); ch.post(4, 1, buffers, 0, null); frames = ch.OutgoingFrames; Assert.IsTrue(frames.Count == 8); Assert.IsTrue(frames[0].payload.Length == 123); Assert.IsTrue(frames[1].payload.Length == 1); Assert.IsTrue(frames[2].payload.Length == 2); Assert.IsTrue(frames[3].payload.Length == 3); Assert.IsTrue(frames[4].payload.Length == 4); Assert.IsTrue(frames[5].payload.Length == 5); Assert.IsTrue(frames[6].payload.Length == 456); Assert.IsTrue(frames[7].payload.Length == 789); // poison pill added between 3 and 4 bool closeMe = ch.postClose(2); Assert.IsFalse(closeMe); frames = ch.OutgoingFrames; Assert.AreEqual(5, frames.Count); Assert.IsTrue(frames[0].payload.Length == 123); Assert.IsTrue(frames[1].payload.Length == 1); Assert.IsTrue(frames[2].payload.Length == 2); Assert.IsTrue(frames[3].payload.Length == 3); Assert.IsTrue(frames[4].closeFlag); }