Ejemplo n.º 1
0
    public static void CommunicationObject_Async_Open_Close_Methods_Called()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        List<string> openMethodsCalled = new List<string>();
        List<string> closeMethodsCalled = new List<string>();
        TimeSpan timeout = TimeSpan.FromSeconds(30);

        // *** SETUP *** \\
        MockCommunicationObject.InterceptAllOpenMethods(mco, openMethodsCalled);
        MockCommunicationObject.InterceptAllCloseMethods(mco, closeMethodsCalled);

        // *** EXECUTE *** \\
        IAsyncResult openAr = mco.BeginOpen(timeout, callback: null, state: null);
        mco.OpenAsyncResult.Complete();
        mco.EndOpen(openAr);

        IAsyncResult closeAr = mco.BeginClose(timeout, callback: null, state: null);
        mco.CloseAsyncResult.Complete();
        mco.EndClose(closeAr);

        // *** VALIDATE *** \\
        string expectedOpens = "OnOpening,OnBeginOpen,OnOpened";
        string actualOpens = String.Join(",", openMethodsCalled);
        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
               String.Format("Expected open methods to be '{0}' but actual was '{1}'.",
                             expectedOpens, actualOpens));

        string expectedCloses = "OnClosing,OnBeginClose,OnClosed";
        string actualCloses = String.Join(",", closeMethodsCalled);
        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
               String.Format("Expected close methods to be '{0}' but actual was '{1}'.",
                             expectedCloses, actualCloses));
    }
Ejemplo n.º 2
0
    public static void CommunicationObject_Sync_Open_Close_Methods_Called()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        List <string>           openMethodsCalled  = new List <string>();
        List <string>           closeMethodsCalled = new List <string>();
        TimeSpan timeout = TimeSpan.FromSeconds(30);

        // *** SETUP *** \\
        InterceptAllOpenMethods(mco, openMethodsCalled);
        InterceptAllCloseMethods(mco, closeMethodsCalled);

        // *** EXECUTE *** \\
        mco.Open(timeout);
        mco.Close(timeout);

        // *** VALIDATE *** \\
        string expectedOpens = "OnOpening,OnOpen,OnOpened";
        string actualOpens   = String.Join(",", openMethodsCalled);

        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
                    String.Format("Expected open methods to be '{0}' but actual was '{1}'.",
                                  expectedOpens, actualOpens));

        string expectedCloses = "OnClosing,OnClose,OnClosed";
        string actualCloses   = String.Join(",", closeMethodsCalled);

        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
                    String.Format("Expected close methods to be '{0}' but actual was '{1}'.",
                                  expectedCloses, actualCloses));
    }
Ejemplo n.º 3
0
    public static void CommunicationObject_Async_Close_Propagates_Exception()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        TimeSpan timeout            = TimeSpan.FromSeconds(30);
        string   exceptionMessage   = "Expected exception";

        // *** SETUP *** \\
        mco.OnBeginCloseOverride = (TimeSpan t, AsyncCallback c, object s) =>
        {
            throw new InvalidOperationException(exceptionMessage);
        };

        // *** EXECUTE *** \\
        IAsyncResult openAr = mco.BeginOpen(timeout, callback: null, state: null);

        mco.OpenAsyncResult.Complete();
        mco.EndOpen(openAr);

        InvalidOperationException actualException = Assert.Throws <InvalidOperationException>(() =>
        {
            IAsyncResult closeAr = mco.BeginClose(timeout, callback: null, state: null);
            mco.CloseAsyncResult.Complete();
            mco.EndClose(closeAr);
        });

        // *** VALIDATE *** \\
        Assert.True(String.Equals(exceptionMessage, actualException.Message),
                    String.Format("Expected exception message '{0}' but actual was '{1}'",
                                  exceptionMessage, actualException.Message));
    }
Ejemplo n.º 4
0
    // This helper will override all the open methods of the MockCommunicationObject
    // and record their names into the provided list in the order they are called.
    private static void InterceptAllOpenMethods(MockCommunicationObject mco, List <string> methodsCalled)
    {
        mco.OnOpeningOverride = () =>
        {
            methodsCalled.Add("OnOpening");
            mco.DefaultOnOpening();
        };

        mco.OnOpenOverride = (TimeSpan t) =>
        {
            methodsCalled.Add("OnOpen");
            mco.DefaultOnOpen(t);
        };

        mco.OnBeginOpenOverride = (TimeSpan t, AsyncCallback c, object s) =>
        {
            methodsCalled.Add("OnBeginOpen");
            return(mco.DefaultOnBeginOpen(t, c, s));
        };

        mco.OnOpenedOverride = () =>
        {
            methodsCalled.Add("OnOpened");
            mco.DefaultOnOpened();
        };
    }
