Beispiel #1
0
        public void TestEndedTransaction()
        {
            using (ProgressTracker tracker = new ProgressTracker()) {
                IProgressTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);

                TestTransaction test1 = new TestTransaction();

                // Step 1
                {
                    Expect.Once.On(mockedSubscriber).
                    Method("IdleStateChanged").
                    WithAnyArguments();

                    Expect.Between(0, 1).On(mockedSubscriber).
                    Method("ProgressChanged").
                    With(
                        new Matcher[] {
                        new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
                        new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.0f))
                    }
                        );

                    tracker.Track(test1);
                }

                // Step 2
                {
                    Expect.Once.On(mockedSubscriber).
                    Method("ProgressChanged").
                    With(
                        new Matcher[] {
                        new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
                        new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.5f))
                    }
                        );

                    tracker.Track(Transaction.EndedDummy);
                }

                // Step 3
                {
                    Expect.Once.On(mockedSubscriber).
                    Method("ProgressChanged").
                    With(
                        new Matcher[] {
                        new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
                        new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(1.0f))
                    }
                        );

                    Expect.Once.On(mockedSubscriber).
                    Method("IdleStateChanged").
                    WithAnyArguments();

                    test1.End();
                }
            }

            this.mockery.VerifyAllExpectationsHaveBeenMet();
        }
        public void SendAcknowledgementNotReceivedReconnectAcknowledgementNotReceived()
        {
            using (mocks.Ordered)
            {
                SetConnectExpectations();
                Expect.Once.On(mockTcpClient).GetProperty("Connected").Will(Return.Value(true));
                Expect.Once.On(mockTcpClient).Method("GetStream").WithNoArguments().Will(Return.Value(mockNetworkStream));
                Expect.Once.On(mockNetworkStream).Method("Write").With(new object[] { testEncodedMessageByteArray, 0, testEncodedMessageByteArray.Length });
                Expect.Between(1, 3).On(mockTcpClient).GetProperty("Available").Will(Return.Value(0));
                // Expectations for attempt to reconnect
                Expect.Once.On(mockTcpClient).Method("Close").WithNoArguments();
                Expect.Once.On(mockTcpClient).Method("Connect").With(new object[] { testIpAddress, testPort });
                Expect.Once.On(mockTcpClient).GetProperty("Connected").Will(Return.Value(true));
                // Expectations for resend
                Expect.Once.On(mockTcpClient).Method("GetStream").WithNoArguments().Will(Return.Value(mockNetworkStream));
                Expect.Once.On(mockNetworkStream).Method("Write").With(new object[] { testEncodedMessageByteArray, 0, testEncodedMessageByteArray.Length });
                Expect.Between(1, 3).On(mockTcpClient).GetProperty("Available").Will(Return.Value(0));
            }

            Exception e = Assert.Throws <Exception>(delegate
            {
                testTcpRemoteSender.Connect();
                testTcpRemoteSender.Send(testMessage);
            });

            mocks.VerifyAllExpectationsHaveBeenMet();
            Assert.That(e.Message, NUnit.Framework.Is.StringStarting("Error sending message.  Failed to send message after reconnecting."));
            Assert.That(e.InnerException.Message, NUnit.Framework.Is.StringStarting("Failed to receive message acknowledgement within timeout period of 25 milliseconds."));
        }
Beispiel #3
0
        public void ExpectInvokedBetweenXAndYTimes()
        {
            Expect.Between(2, 4).MethodCall(() => myObject.MyEmptyMethod());

            myObject.MyEmptyMethod();
            myObject.MyEmptyMethod();
            myObject.MyEmptyMethod();
        }
        public object Invoke([CanBeNull] object @object, [NotNull] string member, [CanBeNull, ItemCanBeNull] object[] args = null)
        {
            Expect.Identifier(nameof(member), member);

            var argCount = args?.Length ?? 0;

            Expect.Between(nameof(args), argCount, 0, MaximumArgumentCount);

            if (@object == null)
            {
                return(null);
            }

            var key = $"{member}.{argCount}";

            if (!_cachedFuncInvokeDelegateDict.TryGetValue(key, out var @delegate))
            {
                @delegate = CreateNewFuncInvokeDelegate(member, argCount);
                _cachedFuncInvokeDelegateDict.Add(key, @delegate);
            }
            Debug.Assert(@delegate != null, "@delegate is always expected to be set.");

            try
            {
                return(@delegate(@object, args));
            }
            catch (TargetInvocationException targetInvokationError)
            {
                if (targetInvokationError.InnerException != null)
                {
                    if (targetInvokationError.InnerException is RuntimeBinderException)
                    {
                        return(null);
                    }

                    throw targetInvokationError.InnerException;
                }
                throw;
            }
            catch (RuntimeBinderException)
            {
                return(null);
            }
        }
