improved exception type also for read with timeout

pull/540/head
kai-morich 2023-08-24 19:51:47 +02:00
rodzic 399d3c9c2f
commit 35fdeb1e13
4 zmienionych plików z 57 dodań i 6 usunięć

Wyświetl plik

@ -13,7 +13,7 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments = [ // Raspi Windows LinuxVM ...
'rfc2217_server_host': '192.168.0.145',
'rfc2217_server_host': '192.168.0.143',
'rfc2217_server_nonstandard_baudrates': 'true', // true false false
]
}

Wyświetl plik

@ -253,13 +253,15 @@ public class DeviceTest {
}
try {
usb.write(new byte[]{0x00});
fail("write error expected");
} catch (IOException ignored) {
fail("write closed expected");
} catch(IOException ex) {
assertEquals("Connection closed", ex.getMessage());
}
try {
usb.read(1);
fail("read error expected");
} catch (IOException ignored) {
fail("read closed expected");
} catch(IOException ex) {
assertEquals("Connection closed", ex.getMessage());
}
try {
usb.setParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE);
@ -281,10 +283,56 @@ public class DeviceTest {
break;
Thread.sleep(1);
}
try {
usb.read();
fail("closed expected");
} catch (IOException ex) {
assertEquals("java.io.IOException: Connection closed", ex.getMessage());
}
// assertEquals(SerialInputOutputManager.State.STOPPED, usb.usbIoManager.getState());
// unstable. null'ify not-stopped ioManager, else usbClose would try again
if(SerialInputOutputManager.State.STOPPED != usb.ioManager.getState())
usb.ioManager = null;
usb.close();
// close while waiting in read
class CloseRunnable implements Runnable {
boolean wait;
public void run() {
try {
while(wait)
Thread.sleep(1);
Thread.sleep(5);
} catch (InterruptedException ignored) {
}
Log.d(TAG, "close");
usb.close();
}
}
usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_THREAD));
CloseRunnable closer = new CloseRunnable();
closer.wait = true;
Thread th = new Thread(closer);
th.start();
try {
closer.wait = false;
usb.serialPort.read(new byte[256], 2000);
fail("closed expected");
} catch(IOException ex) {
assertEquals("Connection closed", ex.getMessage());
}
th.join();
closer.wait = true;
th = new Thread(closer);
th.start();
try {
closer.wait = false;
usb.serialPort.read(new byte[256], 0);
fail("closed expected");
} catch(IOException ex) {
assertEquals("Connection closed", ex.getMessage());
}
th.join();
}
@Test

Wyświetl plik

@ -158,6 +158,9 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
* use simple USB request supported by all devices to test if connection is still valid
*/
protected void testConnection() throws IOException {
if(mConnection == null || mUsbRequest == null) {
throw new IOException("Connection closed");
}
byte[] buf = new byte[2];
int len = mConnection.controlTransfer(0x80 /*DEVICE*/, 0 /*GET_STATUS*/, 0, 0, buf, buf.length, 200);
if(len < 0)

Wyświetl plik

@ -156,7 +156,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
testConnection();
} else {
do {
nread = super.read(dest, timeout, false);
nread = super.read(dest, timeout);
} while (nread == READ_HEADER_LENGTH);
}
return readFilter(dest, nread);