private static void SetupSuccessfulConnectionExpectations(ITcpClient tcpClient, INetworkStream stream, bool withPassword = true) { // client not connected tcpClient.Expect(x => x.Client).PropertyBehavior(); // setup connect expectations var asyncResult = MockRepository.GenerateStub <IAsyncResult>(); tcpClient.Expect(x => x.BeginConnect("server", 10000, null, null)).Return(asyncResult); asyncResult.Stub(x => x.AsyncWaitHandle).Return(new EventWaitHandle(true, EventResetMode.ManualReset)); tcpClient.Expect(x => x.EndConnect(asyncResult)).Do(new Action <IAsyncResult>(ar => { // setup Connected property expectations tcpClient.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); tcpClient.Expect(x => x.Connected).Return(true).Repeat.AtLeastOnce(); })); tcpClient.Expect(x => x.GetStream()).Return(stream); if (withPassword) { // setup SendCommand() expectation stream.Expect(x => x.BeginWrite(null, 0, 0, null, null)).IgnoreArguments().Do( new Func <byte[], int, int, AsyncCallback, object, IAsyncResult>(TestUtilities.DoBeginWrite)).Repeat.AtLeastOnce(); } }
public void UpdateTest1() { using (var messageCache = new MessageCache(CreateClientFactory())) { Connect(messageCache); var buffer = messageCache.InternalBuffer; _stream.Expect(x => x.Read(buffer, 0, buffer.Length)).Do( new Func <byte[], int, int, int>(ConnectionTests.FillBufferWithTestData)); MessageUpdatedEventArgs e = null; messageCache.MessageUpdated += (sender, args) => e = args; bool updateFinishedRaised = false; messageCache.UpdateFinished += delegate { updateFinishedRaised = true; }; messageCache.SocketTimerElapsed(null, null); Assert.AreEqual("units", e.Key); Assert.IsNull(e.DataType); Assert.IsTrue(updateFinishedRaised); Assert.IsNotNull(messageCache.GetJsonMessage(JsonMessageKey.QueueInfo)); } _tcpClient.VerifyAllExpectations(); _stream.VerifyAllExpectations(); }
public void CloseTest() { using (var connection = new Connection(CreateClientFactory())) { Connect(connection); _stream.Expect(x => x.Close()); _tcpClient.Expect(x => x.Close()); bool statusMessageFired = false; connection.StatusMessage += (sender, args) => statusMessageFired = true; connection.Close(); Assert.IsTrue(statusMessageFired); } _tcpClient.VerifyAllExpectations(); _stream.VerifyAllExpectations(); }
private static void SetupSuccessfulConnectionExpectations(ITcpClient tcpClient, INetworkStream stream, bool withPassword = true) { // client not connected tcpClient.Expect(x => x.Client).Return(null).Repeat.Once(); // setup connect expectations var asyncResult = MockRepository.GenerateStub <IAsyncResult>(); tcpClient.Expect(x => x.BeginConnect("server", 10000, null, null)).Return(asyncResult); asyncResult.Stub(x => x.AsyncWaitHandle).Return(new EventWaitHandle(true, EventResetMode.ManualReset)); tcpClient.Expect(x => x.EndConnect(asyncResult)); tcpClient.Expect(x => x.GetStream()).Return(stream); // setup Connected property expectations tcpClient.Expect(x => x.Client).Return(new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)).Repeat.AtLeastOnce(); tcpClient.Expect(x => x.Connected).Return(true).Repeat.AtLeastOnce(); if (withPassword) { // setup SendCommand() expectation var buffer = Encoding.ASCII.GetBytes("auth password\n"); stream.Expect(x => x.Write(buffer, 0, buffer.Length)); } }
public void GetHeartbeatTest() { using (var fahClient = new FahClient(CreateClientFactory())) { Connect(fahClient); var buffer = fahClient.InternalBuffer; _stream.Expect(x => x.Read(buffer, 0, buffer.Length)).Do( new Func <byte[], int, int, int>(FillBufferWithHeartbeatData)); MessageUpdatedEventArgs e = null; fahClient.MessageUpdated += (sender, args) => e = args; fahClient.SocketTimerElapsed(null, null); Assert.AreEqual(JsonMessageKey.Heartbeat, e.Key); Assert.AreEqual(typeof(Heartbeat), e.DataType); var heartbeat = fahClient.GetMessage <Heartbeat>(); Assert.IsNotNull(heartbeat); Assert.AreEqual(0, heartbeat.Errors.Count()); } _tcpClient.VerifyAllExpectations(); _stream.VerifyAllExpectations(); }
private static void SetupSuccessfulConnectionExpectations(ITcpClient tcpClient, INetworkStream stream, bool withPassword = true) { // client not connected tcpClient.Expect(x => x.Client).Return(null); // setup connect expectations var asyncResult = MockRepository.GenerateStub<IAsyncResult>(); tcpClient.Expect(x => x.BeginConnect("server", 10000, null, null)).Return(asyncResult); asyncResult.Stub(x => x.AsyncWaitHandle).Return(new EventWaitHandle(true, EventResetMode.ManualReset)); tcpClient.Expect(x => x.EndConnect(asyncResult)); tcpClient.Expect(x => x.GetStream()).Return(stream); // setup Connected property expectations tcpClient.Expect(x => x.Client).Return(new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)).Repeat.AtLeastOnce(); tcpClient.Expect(x => x.Connected).Return(true).Repeat.AtLeastOnce(); if (withPassword) { // setup SendCommand() expectation var buffer = Encoding.ASCII.GetBytes("auth password\n"); stream.Expect(x => x.Write(buffer, 0, buffer.Length)); } }
private static void SetupSuccessfulConnectionExpectations(ITcpClient tcpClient, INetworkStream stream, bool withPassword = true) { // client not connected tcpClient.Expect(x => x.Client).PropertyBehavior(); // setup connect expectations var asyncResult = MockRepository.GenerateStub<IAsyncResult>(); tcpClient.Expect(x => x.BeginConnect("server", 10000, null, null)).Return(asyncResult); asyncResult.Stub(x => x.AsyncWaitHandle).Return(new EventWaitHandle(true, EventResetMode.ManualReset)); tcpClient.Expect(x => x.EndConnect(asyncResult)).Do(new Action<IAsyncResult>(ar => { // setup Connected property expectations tcpClient.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); tcpClient.Expect(x => x.Connected).Return(true).Repeat.AtLeastOnce(); })); tcpClient.Expect(x => x.GetStream()).Return(stream); if (withPassword) { // setup SendCommand() expectation stream.Expect(x => x.BeginWrite(null, 0, 0, null, null)).IgnoreArguments().Do( new Func<byte[], int, int, AsyncCallback, object, IAsyncResult>(TestUtilities.DoBeginWrite)).Repeat.AtLeastOnce(); } }