Ejemplo n.º 5
0
    // This helper will override all the open methods of the MockCommunicationObject
    // and record their names into the provided list in the order they are called.
    private static void InterceptAllCloseMethods(MockCommunicationObject mco, List <string> methodsCalled)
    {
        mco.OnClosingOverride = () =>
        {
            methodsCalled.Add("OnClosing");
            mco.DefaultOnClosing();
        };

        mco.OnCloseOverride = (TimeSpan t) =>
        {
            methodsCalled.Add("OnClose");
            mco.DefaultOnClose(t);
        };

        mco.OnBeginCloseOverride = (TimeSpan t, AsyncCallback c, object s) =>
        {
            methodsCalled.Add("OnBeginClose");
            return(mco.DefaultOnBeginClose(t, c, s));
        };

        mco.OnClosedOverride = () =>
        {
            methodsCalled.Add("OnClosed");
            mco.DefaultOnClosed();
        };

        // The OnAbort is considered one of the methods associated with close.
        mco.OnAbortOverride = () =>
        {
            methodsCalled.Add("OnAbort");
            mco.DefaultOnAbort();
        };
    }
Ejemplo n.º 6
0
    public static void CommunicationObject_Sync_Close_Propagates_Exception()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        TimeSpan timeout            = TimeSpan.FromSeconds(30);
        string   exceptionMessage   = "Expected exception";

        // *** SETUP *** \\
        mco.OnCloseOverride = (t) =>
        {
            throw new InvalidOperationException(exceptionMessage);
        };

        // *** EXECUTE *** \\
        mco.Open(timeout);

        InvalidOperationException actualException = Assert.Throws <InvalidOperationException>(() =>
        {
            mco.Close(timeout);
        });

        // *** VALIDATE *** \\
        Assert.True(String.Equals(exceptionMessage, actualException.Message),
                    String.Format("Expected exception message '{0}' but actual was '{1}'",
                                  exceptionMessage, actualException.Message));
    }
Ejemplo n.º 7
0
    public static void CommunicationObject_Sync_Open_Close_States_Transition()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        TimeSpan timeout            = TimeSpan.FromSeconds(30);
        CommunicationStateData data = new CommunicationStateData();

        // *** SETUP *** \\
        InterceptAllStateChanges(mco, data);

        // *** EXECUTE *** \\
        mco.Open(timeout);
        mco.Close(timeout);

        // *** VALIDATE *** \\
        Assert.True(data.StateAfterCreate == CommunicationState.Created,
                    String.Format("CommunicationState after creation was '{0}' but expected 'Created'",
                                  data.StateAfterCreate));

        Assert.True(data.StateEnterOnOpening == CommunicationState.Opening,
                    String.Format("CommunicationState entering OnOpening was '{0}' but expected 'Opening'",
                                  data.StateEnterOnOpening));
        Assert.True(data.StateLeaveOnOpening == CommunicationState.Opening,
                    String.Format("CommunicationState leaving OnOpening was '{0}' but expected 'Opening'",
                                  data.StateLeaveOnOpening));

        Assert.True(data.StateEnterOnOpen == CommunicationState.Opening,
                    String.Format("CommunicationState entering OnOpen was '{0}' but expected 'Opening'",
                                  data.StateEnterOnOpen));
        Assert.True(data.StateLeaveOnOpen == CommunicationState.Opening,
                    String.Format("CommunicationState leaving OnOpen was '{0}' but expected 'Opening'",
                                  data.StateLeaveOnOpen));

        Assert.True(data.StateEnterOnOpened == CommunicationState.Opening,
                    String.Format("CommunicationState entering OnOpened was '{0}' but expected 'Opening'",
                                  data.StateEnterOnOpened));
        Assert.True(data.StateLeaveOnOpened == CommunicationState.Opened,
                    String.Format("CommunicationState leaving OnOpened was '{0}' but expected 'Opened'",
                                  data.StateLeaveOnOpened));

        Assert.True(data.StateEnterOnClosing == CommunicationState.Closing,
                    String.Format("CommunicationState entering OnClosing was '{0}' but expected 'Closing'",
                                  data.StateEnterOnClosing));
        Assert.True(data.StateLeaveOnClosing == CommunicationState.Closing,
                    String.Format("CommunicationState leaving OnClosing was '{0}' but expected 'Closing'",
                                  data.StateLeaveOnClosing));

        Assert.True(data.StateEnterOnClose == CommunicationState.Closing,
                    String.Format("CommunicationState entering OnClose was '{0}' but expected 'Closing'",
                                  data.StateEnterOnClose));
        Assert.True(data.StateLeaveOnClose == CommunicationState.Closing,
                    String.Format("CommunicationState leaving OnClose was '{0}' but expected 'Closing'",
                                  data.StateLeaveOnClose));

        Assert.True(data.StateEnterOnClosed == CommunicationState.Closing,
                    String.Format("CommunicationState entering OnClosed was '{0}' but expected 'Closing'",
                                  data.StateEnterOnClosed));
        Assert.True(data.StateLeaveOnClosed == CommunicationState.Closed,
                    String.Format("CommunicationState leaving OnClosed was '{0}' but expected 'Closed'",
                                  data.StateLeaveOnClosed));
    }
