Skip to content

Commit 5484ad7

Browse files
committed
[GR-35327] Fixed select() for emulated sockets, added support for TCP_NODELAY
PullRequest: graalpython/2049
2 parents e58e785 + 6719682 commit 5484ad7

File tree

2 files changed

+252
-22
lines changed

2 files changed

+252
-22
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/SocketTests.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@
7171
import static com.oracle.graal.python.runtime.PosixConstants.SO_DOMAIN;
7272
import static com.oracle.graal.python.runtime.PosixConstants.SO_PROTOCOL;
7373
import static com.oracle.graal.python.runtime.PosixConstants.SO_TYPE;
74+
import static com.oracle.graal.python.runtime.PosixConstants.TCP_NODELAY;
7475
import static com.oracle.graal.python.runtime.PosixConstants.TCP_USER_TIMEOUT;
7576
import static org.hamcrest.CoreMatchers.anyOf;
7677
import static org.hamcrest.CoreMatchers.equalTo;
7778
import static org.junit.Assert.assertArrayEquals;
7879
import static org.junit.Assert.assertEquals;
7980
import static org.junit.Assert.assertFalse;
81+
import static org.junit.Assert.assertNotEquals;
8082
import static org.junit.Assert.assertNull;
8183
import static org.junit.Assert.assertThat;
8284
import static org.junit.Assert.assertTrue;
@@ -90,6 +92,8 @@
9092
import java.util.stream.Collectors;
9193
import java.util.stream.Stream;
9294

95+
import com.oracle.graal.python.runtime.PosixSupportLibrary.SelectResult;
96+
import com.oracle.graal.python.runtime.PosixSupportLibrary.Timeval;
9397
import org.hamcrest.Description;
9498
import org.hamcrest.TypeSafeMatcher;
9599
import org.junit.Before;
@@ -531,6 +535,16 @@ public void setSocketOptions() throws PosixException {
531535
assertEquals(newTimeout, getIntSockOpt(socket, IPPROTO_TCP.value, TCP_USER_TIMEOUT.getValueIfDefined()));
532536
}
533537

538+
@Test
539+
public void setSocketOptionsTcpNodelay() throws PosixException {
540+
assumeTrue(TCP_NODELAY.defined);
541+
TcpClient cli = new TcpClient(AF_INET.value);
542+
setIntSockOpt(cli.fd, IPPROTO_TCP.value, TCP_NODELAY.getValueIfDefined(), 1);
543+
TcpServer srv = new TcpServer(AF_INET.value);
544+
cli.connect(srv.usa());
545+
assertNotEquals(0, getIntSockOpt(cli.fd, IPPROTO_TCP.value, TCP_NODELAY.getValueIfDefined()));
546+
}
547+
534548
@Test
535549
public void nonBlockingDgramRecv() throws PosixException {
536550
expectErrno(OSErrorEnum.EWOULDBLOCK);
@@ -555,6 +569,55 @@ public void nonBlockingAccept() throws PosixException {
555569
srv.accept(cli.address());
556570
}
557571

572+
@Test
573+
public void dgramSelect() throws PosixException {
574+
UdpServer srv = new UdpServer(AF_INET.value);
575+
UdpClient cli = new UdpClient(AF_INET.value);
576+
577+
SelectResult res = lib.select(posixSupport, new int[]{srv.fd}, new int[0], new int[0], new Timeval(0, 100000));
578+
assertFalse(res.getReadFds()[0]);
579+
580+
cli.sendto(DATA, 0, srv.usa());
581+
582+
res = lib.select(posixSupport, new int[]{srv.fd}, new int[0], new int[0], new Timeval(0, 100000));
583+
assertTrue(res.getReadFds()[0]);
584+
585+
checkUsa(cli.address(), srv.recvfrom(DATA, 0));
586+
}
587+
588+
@Test
589+
public void streamSelect() throws PosixException {
590+
TcpServer srv = new TcpServer(AF_INET.value);
591+
TcpClient cli = new TcpClient(AF_INET.value);
592+
593+
SelectResult res = lib.select(posixSupport, new int[]{srv.fd}, new int[0], new int[0], new Timeval(0, 100000));
594+
assertFalse(res.getReadFds()[0]);
595+
596+
cli.connect(srv.usa());
597+
598+
res = lib.select(posixSupport, new int[]{srv.fd, cli.fd}, new int[0], new int[0], new Timeval(0, 100000));
599+
assertTrue(res.getReadFds()[0]);
600+
assertFalse(res.getReadFds()[1]);
601+
602+
TcpClient c = srv.accept(cli.address());
603+
604+
checkUsa(c.address(), cli.getpeername());
605+
checkUsa(cli.address(), c.getpeername());
606+
607+
c.shutdown(SHUT_RD.value);
608+
609+
c.send(DATA, 0);
610+
611+
res = lib.select(posixSupport, new int[]{srv.fd, cli.fd, c.fd}, new int[0], new int[0], new Timeval(0, 100000));
612+
assertFalse(res.getReadFds()[0]);
613+
assertTrue(res.getReadFds()[1]);
614+
assertTrue(res.getReadFds()[2]);
615+
616+
assertEquals(0, lib.recv(posixSupport, c.fd, new byte[10], 0, 10, 0));
617+
618+
cli.recv(DATA, 0);
619+
}
620+
558621
@Test
559622
public void getnameinfo() throws GetAddrInfoException {
560623
Object[] res = lib.getnameinfo(posixSupport, createUsa(new Inet6SockAddr(443, IN6ADDR_LOOPBACK, 0, 0)), NI_NUMERICSERV.value | NI_NUMERICHOST.value);

0 commit comments

Comments
 (0)