]> git.plutz.net Git - quickjs_net/commitdiff
adapt tests to latest refactoring
authorPaul Hänsch <paul@plutz.net>
Thu, 19 Mar 2026 22:50:52 +0000 (23:50 +0100)
committerPaul Hänsch <paul@plutz.net>
Thu, 19 Mar 2026 22:50:52 +0000 (23:50 +0100)
socket-tests.js

index 0a65e392445433d1cf3f5888d44a5be40787c22f..b9c8ed2530cb0ac5c7f55b309addc2c559e7caa8 100644 (file)
@@ -31,11 +31,11 @@ function server() {
   assert(true, "running server");
 
   // set up listeners
-  assert(t4 = net.tcpListen("localhost", 1024),  "listening on ip4/tcp");
-  assert(t6 = net.tcp6Listen("localhost", 1024), "listening on ip6/tcp");
-  assert(u4 = net.udpListen("localhost", 1024),  "listening on ip4/udp");
-  assert(u6 = net.udp6Listen("localhost", 1024), "listening on ip6/udp");
-  assert(ux = net.unixListen(xsock), "listening on unix/" + xsock);
+  assert(t4 = new net.Socket("tcp" ).listen("localhost", 1024), "listening on ip4/tcp");
+  assert(t6 = new net.Socket("tcp6").listen("localhost", 1024), "listening on ip6/tcp");
+  assert(u4 = new net.Socket("udp" ).listen("localhost", 1024), "listening on ip4/udp");
+  assert(u6 = new net.Socket("udp6").listen("localhost", 1024), "listening on ip6/udp");
+  assert(ux = new net.Socket("unix").listen(xsock), "listening on unix:" + xsock);
 
   std.printf("\n")
   // check socket props
@@ -81,6 +81,7 @@ function server() {
 
   // launch client
   new os.Worker(scriptArgs[0]);
+  // if (!net.fork()) client();
 
   // accept connections
   assert(ct4 = t4.accept(), "received connection on ip4/tcp (by "+ct4.peerName+"/"+ct4.peerPort+")");
@@ -126,25 +127,37 @@ function client() {
   assert(true, "running client");
 
   var t4, t6, u4, u6, ux, sock;
-  // test global timeout setting
-  assert(! net.getTimeout(), "default connect timeout unset");
-  net.setTimeout(1);
-  assert(net.getTimeout() == 1, "set default connect timeout");
+
+  t4 = new net.Socket("tcp" );
+  t6 = new net.Socket("tcp6");
+  u4 = new net.Socket("udp" );
+  u6 = new net.Socket("udp6");
+  ux = new net.Socket("unix");
+
+  // test default timeout setting
+  for (sock of [t4, t6, u4, u6, ux]) {
+    assert(! sock.timeout, "default connect timeout unset");
+    sock.timeout = 1;
+    assert(sock.timeout == 1, "set default connect timeout");
+  }
 
   // test failing connections
-  try { t4 = net.tcpConnect( "localhost", 1); } catch(e) {}
-  assert(!t4, "conrefused for ip4/tcp");
-  try { t6 = net.tcp6Connect("localhost", 1); } catch(e) {}
-  assert(!t6, "conrefused for ip6/tcp");
-  try { ux = net.unixConnect("/nonexist");    } catch(e) {}
-  assert(!ux, "conrefused for unix");
+  try { t4.connect( "localhost", 1); } catch(e) {
+    assert(!t4.connected, "conrefused for ip4/tcp");
+  }
+  try { t6.connect("localhost", 1); } catch(e) {
+    assert(!t6.connected, "conrefused for ip6/tcp");
+  }
+  try { ux.connect("/nonexist");    } catch(e) {
+    assert(!ux.connected, "conrefused for unix");
+  }
 
   // test connection state property
-  assert(t4 = net.tcpConnect("localhost", 1024),  "connected to ip4/tcp");
-  assert(t6 = net.tcp6Connect("localhost", 1024), "connected to ip6/tcp");
-  assert(u4 = net.udpConnect("localhost", 1024),  "connected to ip4/udp");
-  assert(u6 = net.udp6Connect("localhost", 1024), "connected to ip6/udp");
-  assert(ux = net.unixConnect(xsock), "connected to unix");
+  assert(t4.connect("localhost", 1024),  "connected to ip4/tcp");
+  assert(t6.connect("localhost", 1024), "connected to ip6/tcp");
+  assert(u4.connect("localhost", 1024),  "connected to ip4/udp");
+  assert(u6.connect("localhost", 1024), "connected to ip6/udp");
+  assert(ux.connect(xsock), "connected to unix");
 
   // send "ketchup", receive "mayo"
   for ( sock of [t4, t6, ux, u4, u6] ) {
@@ -167,8 +180,8 @@ function client() {
   // set up new udp connections, test destination option in udp send, send "mustard"
   assert(!u4.close(), "closed ip4/udp client");
   assert(!u6.close(), "closed ip6/udp client");
-  assert(u4 = net.udpConnect("localhost", 1025),  "connected to ip4/udp");
-  assert(u6 = net.udp6Connect("localhost", 1025), "connected to ip6/udp");
+  assert(u4 = new net.Socket("udp").connect("localhost", 1025),  "connected to ip4/udp");
+  assert(u6 = new net.Socket("udp6").connect("localhost", 1025), "connected to ip6/udp");
   assert(u4.send("mustard", "localhost", 1024), "send string from different ip4/udp connection")
   assert(u6.send("mustard", "localhost", 1024), "send string from different ip6/udp connection")
 
@@ -180,7 +193,7 @@ function client() {
 }
 
 if (os.Worker.parent) {
-  // test global timeout setting
+  // test default timeout setting
   // test failing connections
   // test connection state property
   // send "ketchup", receive "mayo"