Ejemplo n.º 8
0
    public static void CommunicationObject_Async_Open_Close_Events_Fire()
    {
        MockCommunicationObject mco              = new MockCommunicationObject();
        List <string>           openEventsFired  = new List <string>();
        List <string>           closeEventsFired = new List <string>();
        TimeSpan timeout = TimeSpan.FromMinutes(30);

        // *** SETUP *** \\
        InterceptAllOpenEvents(mco, openEventsFired);
        InterceptAllCloseEvents(mco, closeEventsFired);

        // *** EXECUTE *** \\
        IAsyncResult openAr = mco.BeginOpen(timeout, callback: null, state: null);

        mco.OpenAsyncResult.Complete();
        mco.EndOpen(openAr);

        IAsyncResult closeAr = mco.BeginClose(timeout, callback: null, state: null);

        mco.CloseAsyncResult.Complete();
        mco.EndClose(closeAr);

        // *** VALIDATE *** \\
        string expectedOpens = "Opening,Opened";
        string actualOpens   = String.Join(",", openEventsFired);

        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
                    String.Format("Expected open events to be '{0}' but actual was '{1}'.",
                                  expectedOpens, actualOpens));

        string expectedCloses = "Closing,Closed";
        string actualCloses   = String.Join(",", closeEventsFired);

        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
                    String.Format("Expected close events to be '{0}' but actual was '{1}'.",
                                  expectedCloses, actualCloses));
    }
Ejemplo n.º 9
0
    public static void CommunicationObject_Abort_Close_Methods_Called()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        List<string> closeMethodsCalled = new List<string>();
        TimeSpan timeout = TimeSpan.FromSeconds(30);

        // *** SETUP *** \\
        MockCommunicationObject.InterceptAllCloseMethods(mco, closeMethodsCalled);

        // *** EXECUTE *** \\
        mco.Open(timeout);
        mco.Abort();

        // *** VALIDATE *** \\
        string expectedCloses = "OnClosing,OnAbort,OnClosed";
        string actualCloses = String.Join(",", closeMethodsCalled);
        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
               String.Format("Expected close methods to be '{0}' but actual was '{1}'.",
                             expectedCloses, actualCloses));

        Assert.True(mco.State == CommunicationState.Closed,
                String.Format("Expected final state to be 'Closed' but actual was '{0}", mco.State));

    }
Ejemplo n.º 10
0
    public static void CommunicationObject_Abort_Close_Methods_Called()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        List <string>           closeMethodsCalled = new List <string>();
        TimeSpan timeout = TimeSpan.FromSeconds(30);

        // *** SETUP *** \\
        InterceptAllCloseMethods(mco, closeMethodsCalled);

        // *** EXECUTE *** \\
        mco.Open(timeout);
        mco.Abort();

        // *** VALIDATE *** \\
        string expectedCloses = "OnClosing,OnAbort,OnClosed";
        string actualCloses   = String.Join(",", closeMethodsCalled);

        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
                    String.Format("Expected close methods to be '{0}' but actual was '{1}'.",
                                  expectedCloses, actualCloses));

        Assert.True(mco.State == CommunicationState.Closed,
                    String.Format("Expected final state to be 'Closed' but actual was '{0}", mco.State));
    }
Ejemplo n.º 11
0
    // Intercepts all the open and close methods in MockCommunicationObject
    // and records the CommunicationState before and after the default code executes,
    private static void InterceptAllStateChanges(MockCommunicationObject mco, CommunicationStateData data)
    {
        // Immediately capture the current state after initial creation
        data.StateAfterCreate = mco.State;

        mco.OnOpeningOverride = () =>
        {
            data.StateEnterOnOpening = mco.State;
            mco.DefaultOnOpening();
            data.StateLeaveOnOpening = mco.State;
        };

        mco.OnOpenOverride = (TimeSpan t) =>
        {
            data.StateEnterOnOpen = mco.State;
            mco.DefaultOnOpen(t);
            data.StateLeaveOnOpen = mco.State;
        };

        mco.OnBeginOpenOverride = (TimeSpan t, AsyncCallback c, object s) =>
        {
            data.StateEnterOnBeginOpen = mco.State;
            IAsyncResult result = mco.DefaultOnBeginOpen(t, c, s);
            data.StateLeaveOnBeginOpen = mco.State;
            return(result);
        };

        mco.OnOpenedOverride = () =>
        {
            data.StateEnterOnOpened = mco.State;
            mco.DefaultOnOpened();
            data.StateLeaveOnOpened = mco.State;
        };

        mco.OnClosingOverride = () =>
        {
            data.StateEnterOnClosing = mco.State;
            mco.DefaultOnClosing();
            data.StateLeaveOnClosing = mco.State;
        };

        mco.OnCloseOverride = (TimeSpan t) =>
        {
            data.StateEnterOnClose = mco.State;
            mco.DefaultOnClose(t);
            data.StateLeaveOnClose = mco.State;
        };

        mco.OnBeginCloseOverride = (TimeSpan t, AsyncCallback c, object s) =>
        {
            data.StateEnterOnBeginClose = mco.State;
            IAsyncResult result = mco.DefaultOnBeginClose(t, c, s);
            data.StateLeaveOnBeginClose = mco.State;
            return(result);
        };

        mco.OnClosedOverride = () =>
        {
            data.StateEnterOnClosed = mco.State;
            mco.DefaultOnClosed();
            data.StateLeaveOnClosed = mco.State;
        };
    }
