Beispiel #1
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));
    }
    // Intercepts all the open and close methods in MockCommunicationObject
    // and records the CommunicationState before and after the default code executes,
    public static void InterceptAllStateChanges(IMockCommunicationObject 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;
        };

        #endregion helpers
    }
Beispiel #3
0
    // Intercepts all the open and close methods in MockCommunicationObject
    // and records the CommunicationState before and after the default code executes,
    public static void InterceptAllStateChanges(IMockCommunicationObject 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;
        };

        #endregion helpers
    }
Beispiel #4
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));
    }
Beispiel #5
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));
    }
Beispiel #6
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));
    }