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)); }
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)); }
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 *** \\ InterceptAllOpenMethods(mco, openMethodsCalled); 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)); }
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)); }
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)); }
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)); }