Ejemplo n.º 12
0
    public static void CommunicationObject_Async_Open_Close_States_Transition()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        TimeSpan timeout            = TimeSpan.FromMinutes(5);
        CommunicationStateData data = new CommunicationStateData();

        // *** SETUP *** \\
        InterceptAllStateChanges(mco, data);

        // *** EXECUTE *** \\
        IAsyncResult openAr = mco.BeginOpen(timeout, callback: null, state: null);

        mco.OpenAsyncResult.Complete();
        mco.EndOpen(openAr);

        IAsyncResult closeAr = mco.BeginClose(timeout, callback: null, state: null);

        mco.CloseAsyncResult.Complete();
        mco.EndClose(closeAr);

        // *** VALIDATE *** \\
        Assert.True(data.StateAfterCreate == CommunicationState.Created,
                    String.Format("CommunicationState after creation was '{0}' but expected 'Created'",
                                  data.StateAfterCreate));

        Assert.True(data.StateEnterOnOpening == CommunicationState.Opening,
                    String.Format("CommunicationState entering OnOpening was '{0}' but expected 'Opening'",
                                  data.StateEnterOnOpening));
        Assert.True(data.StateLeaveOnOpening == CommunicationState.Opening,
                    String.Format("CommunicationState leaving OnOpening was '{0}' but expected 'Opening'",
                                  data.StateLeaveOnOpening));

        Assert.True(data.StateEnterOnBeginOpen == CommunicationState.Opening,
                    String.Format("CommunicationState entering OnBeginOpen was '{0}' but expected 'Opening'",
                                  data.StateEnterOnBeginOpen));
        Assert.True(data.StateLeaveOnBeginOpen == CommunicationState.Opening,
                    String.Format("CommunicationState leaving OnBeginOpen was '{0}' but expected 'Opening'",
                                  data.StateLeaveOnBeginOpen));

        Assert.True(data.StateEnterOnOpened == CommunicationState.Opening,
                    String.Format("CommunicationState entering OnOpened was '{0}' but expected 'Opening'",
                                  data.StateEnterOnOpened));
        Assert.True(data.StateLeaveOnOpened == CommunicationState.Opened,
                    String.Format("CommunicationState leaving OnOpened was '{0}' but expected 'Opened'",
                                  data.StateLeaveOnOpened));

        Assert.True(data.StateEnterOnClosing == CommunicationState.Closing,
                    String.Format("CommunicationState entering OnClosing was '{0}' but expected 'Closing'",
                                  data.StateEnterOnClosing));
        Assert.True(data.StateLeaveOnClosing == CommunicationState.Closing,
                    String.Format("CommunicationState leaving OnClosing was '{0}' but expected 'Closing'",
                                  data.StateLeaveOnClosing));

        Assert.True(data.StateEnterOnBeginClose == CommunicationState.Closing,
                    String.Format("CommunicationState entering OnBeginClose was '{0}' but expected 'Closing'",
                                  data.StateEnterOnBeginClose));
        Assert.True(data.StateLeaveOnBeginClose == CommunicationState.Closing,
                    String.Format("CommunicationState leaving OnClose was '{0}' but expected 'Closing'",
                                  data.StateLeaveOnBeginClose));

        Assert.True(data.StateEnterOnClosed == CommunicationState.Closing,
                    String.Format("CommunicationState entering OnClosed was '{0}' but expected 'Closing'",
                                  data.StateEnterOnClosed));
        Assert.True(data.StateLeaveOnClosed == CommunicationState.Closed,
                    String.Format("CommunicationState leaving OnClosed was '{0}' but expected 'Closed'",
                                  data.StateLeaveOnClosed));
    }
