@Test public void readWithTimeoutSerialPortTest() throws IOException, InterruptedException { int streamDelayTooLong = 200; int streamDelayOk = 5; int timeout = 100; int cpuDelay = 10; SerialCommunicationsPort serial = org.mockito.Mockito.mock(SerialCommunicationsPort.class); byte[] bytes = "j".getBytes(); byte[] bytes2 = "k".getBytes(); Mockito.when(serial.read()) .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayTooLong, null)) .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayTooLong, bytes)) .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayTooLong, null)) .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayOk, bytes2)) .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayOk, null)); String data = IOUtilities.readWithTimeout(serial, timeout, cpuDelay); Assert.assertEquals("", data); data = IOUtilities.readWithTimeout(serial, timeout, cpuDelay); Assert.assertEquals("j", data); data = IOUtilities.readWithTimeout(serial, timeout, cpuDelay); Assert.assertEquals("k", data); }
@Test public void serialPortSplitReadLineTest() throws IOException { int streamDelayTooLong = 100; int timeout = 50; int cpuDelay = 10; StringBuilder builder = new StringBuilder(); SerialCommunicationsPort serial = org.mockito.Mockito.mock(SerialCommunicationsPort.class); Printer printer = org.mockito.Mockito.mock(Printer.class); byte[] bytes = "o".getBytes(); byte[] bytes2 = "k\nwo".getBytes(); byte[] bytes3 = "rl".getBytes(); byte[] bytes4 = "d\n".getBytes(); Mockito.when(serial.read()) .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayTooLong, bytes)) .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayTooLong, bytes2)) .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayTooLong, bytes3)) .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayTooLong, bytes4)) .thenThrow( new IllegalArgumentException( "The read method should never have been called this time.")); // This tests that an inputstream read wasn't able to complete a full line read in the given // timeout period. ParseState state = IOUtilities.readLine(printer, serial, builder, 0, timeout, cpuDelay); Assert.assertEquals(1, state.parseLocation); Assert.assertEquals(null, state.currentLine); // The next read should be able to read the the ok state = IOUtilities.readLine(printer, serial, builder, state.parseLocation, timeout, cpuDelay); Assert.assertEquals(0, state.parseLocation); Assert.assertEquals("ok\n", state.currentLine); // Again, this read won't be able to read the full amount of data in the timeout period. state = IOUtilities.readLine(printer, serial, builder, state.parseLocation, timeout, cpuDelay); Assert.assertEquals(4, state.parseLocation); Assert.assertEquals(null, state.currentLine); // There is nothing left on the stream to read so it continues to parse the rest of the // StringBuilder state = IOUtilities.readLine(printer, serial, builder, state.parseLocation, timeout, cpuDelay); Assert.assertEquals(0, state.parseLocation); Assert.assertEquals("world\n", state.currentLine); }