public void UnicastMessage_ReReadsIfValidateResponseIsFalse() { MockRepository mocks = new MockRepository(); ModbusTransport transport = mocks.PartialMock <ModbusTransport>(); transport.WaitToRetryMilliseconds = 5; transport.Write(null); LastCall.IgnoreArguments(); Expect.Call(transport.ReadResponse <ReadHoldingInputRegistersResponse>()) .Return(new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1))) .Repeat.Times(2) .Message("ReadResponse should be called twice, one for the retry"); Expect.Call(transport.OnValidateResponse(null, null)) .IgnoreArguments() .Return(false) .Repeat.Times(1); Expect.Call(transport.OnValidateResponse(null, null)) .IgnoreArguments() .Return(true) .Repeat.Times(1); mocks.ReplayAll(); ReadHoldingInputRegistersRequest request = new ReadHoldingInputRegistersRequest(Modbus.ReadHoldingRegisters, 1, 1, 1); ReadHoldingInputRegistersResponse response = transport.UnicastMessage <ReadHoldingInputRegistersResponse>(request); mocks.VerifyAll(); }
public void UnicastMessage_SingleFailingException(Type exceptionType) { MockRepository mocks = new MockRepository(); ModbusTransport transport = mocks.PartialMock <ModbusTransport>(); transport.Retries = 1; transport.Write(null); LastCall.IgnoreArguments().Repeat.Times(2); Expect.Call(transport.ReadResponse <ReadCoilsInputsResponse>()) .Do((ThrowExceptionDelegate) delegate { throw (Exception)Activator.CreateInstance(exceptionType); }); Expect.Call(transport.ReadResponse <ReadCoilsInputsResponse>()) .Return(new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1, new DiscreteCollection(true, false, true, false, false, false, false, false))); transport.OnValidateResponse(null, null); LastCall.IgnoreArguments(); mocks.ReplayAll(); ReadCoilsInputsRequest request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 2, 3, 4); transport.UnicastMessage <ReadCoilsInputsResponse>(request); mocks.VerifyAll(); }
public void UnicastMessage() { MockRepository mocks = new MockRepository(); ModbusTransport transport = mocks.PartialMock <ModbusTransport>(); transport.Write(null); LastCall.IgnoreArguments(); // read 4 coils from slave id 2 Expect.Call(transport.ReadResponse <ReadCoilsInputsResponse>()) .Return(new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1, new DiscreteCollection(true, false, true, false, false, false, false, false))); transport.OnValidateResponse(null, null); LastCall.IgnoreArguments(); mocks.ReplayAll(); ReadCoilsInputsRequest request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 2, 3, 4); ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1, new DiscreteCollection(true, false, true, false, false, false, false, false)); ReadCoilsInputsResponse response = transport.UnicastMessage <ReadCoilsInputsResponse>(request); Assert.Equal(expectedResponse.MessageFrame, response.MessageFrame); mocks.VerifyAll(); }
public void UnicastMessage_SlaveDeviceBusySlaveExceptionDoesNotFailAfterExceedingRetries() { MockRepository mocks = new MockRepository(); ModbusTransport transport = mocks.PartialMock <ModbusTransport>(); // set the wait to retry property to a small value so the test completes quickly transport.WaitToRetryMilliseconds = 5; transport.Write(null); LastCall.IgnoreArguments() .Repeat.Times(transport.Retries + 1); // return a slave exception a greater number of times than number of retries to make sure we aren't just retrying Expect.Call(transport.ReadResponse <ReadHoldingInputRegistersResponse>()) .Return(new SlaveExceptionResponse(1, Modbus.ReadHoldingRegisters + Modbus.ExceptionOffset, Modbus.SlaveDeviceBusy)) .Repeat.Times(transport.Retries); Expect.Call(transport.ReadResponse <ReadHoldingInputRegistersResponse>()) .Return(new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1))); Expect.Call(transport.OnValidateResponse(null, null)) .IgnoreArguments() .Return(true); mocks.ReplayAll(); ReadHoldingInputRegistersRequest request = new ReadHoldingInputRegistersRequest(Modbus.ReadHoldingRegisters, 1, 1, 1); ReadHoldingInputRegistersResponse expectedResponse = new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1)); ReadHoldingInputRegistersResponse response = transport.UnicastMessage <ReadHoldingInputRegistersResponse>(request); Assert.AreEqual(expectedResponse.MessageFrame, response.MessageFrame); mocks.VerifyAll(); }
public void ValidateResponse_CallsOnValidateResponse() { MockRepository mocks = new MockRepository(); ModbusTransport transport = mocks.PartialMock <ModbusTransport>(); IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1); IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null); transport.OnValidateResponse(null, null); LastCall.IgnoreArguments(); mocks.ReplayAll(); transport.ValidateResponse(request, response); mocks.VerifyAll(); }
public void ValidateResponse_CallsOnValidateResponse() { MockRepository mocks = new MockRepository(); ModbusTransport transport = mocks.PartialMock <ModbusTransport>(); IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1); IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null); Expect.Call(transport.OnValidateResponse(request, response)) .Return(true); mocks.ReplayAll(); transport.ValidateResponse(request, response); mocks.VerifyAll(); }