Ejemplo n.º 13
0
 // Intercepts all the events expected to fire during an open
 private static void InterceptAllOpenEvents(MockCommunicationObject mco, List <string> eventsFired)
 {
     mco.Opening += (s, ea) => eventsFired.Add("Opening");
     mco.Opened  += (s, ea) => eventsFired.Add("Opened");
 }
Ejemplo n.º 14
0
 // Intercepts all the events expected to fire during a close
 private static void InterceptAllCloseEvents(MockCommunicationObject mco, List <string> eventsFired)
 {
     mco.Closing += (s, ea) => eventsFired.Add("Closing");
     mco.Closed  += (s, ea) => eventsFired.Add("Closed");
 }
Ejemplo n.º 15
0
    public static void CommunicationObject_Async_Open_Close_States_Transition()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        TimeSpan timeout = TimeSpan.FromMinutes(5);
        CommunicationStateData data = new CommunicationStateData();

        // *** SETUP *** \\
        MockCommunicationObject.InterceptAllStateChanges(mco, data);

        // *** EXECUTE *** \\
        IAsyncResult openAr = mco.BeginOpen(timeout, callback: null, state: null);
        mco.OpenAsyncResult.Complete();
        mco.EndOpen(openAr);

        IAsyncResult closeAr = mco.BeginClose(timeout, callback: null, state: null);
        mco.CloseAsyncResult.Complete();
        mco.EndClose(closeAr);

        // *** VALIDATE *** \\
        Assert.True(data.StateAfterCreate == CommunicationState.Created,
                    String.Format("CommunicationState after creation was '{0}' but expected 'Created'",
                        data.StateAfterCreate));

        Assert.True(data.StateEnterOnOpening == CommunicationState.Opening,
                    String.Format("CommunicationState entering OnOpening was '{0}' but expected 'Opening'",
                        data.StateEnterOnOpening));
        Assert.True(data.StateLeaveOnOpening == CommunicationState.Opening,
                    String.Format("CommunicationState leaving OnOpening was '{0}' but expected 'Opening'",
                        data.StateLeaveOnOpening));

        Assert.True(data.StateEnterOnBeginOpen == CommunicationState.Opening,
                    String.Format("CommunicationState entering OnBeginOpen was '{0}' but expected 'Opening'",
                        data.StateEnterOnBeginOpen));
        Assert.True(data.StateLeaveOnBeginOpen == CommunicationState.Opening,
                    String.Format("CommunicationState leaving OnBeginOpen was '{0}' but expected 'Opening'",
                        data.StateLeaveOnBeginOpen));

        Assert.True(data.StateEnterOnOpened == CommunicationState.Opening,
                    String.Format("CommunicationState entering OnOpened was '{0}' but expected 'Opening'",
                        data.StateEnterOnOpened));
        Assert.True(data.StateLeaveOnOpened == CommunicationState.Opened,
                    String.Format("CommunicationState leaving OnOpened was '{0}' but expected 'Opened'",
                        data.StateLeaveOnOpened));

        Assert.True(data.StateEnterOnClosing == CommunicationState.Closing,
                    String.Format("CommunicationState entering OnClosing was '{0}' but expected 'Closing'",
                        data.StateEnterOnClosing));
        Assert.True(data.StateLeaveOnClosing == CommunicationState.Closing,
                    String.Format("CommunicationState leaving OnClosing was '{0}' but expected 'Closing'",
                        data.StateLeaveOnClosing));

        Assert.True(data.StateEnterOnBeginClose == CommunicationState.Closing,
                    String.Format("CommunicationState entering OnBeginClose was '{0}' but expected 'Closing'",
                        data.StateEnterOnBeginClose));
        Assert.True(data.StateLeaveOnBeginClose == CommunicationState.Closing,
                    String.Format("CommunicationState leaving OnClose was '{0}' but expected 'Closing'",
                        data.StateLeaveOnBeginClose));

        Assert.True(data.StateEnterOnClosed == CommunicationState.Closing,
                    String.Format("CommunicationState entering OnClosed was '{0}' but expected 'Closing'",
                        data.StateEnterOnClosed));
        Assert.True(data.StateLeaveOnClosed == CommunicationState.Closed,
                    String.Format("CommunicationState leaving OnClosed was '{0}' but expected 'Closed'",
                        data.StateLeaveOnClosed));
    }
Ejemplo n.º 16
0
    public static void CommunicationObject_Sync_Close_Propagates_Exception()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        TimeSpan timeout = TimeSpan.FromSeconds(30);
        string exceptionMessage = "Expected exception";

        // *** SETUP *** \\
        mco.OnCloseOverride = (t) =>
        {
            throw new InvalidOperationException(exceptionMessage);
        };

        // *** EXECUTE *** \\
        mco.Open(timeout);

        InvalidOperationException actualException = Assert.Throws<InvalidOperationException>(() =>
        {
            mco.Close(timeout);
        });

        // *** VALIDATE *** \\
        Assert.True(String.Equals(exceptionMessage, actualException.Message),
                    String.Format("Expected exception message '{0}' but actual was '{1}'",
                                  exceptionMessage, actualException.Message));
    }
