예제 #1
0
        public void ConcurrentBeginWritesSecondFails()
        {
            // GIVEN: a QueuedStream
            var m     = new Mock <Stream>(MockBehavior.Strict);
            var q     = new QueuedStream(m.Object);
            var a     = new MockAsyncResult("A");
            var b     = new MockAsyncResult("B");
            var c     = new MockAsyncResult("C");
            var trace = new StringBuilder();

            m.SetupBeginWrite(a, trace);
            m.SetupEndWrite(a, trace);
            m.SetupBeginWrite(b, new ApplicationException("**ERROR**"), trace);
            m.SetupBeginWrite(c, trace);
            m.SetupEndWrite(c, trace);

            // WHEN: i make three concurrent calls to BeginWrite
            // AND : the second concurrent operation fails in BeginWrite
            q.BeginWrite(a.Data, 0, a.Data.Length, q.EndWrite, null);
            q.BeginWrite(b.Data, 0, b.Data.Length, ar => {
                var ex = Assert.Throws <ApplicationException>(() => q.EndWrite(ar));
                trace.AppendFormat("EndWrite({0})", ex.Message);
            }, null);
            q.BeginWrite(c.Data, 0, c.Data.Length, q.EndWrite, null);
            a.Complete(trace);
            c.Complete(trace);

            // THEN: i expect BeginWrite of the underlying stream to be called one after each other
            // AND : and EndWrite of the second operation fails with and exception
            Assert.That(trace.ToString(), Is.EqualTo("BeginWrite(A) Complete(A) EndWrite(A)BeginWrite(**ERROR**)EndWrite(**ERROR**)BeginWrite(C) Complete(C) EndWrite(C)"));
            m.VerifyAll();
        }
예제 #2
0
        public void ConcurrentBeginWritesFirstEndWriteFails()
        {
            // GIVEN: a QueuedStream
            var m     = new Mock <Stream>(MockBehavior.Strict);
            var q     = new QueuedStream(m.Object);
            var a     = new MockAsyncResult("A");
            var b     = new MockAsyncResult("B");
            var trace = new StringBuilder();

            m.SetupBeginWrite(a, trace);
            m.SetupEndWrite(a, new ApplicationException("**ERROR**"), trace);
            m.SetupBeginWrite(b, trace);
            m.SetupEndWrite(b, trace);

            // WHEN: i make two concurrent calls to BeginWrite
            // AND : the first concurrent operation fails in EndWrite
            q.BeginWrite(a.Data, 0, a.Data.Length, ar => Assert.Throws <ApplicationException>(() => q.EndWrite(ar)), null);
            q.BeginWrite(b.Data, 0, b.Data.Length, q.EndWrite, null);
            a.Complete(trace);
            b.Complete(trace);

            // THEN: i expect BeginWrite of the underlying stream to be called one after each other
            // AND : and EndWrite of the first operation fails with an exception
            Assert.That(trace.ToString(), Is.EqualTo("BeginWrite(A) Complete(A) EndWrite(**ERROR**)BeginWrite(B) Complete(B) EndWrite(B)"));
            m.VerifyAll();
        }
예제 #3
0
        public void EndWrite()
        {
            var q = new QueuedStream(new MemoryStream());
            var a = new MockAsyncResult("A");

            Assert.Throws <ArgumentException>(() => q.EndWrite(a));
        }
예제 #4
0
 public static void SetupBeginWrite(this Mock <Stream> mock, MockAsyncResult ar, StringBuilder trace)
 {
     mock.Setup(x => x.BeginWrite(ar.Data, 0, ar.Data.Length, It.IsAny <AsyncCallback>(), It.IsAny <object>()))
     .Returns <byte[], int, int, AsyncCallback, object>((b, o, l, cb, os) => {
         trace.AppendFormat("BeginWrite({0})", ar);
         ar.Callback = cb;
         return(ar);
     });
 }
예제 #5
0
        public void EndRead()
        {
            var m = new Mock <Stream>();
            var q = new QueuedStream(m.Object);
            var a = new MockAsyncResult("A");

            m.Setup(x => x.EndRead(a));

            q.EndRead(a);

            m.VerifyAll();
        }
예제 #6
0
        public void BeginRead()
        {
            var m = new Mock <Stream>();
            var q = new QueuedStream(m.Object);
            var d = new byte[] { 1, 2, 3 };
            var a = new MockAsyncResult("A");

            m.Setup(x => x.BeginRead(d, 0, d.Length, It.IsAny <AsyncCallback>(), null))
            .Returns <byte[], int, int, AsyncCallback, object>((b, o, l, c, s) => a);

            Assert.That(q.BeginRead(d, 0, d.Length, ar => { }, null), Is.EqualTo(a));

            m.VerifyAll();
        }
예제 #7
0
        public void ConcurrentBeginWrites()
        {
            // GIVEN: a QueuedStream
            var m     = new Mock <Stream>();
            var q     = new QueuedStream(m.Object);
            var a     = new MockAsyncResult("A");
            var b     = new MockAsyncResult("B");
            var trace = new StringBuilder();

            m.SetupBeginWrite(a, trace);
            m.SetupEndWrite(a, trace);
            m.SetupBeginWrite(b, trace);
            m.SetupEndWrite(b, trace);

            // WHEN: i make two concurrent calls to BeginWrite
            q.BeginWrite(a.Data, 0, a.Data.Length, q.EndWrite, null);
            q.BeginWrite(b.Data, 0, b.Data.Length, q.EndWrite, null);
            a.Complete(trace);
            b.Complete(trace);

            // THEN: i expect BeginWrite of the underlying stream to be called one after each other
            Assert.That(trace.ToString(), Is.EqualTo("BeginWrite(A) Complete(A) EndWrite(A)BeginWrite(B) Complete(B) EndWrite(B)"));
            m.VerifyAll();
        }
예제 #8
0
 public static void SetupEndWrite(this Mock <Stream> mock, MockAsyncResult ar, Exception error, StringBuilder trace)
 {
     mock.Setup(x => x.EndWrite(ar)).Callback(() => trace.AppendFormat("EndWrite({0})", error.Message)).Throws(error);
 }
예제 #9
0
 public static void SetupEndWrite(this Mock <Stream> mock, MockAsyncResult ar, StringBuilder rec)
 {
     mock.Setup(x => x.EndWrite(ar)).Callback(() => rec.AppendFormat("EndWrite({0})", ar));
 }
예제 #10
0
 public static void SetupBeginWrite(this Mock <Stream> mock, MockAsyncResult ar, Exception error, StringBuilder trace)
 {
     mock.Setup(x => x.BeginWrite(ar.Data, 0, ar.Data.Length, It.IsAny <AsyncCallback>(), It.IsAny <object>()))
     .Callback(() => trace.AppendFormat("BeginWrite({0})", error.Message))
     .Throws(error);
 }