Skip to content

Commit

Permalink
Apply review changes (PR 155)
Browse files Browse the repository at this point in the history
Round 1: - Fix typo in exception msg.
         - Indent 'case' more.
         - use variable in place of constant.
Round 2: - Ignore test on windoof.
         - Whops, did ignore the wrong test (force-pushed to fix the
           other one).
Round 3: - Enable windoof test again as requested in PR review.
Round 4: - Move failing OSX to its own test.

Taken from 3dcb4ec.
  • Loading branch information
hiddenalpha committed Nov 21, 2023
1 parent 772f9be commit 98258e1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 43 deletions.
72 changes: 36 additions & 36 deletions src/main/cpp/_nix_based/jssc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ static int awaitReadReady(JNIEnv*env, jlong fd){

if( fd >= FD_SETSIZE ){
jclass exClz = env->FindClass("java/lang/UnsupportedOperationException");
if( exClz != NULL ) env->ThrowNew(exClz, "Bad luck. 'select' cannot hanlde large fds.");
if( exClz != NULL ) env->ThrowNew(exClz, "Bad luck. 'select' cannot handle large fds.");
static_assert(EBADF > 0, "EBADF > 0");
return -EBADF;
}
Expand All @@ -574,25 +574,25 @@ static int awaitReadReady(JNIEnv*env, jlong fd){
err = errno;
jclass exClz = NULL;
switch( err ){
case EBADF:
exClz = env->FindClass("java/lang/IllegalArgumentException");
if( exClz != NULL ) env->ThrowNew(exClz, "EBADF select()");
static_assert(EBADF > 0, "EBADF > 0");
return -err;
case EINVAL:
exClz = env->FindClass("java/lang/IllegalArgumentException");
if( exClz != NULL ) env->ThrowNew(exClz, "EINVAL select()");
static_assert(EINVAL > 0, "EINVAL > 0");
return -err;
default:
// TODO: Maybe other errors are candidates to raise java exceptions too. We can
// add them as soon we know which of them occur, and what they actually
// mean in our context. For now, we infinitely loop, as the original code
// did.
if( numUnknownErrors == 0) fprintf(stderr, "select(): %s\n", strerror(err));
static_assert(INT_MAX >= 0x7FFF, "INT_MAX >= 0x7FFF");
numUnknownErrors = (numUnknownErrors + 1) & 0x3FFF;
continue;
case EBADF:
exClz = env->FindClass("java/lang/IllegalArgumentException");
if( exClz != NULL ) env->ThrowNew(exClz, "EBADF select()");
static_assert(EBADF > 0, "EBADF > 0");
return -err;
case EINVAL:
exClz = env->FindClass("java/lang/IllegalArgumentException");
if( exClz != NULL ) env->ThrowNew(exClz, "EINVAL select()");
static_assert(EINVAL > 0, "EINVAL > 0");
return -err;
default:
// TODO: Maybe other errors are candidates to raise java exceptions too. We can
// add them as soon we know which of them occur, and what they actually
// mean in our context. For now, we infinitely loop, as the original code
// did.
if( numUnknownErrors == 0) fprintf(stderr, "select(): %s\n", strerror(err));
static_assert(INT_MAX >= 0x7FFF, "INT_MAX >= 0x7FFF");
numUnknownErrors = (numUnknownErrors + 1) & 0x3FFF;
continue;
}
}
// Did wait successfully.
Expand All @@ -613,20 +613,20 @@ static int awaitReadReady(JNIEnv*env, jlong fd){
err = errno;
jclass exClz = NULL;
switch( err ){
case EINVAL:
exClz = env->FindClass("java/lang/IllegalArgumentException");
if( exClz != NULL ) env->ThrowNew(exClz, "EINVAL poll()");
static_assert(EINVAL > 0, "EINVAL > 0");
return -EINVAL;
default:
// TODO: Maybe other errors are candidates to raise java exceptions too. We can
// add them as soon we know which of them occur, and what they actually
// mean in our context. For now, we infinitely loop, as the original code
// did.
if( numUnknownErrors == 0) fprintf(stderr, "poll(): %s\n", strerror(err));
static_assert(INT_MAX >= 0x7FFF, "INT_MAX >= 0x7FFF");
numUnknownErrors = (numUnknownErrors + 1) & 0x3FFF;
continue;
case EINVAL:
exClz = env->FindClass("java/lang/IllegalArgumentException");
if( exClz != NULL ) env->ThrowNew(exClz, "EINVAL poll()");
static_assert(EINVAL > 0, "EINVAL > 0");
return -err;
default:
// TODO: Maybe other errors are candidates to raise java exceptions too. We can
// add them as soon we know which of them occur, and what they actually
// mean in our context. For now, we infinitely loop, as the original code
// did.
if( numUnknownErrors == 0) fprintf(stderr, "poll(): %s\n", strerror(err));
static_assert(INT_MAX >= 0x7FFF, "INT_MAX >= 0x7FFF");
numUnknownErrors = (numUnknownErrors + 1) & 0x3FFF;
continue;
}
}
// Did wait successfully.
Expand Down Expand Up @@ -674,8 +674,8 @@ JNIEXPORT jbyteArray JNICALL Java_jssc_SerialNativeInterface_readBytes
err = errno;
const char *exName = NULL, *emsg = NULL;
switch( err ){
case EBADF: exName = "java/lang/IllegalArgumentException"; emsg = "EBADF"; break;
default: exName = "java/io/IOException"; emsg = strerror(err); break;
case EBADF: exName = "java/lang/IllegalArgumentException"; emsg = "EBADF"; break;
default: exName = "java/io/IOException"; emsg = strerror(err); break;
}
jclass exClz = env->FindClass(exName);
if( exClz != NULL ) env->ThrowNew(exClz, emsg);
Expand Down
34 changes: 27 additions & 7 deletions src/test/java/jssc/SerialNativeInterfaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

public class SerialNativeInterfaceTest {

private SerialNativeInterface testTarget;

@Before
public void before(){
testTarget = new SerialNativeInterface();
}

@Test
public void testInitNativeInterface() {
SerialNativeInterface serial = new SerialNativeInterface();
Expand Down Expand Up @@ -60,6 +55,31 @@ public void reportsWriteErrorsAsIOException() throws Exception {

@Test
public void throwsIllegalArgumentExceptionIfPortHandleIllegal() throws Exception {
assumeFalse(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_MAC_OS_X);

SerialNativeInterface testTarget = new SerialNativeInterface();
try{
testTarget.readBytes(999, 42);
fail("Where is the exception?");
}catch( IllegalArgumentException ex ){
assertTrue(ex.getMessage().contains("EBADF"));
}
}

/**
* <p>This is a duplicate of {@link #throwsIllegalArgumentExceptionIfPortHandleIllegal()}
* but targets osx only. Not yet analyzed why osx (using select) hangs here. See also <a
* href="https://github.com/java-native/jssc/pull/155">PR 155</a>. Where this
* was discovered.</p>
*
* <p>TODO: Go down that rabbit hole and get rid of that "bug".</p>
*/
@Test
@org.junit.Ignore("TODO analyze where this osx hang comes from")
public void throwsIllegalArgumentExceptionIfPortHandleIllegalOsx() throws Exception {
assumeTrue(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_MAC_OS_X);

SerialNativeInterface testTarget = new SerialNativeInterface();
try{
testTarget.readBytes(999, 42);
fail("Where is the exception?");
Expand Down

0 comments on commit 98258e1

Please sign in to comment.