Ejemplo n.º 17
0
    public static void CommunicationObject_Async_Close_Propagates_Exception()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        TimeSpan timeout = TimeSpan.FromSeconds(30);
        string exceptionMessage = "Expected exception";

        // *** SETUP *** \\
        mco.OnBeginCloseOverride = (TimeSpan t, AsyncCallback c, object s) =>
        {
            throw new InvalidOperationException(exceptionMessage);
        };

        // *** EXECUTE *** \\
        IAsyncResult openAr = mco.BeginOpen(timeout, callback: null, state: null);
        mco.OpenAsyncResult.Complete();
        mco.EndOpen(openAr);

        InvalidOperationException actualException = Assert.Throws<InvalidOperationException>(() =>
        {
            IAsyncResult closeAr = mco.BeginClose(timeout, callback: null, state: null);
            mco.CloseAsyncResult.Complete();
            mco.EndClose(closeAr);
        });

        // *** VALIDATE *** \\
        Assert.True(String.Equals(exceptionMessage, actualException.Message),
                    String.Format("Expected exception message '{0}' but actual was '{1}'",
                                  exceptionMessage, actualException.Message));
    }
Ejemplo n.º 18
0
    public static void CustomChannel_Async_Open_Close_Methods_Called()
    {
        MockChannelFactory <IRequestChannel> mockChannelFactory = null;
        MockRequestChannel mockRequestChannel        = null;
        List <string>      channelOpenMethodsCalled  = new List <string>();
        List <string>      channelCloseMethodsCalled = new List <string>();
        List <string>      factoryOpenMethodsCalled  = new List <string>();
        List <string>      factoryCloseMethodsCalled = new List <string>();
        string             testMessageBody           = "CustomChannelTest_Async";
        Message            inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        // Intercept the creation of the factory so we can intercept creation of the channel
        Func <Type, BindingContext, IChannelFactory> buildFactoryAction = (Type type, BindingContext context) =>
        {
            // Create the channel factory and intercept all open and close method calls
            mockChannelFactory = new MockChannelFactory <IRequestChannel>(context, new TextMessageEncodingBindingElement().CreateMessageEncoderFactory());
            MockCommunicationObject.InterceptAllOpenMethods(mockChannelFactory, factoryOpenMethodsCalled);
            MockCommunicationObject.InterceptAllCloseMethods(mockChannelFactory, factoryCloseMethodsCalled);

            // Override the OnCreateChannel call so we get the mock channel created by the factory
            mockChannelFactory.OnCreateChannelOverride = (EndpointAddress endpoint, Uri via) =>
            {
                // Create the default mock channel and intercept all its open and close method calls
                mockRequestChannel = (MockRequestChannel)mockChannelFactory.DefaultOnCreateChannel(endpoint, via);
                MockCommunicationObject.InterceptAllOpenMethods(mockRequestChannel, channelOpenMethodsCalled);
                MockCommunicationObject.InterceptAllCloseMethods(mockRequestChannel, channelCloseMethodsCalled);

                return(mockRequestChannel);
            };
            return(mockChannelFactory);
        };

        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();

        mockBindingElement.BuildChannelFactoryOverride = buildFactoryAction;

        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);

        // Explicitly open factory asynchronously because the implicit open is synchronous
        IAsyncResult openResult = factory.BeginOpen(null, null);
        Task         openTask   = Task.Factory.FromAsync(openResult, factory.EndOpen);

        openTask.GetAwaiter().GetResult();

        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        Task <Message> processTask = channel.ProcessAsync(inputMessage);

        // The mock's default behavior is just to loopback what we sent.
        Message outputMessage = processTask.GetAwaiter().GetResult();
        var     result        = outputMessage.GetBody <string>();

        // Explicitly close the channel factory asynchronously.
        // One of the important aspects of this test is that an asynchronous
        // close of the factory also asynchronously closes the channel.
        IAsyncResult asyncResult = factory.BeginClose(null, null);
        Task         task        = Task.Factory.FromAsync(asyncResult, factory.EndClose);

        task.GetAwaiter().GetResult();

        // *** VALIDATE *** \\
        Assert.True(String.Equals(testMessageBody, result),
                    String.Format("Expected body to be '{0}' but actual was '{1}'", testMessageBody, result));

        string expectedOpens  = "OnOpening,OnBeginOpen,OnOpened";
        string expectedCloses = "OnClosing,OnBeginClose,OnClosed";

        string actualOpens = String.Join(",", channelOpenMethodsCalled);

        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
                    String.Format("Expected channel open methods to be '{0}' but actual was '{1}'.",
                                  expectedOpens, actualOpens));

        string actualCloses = String.Join(",", channelCloseMethodsCalled);

        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
                    String.Format("Expected channel close methods to be '{0}' but actual was '{1}'.",
                                  expectedCloses, actualCloses));

        actualOpens = String.Join(",", factoryOpenMethodsCalled);
        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
                    String.Format("Expected factory open methods to be '{0}' but actual was '{1}'.",
                                  expectedOpens, actualOpens));

        actualCloses = String.Join(",", factoryCloseMethodsCalled);
        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
                    String.Format("Expected factory close methods to be '{0}' but actual was '{1}'.",
                                  expectedCloses, actualCloses));

        Assert.True(factory.State == CommunicationState.Closed,
                    String.Format("Expected factory's final state to be Closed but was '{0}'", factory.State));

        Assert.True(((ICommunicationObject)channel).State == CommunicationState.Closed,
                    String.Format("Expected channel's final state to be Closed but was '{0}'", ((ICommunicationObject)channel).State));
    }
