Ejemplo n.º 1
0
        public void BeginReadInvokesCallbackWhenDataAvailable()
        {
            byte[] writeBuf = new byte[256];
            byte[] readBuf  = new byte[256];

            for (int i = 0; i < 256; i++)
            {
                writeBuf[i] = (byte)i;
            }

            bool[] readCallbackInvoked  = new bool[1];
            bool[] writeCallbackInvoked = new bool[1];

            AsyncCallback readCallback = (ar) => {
                int numBytes = StreamA.EndRead(ar);
                Assert.AreEqual(readBuf.Length, numBytes);
                readCallbackInvoked[0] = true;
            };

            AsyncCallback writeCallback = (ar) => {
                StreamB.EndWrite(ar);
                writeCallbackInvoked[0] = true;
            };

            StreamA.BeginRead(readBuf, 0, readBuf.Length, readCallback, null);

            StreamB.BeginWrite(writeBuf, 0, writeBuf.Length, writeCallback, null);

            while (!readCallbackInvoked[0] || !writeCallbackInvoked[0])
            {
                Thread.Sleep(1);
            }

            Assert.AreEqual(readBuf, writeBuf);
        }
Ejemplo n.º 2
0
        public void FillingSendBufferCausesWriteToBlock()
        {
            byte[] buf = new byte[102400];

            StreamA.Write(buf, 0, buf.Length);

            var f = new Future <object>();

            ThreadPool.QueueUserWorkItem((_) => {
                try {
                    StreamA.Write(buf, 0, buf.Length);
                    f.Complete();
                } catch (Exception ex) {
                    f.Fail(ex);
                }
            }, null);

            Thread.Sleep(3000);

            Assert.IsFalse(f.Completed, "Expected a full send buffer to make write operation block");

            A.Close();
            B.Close();
            StreamA.Dispose();
            StreamB.Dispose();

            GC.Collect();

            Thread.Sleep(1000);

            Assert.IsTrue(f.Completed);
            Assert.IsTrue(f.Failed);
        }
Ejemplo n.º 3
0
 private void StopAllStreams()
 {
     StreamA.Stop();
     StreamB.Stop();
     StreamC.Stop();
     StreamD.Stop();
 }