Skip to content

Commit 2bfd9f8

Browse files
committed
Add a test for setBlocking
See #44
1 parent 04e6dc8 commit 2bfd9f8

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/main/java/jnr/enxio/channels/Native.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ public static void setBlocking(int fd, boolean block) {
158158

159159
libc().fcntl(fd, LibC.F_SETFL, flags);
160160
}
161+
162+
public static boolean getBlocking(int fd) {
163+
int flags = libc().fcntl(fd, LibC.F_GETFL, 0);
164+
165+
return !((flags & LibC.O_NONBLOCK) == LibC.O_NONBLOCK);
166+
}
161167

162168
public static int shutdown(int fd, int how) {
163169
return libc().shutdown(fd, how);

src/test/java/jnr/enxio/channels/NativeTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package jnr.enxio.channels;
22

3+
import org.junit.Assert;
34
import org.junit.Rule;
45
import org.junit.Test;
56
import org.junit.rules.ExpectedException;
67

78
import java.io.FileDescriptor;
89
import java.io.FileOutputStream;
910
import java.lang.reflect.Field;
11+
import java.nio.channels.Pipe;
1012

1113
public class NativeTest {
1214
@Rule
@@ -23,4 +25,20 @@ public void closeThrowsOnNativeError() throws Exception {
2325
expectedEx.expect(NativeException.class);
2426
Native.close(fd);
2527
}
28+
29+
@Test
30+
public void setBlocking() throws Exception {
31+
Pipe pipe = Pipe.open();
32+
Pipe.SinkChannel sink = pipe.sink();
33+
// sink.getClass().getModule().addOpens("sun.nio.ch", NativeTest.class.getModule());
34+
Field fd1 = sink.getClass().getDeclaredField("fd");
35+
fd1.setAccessible(true);
36+
FileDescriptor descriptor = (FileDescriptor) fd1.get(sink);
37+
Field fdField = descriptor.getClass().getDeclaredField("fd");
38+
fdField.setAccessible(true);
39+
int fd = (int)(Integer)fdField.get(descriptor);
40+
Assert.assertEquals(true, Native.getBlocking(fd));
41+
Native.setBlocking(fd, false);
42+
Assert.assertEquals(false, Native.getBlocking(fd));
43+
}
2644
}

0 commit comments

Comments
 (0)