Ejemplo n.º 19
0
    public static void CommunicationObject_Sync_Open_Close_States_Transition()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        TimeSpan timeout = TimeSpan.FromSeconds(30);
        CommunicationStateData data = new CommunicationStateData();

        // *** SETUP *** \\
        MockCommunicationObject.InterceptAllStateChanges(mco, data);

        // *** EXECUTE *** \\
        mco.Open(timeout);
        mco.Close(timeout);

        // *** VALIDATE *** \\
        Assert.True(data.StateAfterCreate == CommunicationState.Created,
                    String.Format("CommunicationState after creation was '{0}' but expected 'Created'",
                        data.StateAfterCreate));

        Assert.True(data.StateEnterOnOpening == CommunicationState.Opening,
                    String.Format("CommunicationState entering OnOpening was '{0}' but expected 'Opening'",
                        data.StateEnterOnOpening));
        Assert.True(data.StateLeaveOnOpening == CommunicationState.Opening,
                    String.Format("CommunicationState leaving OnOpening was '{0}' but expected 'Opening'",
                        data.StateLeaveOnOpening));

        Assert.True(data.StateEnterOnOpen == CommunicationState.Opening,
                    String.Format("CommunicationState entering OnOpen was '{0}' but expected 'Opening'",
                        data.StateEnterOnOpen));
        Assert.True(data.StateLeaveOnOpen == CommunicationState.Opening,
                    String.Format("CommunicationState leaving OnOpen was '{0}' but expected 'Opening'",
                        data.StateLeaveOnOpen));

        Assert.True(data.StateEnterOnOpened == CommunicationState.Opening,
                    String.Format("CommunicationState entering OnOpened was '{0}' but expected 'Opening'",
                        data.StateEnterOnOpened));
        Assert.True(data.StateLeaveOnOpened == CommunicationState.Opened,
                    String.Format("CommunicationState leaving OnOpened was '{0}' but expected 'Opened'",
                        data.StateLeaveOnOpened));

        Assert.True(data.StateEnterOnClosing == CommunicationState.Closing,
                    String.Format("CommunicationState entering OnClosing was '{0}' but expected 'Closing'",
                        data.StateEnterOnClosing));
        Assert.True(data.StateLeaveOnClosing == CommunicationState.Closing,
                    String.Format("CommunicationState leaving OnClosing was '{0}' but expected 'Closing'",
                        data.StateLeaveOnClosing));

        Assert.True(data.StateEnterOnClose == CommunicationState.Closing,
                    String.Format("CommunicationState entering OnClose was '{0}' but expected 'Closing'",
                        data.StateEnterOnClose));
        Assert.True(data.StateLeaveOnClose == CommunicationState.Closing,
                    String.Format("CommunicationState leaving OnClose was '{0}' but expected 'Closing'",
                        data.StateLeaveOnClose));

        Assert.True(data.StateEnterOnClosed == CommunicationState.Closing,
                    String.Format("CommunicationState entering OnClosed was '{0}' but expected 'Closing'",
                        data.StateEnterOnClosed));
        Assert.True(data.StateLeaveOnClosed == CommunicationState.Closed,
                    String.Format("CommunicationState leaving OnClosed was '{0}' but expected 'Closed'",
                        data.StateLeaveOnClosed));
    }