Beispiel #5
0
        public void TestSummedProgress()
        {
            using (ProgressTracker tracker = new ProgressTracker()) {
                IProgressTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);

                TestTransaction test1 = new TestTransaction();
                TestTransaction test2 = new TestTransaction();

                // Step 1
                {
                    Expect.Once.On(mockedSubscriber).Method("IdleStateChanged").WithAnyArguments();

                    // Since the progress is already at 0, these redundant reports are optional
                    Expect.Between(0, 2).On(mockedSubscriber).Method("ProgressChanged").With(
                        new Matcher[] {
                        new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
                        new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.0f))
                    }
                        );

                    tracker.Track(test1);
                    tracker.Track(test2);
                }

                // Step 2
                {
                    Expect.Once.On(mockedSubscriber).Method("ProgressChanged").With(
                        new Matcher[] {
                        new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
                        new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.25f))
                    }
                        );

                    test1.ChangeProgress(0.5f);
                }
            }

            this.mockery.VerifyAllExpectationsHaveBeenMet();
        }
        public void MeetExpectationsInScope()
        {
            var expectationScope = new ExpectationScope();

            var myObject1 = Mock.Interface <IMyObject>(expectationScope);
            var myObject2 = Mock.Interface <IMyObject>(expectationScope);

            Expect.Once.MethodCall(() => myObject1.MyMethod(1));
            Expect.Once.MethodCall(() => myObject2.MyMethod(2));

            using (expectationScope.BeginOrdered())
            {
                Expect.AtLeastOnce.MethodCall(() => myObject1.MyMethod(3));
                Expect.Between(0, 3).MethodCall(() => myObject1.MyGenericMethod("WILL PROBABLY NEVER BE INVOKED"));
                Expect.AtLeastOnce.MethodCall(() => myObject1.MyMethod(Any <int> .Value.Matching(i => i > 10)));

                using (expectationScope.BeginUnordered())
                {
                    Expect.AtLeastOnce.MethodCall(() => myObject2.MyMethod(4));
                    Expect.AtLeastOnce.MethodCall(() => myObject1.MyMethod(5));
                }
            }

            myObject1.MyMethod(1);
            myObject2.MyMethod(2);

            myObject1.MyMethod(3);
            myObject1.MyMethod(3);

            myObject1.MyMethod(13);
            myObject1.MyMethod(12);
            myObject1.MyMethod(11);

            myObject1.MyMethod(5);
            myObject2.MyMethod(4);

            AssertExpectations.IsMetFor(expectationScope);
        }
        public void SendAcknowledgementNotReceivedReconnectSuccessTest()
        {
            using (mocks.Ordered)
            {
                SetConnectExpectations();
                Expect.Once.On(mockTcpClient).GetProperty("Connected").Will(Return.Value(true));
                Expect.Once.On(mockTcpClient).Method("GetStream").WithNoArguments().Will(Return.Value(mockNetworkStream));
                Expect.Once.On(mockNetworkStream).Method("Write").With(new object[] { testEncodedMessageByteArray, 0, testEncodedMessageByteArray.Length });
                Expect.Between(1, 3).On(mockTcpClient).GetProperty("Available").Will(Return.Value(0));
                // Expectations for attempt to reconnect
                Expect.Once.On(mockTcpClient).Method("Close").WithNoArguments();
                Expect.Once.On(mockTcpClient).Method("Connect").With(new object[] { testIpAddress, testPort });
                Expect.Once.On(mockTcpClient).GetProperty("Connected").Will(Return.Value(true));
                // Expectations for resend
                Expect.Once.On(mockTcpClient).Method("GetStream").WithNoArguments().Will(Return.Value(mockNetworkStream));
                Expect.Once.On(mockNetworkStream).Method("Write").With(new object[] { testEncodedMessageByteArray, 0, testEncodedMessageByteArray.Length });
                Expect.Once.On(mockTcpClient).GetProperty("Available").Will(Return.Value(1));
                Expect.Once.On(mockNetworkStream).Method("ReadByte").WithNoArguments().Will(Return.Value(6));
            }

            testTcpRemoteSender.Connect();
            testTcpRemoteSender.Send(testMessage);
            mocks.VerifyAllExpectationsHaveBeenMet();
        }
Beispiel #8
0
        public void TestDelayedRemoval()
        {
            using (ProgressTracker tracker = new ProgressTracker()) {
                IProgressTrackerSubscriber mockedSubscriber = mockSubscriber(tracker);

                TestTransaction test1 = new TestTransaction();
                TestTransaction test2 = new TestTransaction();

                // Step 1
                {
                    Expect.Once.On(mockedSubscriber).
                    Method("IdleStateChanged").
                    WithAnyArguments();

                    // This is optional. The tracker's progress is currently 0, so there's no need
                    // to send out superfluous progress reports.
                    Expect.Between(0, 2).On(mockedSubscriber).
                    Method("ProgressChanged").
                    With(
                        new Matcher[] {
                        new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
                        new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.0f))
                    }
                        );

                    tracker.Track(test1);
                    tracker.Track(test2);
                }

                // Step 2
                {
                    Expect.Once.On(mockedSubscriber).
                    Method("ProgressChanged").
                    With(
                        new Matcher[] {
                        new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
                        new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.25f))
                    }
                        );

                    // Total progress should be 0.25 after this call (two transactions, one with
                    // 0% progress and one with 50% progress)
                    test1.ChangeProgress(0.5f);
                }

                // Step 3
                {
                    Expect.Once.On(mockedSubscriber).
                    Method("ProgressChanged").
                    With(
                        new Matcher[] {
                        new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
                        new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(0.75f))
                    }
                        );

                    // Total progress should be 0.75 after this call (one transaction at 100%,
                    // the other one at 50%). If the second transaction would be removed by the tracker,
                    // (which would be inappropriate!) the progress would falsely jump to 0.5 instead.
                    test2.End();
                }

                // Step 4
                {
                    Expect.Once.On(mockedSubscriber).
                    Method("ProgressChanged").
                    With(
                        new Matcher[] {
                        new NMock2.Matchers.TypeMatcher(typeof(ProgressTracker)),
                        new ProgressReportEventArgsMatcher(new ProgressReportEventArgs(1.0f))
                    }
                        );

                    Expect.Once.On(mockedSubscriber).
                    Method("IdleStateChanged").
                    WithAnyArguments();

                    test1.End();
                }
            }

            this.mockery.VerifyAllExpectationsHaveBeenMet();
        }
Beispiel #9
0
 //----
 internal static void HackAllowShutdownCall(IBluetopiaApi mockedApi)
 {
     Expect.Between(0, 1).On(mockedApi).Method("BSC_Shutdown").WithAnyArguments();
 }