89 lines
2.5 KiB
Plaintext
89 lines
2.5 KiB
Plaintext
|
// socket_test_simple.wren
|
||
|
import "socket" for Socket, SocketInstance
|
||
|
|
||
|
foreign class Host {
|
||
|
foreign static signalDone()
|
||
|
}
|
||
|
|
||
|
var mainFiber = Fiber.new {
|
||
|
System.print("Testing socket creation...")
|
||
|
|
||
|
// Test creating a socket using the high-level API
|
||
|
System.print("Creating socket via high-level API...")
|
||
|
var result = SocketInstance.new()
|
||
|
|
||
|
if (result) {
|
||
|
System.print("Result received: %(result)")
|
||
|
|
||
|
if (result.count != 2) {
|
||
|
System.print("Unexpected result format: expected [error, socket], got %(result)")
|
||
|
Host.signalDone()
|
||
|
while(true) { Fiber.yield() } // Keep fiber alive
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var err = result[0]
|
||
|
var sock = result[1]
|
||
|
|
||
|
if (err) {
|
||
|
System.print("Error creating socket: %(err)")
|
||
|
} else if (!sock) {
|
||
|
System.print("Socket is null despite no error")
|
||
|
} else {
|
||
|
System.print("Successfully created socket!")
|
||
|
System.print("Socket instance: %(sock)")
|
||
|
|
||
|
// Test binding
|
||
|
System.print("\nTesting bind...")
|
||
|
var bindResult = sock.bind("127.0.0.1", 12345)
|
||
|
System.print("Bind result: %(bindResult)")
|
||
|
|
||
|
if (bindResult && bindResult.count == 2) {
|
||
|
if (bindResult[0]) {
|
||
|
System.print("Bind error: %(bindResult[0])")
|
||
|
} else {
|
||
|
System.print("Successfully bound to port 12345")
|
||
|
|
||
|
// Test listen
|
||
|
System.print("\nTesting listen...")
|
||
|
var listenResult = sock.listen(5)
|
||
|
System.print("Listen result: %(listenResult)")
|
||
|
|
||
|
if (listenResult && listenResult.count == 2) {
|
||
|
if (listenResult[0]) {
|
||
|
System.print("Listen error: %(listenResult[0])")
|
||
|
} else {
|
||
|
System.print("Successfully listening")
|
||
|
}
|
||
|
} else {
|
||
|
System.print("Unexpected listen result format")
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
System.print("Unexpected bind result format")
|
||
|
}
|
||
|
|
||
|
// Close the socket
|
||
|
System.print("\nClosing socket...")
|
||
|
var closeErr = sock.close()
|
||
|
if (closeErr && closeErr != "Close operation did not complete") {
|
||
|
System.print("Close error: %(closeErr)")
|
||
|
} else if (closeErr == "Close operation did not complete") {
|
||
|
System.print("Close operation timed out")
|
||
|
} else {
|
||
|
System.print("Socket closed successfully")
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
System.print("Result was null")
|
||
|
}
|
||
|
|
||
|
System.print("\nTest complete")
|
||
|
Host.signalDone()
|
||
|
|
||
|
// Keep the fiber alive to prevent "Cannot call root fiber" error
|
||
|
while(true) {
|
||
|
Fiber.yield()
|
||
|
}
|
||
|
}
|