Ejemplo n.º 20
0
    public static void CustomChannel_Factory_Abort_Aborts_Channel()
    {
        MockChannelFactory <IRequestChannel> mockChannelFactory = null;
        MockRequestChannel mockRequestChannel        = null;
        List <string>      channelOpenMethodsCalled  = new List <string>();
        List <string>      channelCloseMethodsCalled = new List <string>();
        List <string>      factoryOpenMethodsCalled  = new List <string>();
        List <string>      factoryCloseMethodsCalled = new List <string>();
        string             testMessageBody           = "CustomChannelTest_Sync";
        Message            inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        // Intercept the creation of the factory so we can intercept creation of the channel
        Func <Type, BindingContext, IChannelFactory> buildFactoryAction = (Type type, BindingContext context) =>
        {
            // Create the channel factory and intercept all open and close method calls
            mockChannelFactory = new MockChannelFactory <IRequestChannel>(context, new TextMessageEncodingBindingElement().CreateMessageEncoderFactory());
            MockCommunicationObject.InterceptAllOpenMethods(mockChannelFactory, factoryOpenMethodsCalled);
            MockCommunicationObject.InterceptAllCloseMethods(mockChannelFactory, factoryCloseMethodsCalled);

            // Override the OnCreateChannel call so we get the mock channel created by the factory
            mockChannelFactory.OnCreateChannelOverride = (EndpointAddress endpoint, Uri via) =>
            {
                // Create the mock channel and intercept all its open and close method calls
                mockRequestChannel = (MockRequestChannel)mockChannelFactory.DefaultOnCreateChannel(endpoint, via);
                MockCommunicationObject.InterceptAllOpenMethods(mockRequestChannel, channelOpenMethodsCalled);
                MockCommunicationObject.InterceptAllCloseMethods(mockRequestChannel, channelCloseMethodsCalled);
                return(mockRequestChannel);
            };
            return(mockChannelFactory);
        };

        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();

        mockBindingElement.BuildChannelFactoryOverride = buildFactoryAction;

        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);

        // We rely on the implicit open of the channel to be synchronous.
        // This is true for both the full framework and this NET Core version.
        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        Message outputMessage = channel.Process(inputMessage);

        // The mock's default behavior is just to loopback what we sent.
        var result = outputMessage.GetBody <string>();

        // Abort the factory and expect both factory and channel to be aborted.
        factory.Abort();

        // *** VALIDATE *** \\
        Assert.True(String.Equals(testMessageBody, result),
                    String.Format("Expected body to be '{0}' but actual was '{1}'", testMessageBody, result));

        string expectedOpens  = "OnOpening,OnOpen,OnOpened";
        string expectedCloses = "OnClosing,OnAbort,OnClosed";

        string actualOpens = String.Join(",", channelOpenMethodsCalled);

        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
                    String.Format("Expected channel open methods to be '{0}' but actual was '{1}'.",
                                  expectedOpens, actualOpens));

        string actualCloses = String.Join(",", channelCloseMethodsCalled);

        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
                    String.Format("Expected channel close methods to be '{0}' but actual was '{1}'.",
                                  expectedCloses, actualCloses));

        actualOpens = String.Join(",", factoryOpenMethodsCalled);
        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
                    String.Format("Expected factory open methods to be '{0}' but actual was '{1}'.",
                                  expectedOpens, actualOpens));

        actualCloses = String.Join(",", factoryCloseMethodsCalled);
        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
                    String.Format("Expected factory close methods to be '{0}' but actual was '{1}'.",
                                  expectedCloses, actualCloses));

        Assert.True(factory.State == CommunicationState.Closed,
                    String.Format("Expected factory's final state to be Closed but was '{0}'", factory.State));

        Assert.True(((ICommunicationObject)channel).State == CommunicationState.Closed,
                    String.Format("Expected channel's final state to be Closed but was '{0}'", ((ICommunicationObject)channel).State));
    }
Ejemplo n.º 21
0
    public static void CommunicationObject_Sync_Open_Close_Events_Fire()
    {
        MockCommunicationObject mco = new MockCommunicationObject();
        List<string> openEventsFired = new List<string>();
        List<string> closeEventsFired = new List<string>();
        TimeSpan timeout = TimeSpan.FromSeconds(30);

        // *** SETUP *** \\
        MockCommunicationObject.InterceptAllOpenEvents(mco, openEventsFired);
        MockCommunicationObject.InterceptAllCloseEvents(mco, closeEventsFired);

        // *** EXECUTE *** \\
        mco.Open(timeout);
        mco.Close(timeout);

        // *** VALIDATE *** \\
        string expectedOpens = "Opening,Opened";
        string actualOpens = String.Join(",", openEventsFired);
        Assert.True(String.Equals(expectedOpens, actualOpens, StringComparison.Ordinal),
               String.Format("Expected open events to be '{0}' but actual was '{1}'.",
                             expectedOpens, actualOpens));

        string expectedCloses = "Closing,Closed";
        string actualCloses = String.Join(",", closeEventsFired);
        Assert.True(String.Equals(expectedCloses, actualCloses, StringComparison.Ordinal),
               String.Format("Expected close events to be '{0}' but actual was '{1}'.",
                             expectedCloses, actualCloses));
    }