@Test
  public void readWithTimeoutInputStreamTest() throws IOException, InterruptedException {
    int streamDelayTooLong = 200;
    int streamDelayOk = 5;
    int timeout = 100;
    int cpuDelay = 10;

    InputStream stream = org.mockito.Mockito.mock(InputStream.class);
    byte[] bytes = "hello".getBytes();
    byte[] bytes2 = "worlds".getBytes();
    byte[] checkBytes = new byte[bytes.length];
    byte[] checkBytes2 = new byte[bytes2.length];

    Mockito.when(stream.available())
        .thenReturn(0)
        .thenReturn(bytes.length)
        .thenReturn(bytes2.length)
        .thenReturn(0);
    Mockito.when(
            stream.read(
                Mockito.any(byte[].class), Mockito.any(Integer.class), Mockito.any(Integer.class)))
        .thenAnswer(new InputStreamReadDelayedAnswer(streamDelayTooLong, new byte[0]))
        .thenAnswer(new InputStreamReadDelayedAnswer(streamDelayTooLong, bytes))
        .thenAnswer(new InputStreamReadDelayedAnswer(streamDelayOk, bytes2));

    int dataRead = IOUtilities.readWithTimeout(stream, checkBytes, timeout, cpuDelay);
    Assert.assertEquals(0, dataRead);

    dataRead = IOUtilities.readWithTimeout(stream, checkBytes, timeout, cpuDelay);
    Assert.assertEquals(bytes.length, dataRead);
    Assert.assertArrayEquals(bytes, checkBytes);

    dataRead = IOUtilities.readWithTimeout(stream, checkBytes2, timeout, cpuDelay);
    Assert.assertArrayEquals(bytes2, checkBytes2);
  }
  @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);
  }