Fix concurrent TCP CLI scope and add regressions

This commit is contained in:
sergeych committed 2026-04-09 11:55:01 +03:00
1 parent c9eb3df93d
commit fbb1683ba3
10 files changed
+471 -61

No files matched your search

+68 -17
View File
@@ -1,21 +1,72 @@
import lyng.buffer
import lyng.io.net
val server = Net.tcpListen(0, "127.0.0.1")
val port = server.localAddress().port
val accepted = launch {
val client = server.accept()
val line = (client.read(4) as Buffer).decodeUtf8()
client.writeUtf8("echo:" + line)
client.flush()
client.close()
server.close()
line
val host = "127.0.0.1"
val clientCount = 1000
val server: TcpServer = Net.tcpListen(0, host, clientCount, true) as TcpServer
val port: Int = server.localAddress().port
fun payloadFor(index: Int): String {
"$index:${Random.nextInt()}:${Random.nextInt()}"
}
val socket = Net.tcpConnect("127.0.0.1", port)
socket.writeUtf8("ping")
socket.flush()
val reply = (socket.read(16) as Buffer).decodeUtf8()
socket.close()
println("${accepted.await()}: $reply")
fun handleClient(client: TcpSocket): String {
try {
val source = client.readLine()
if( source == null ) {
return "server-eof"
}
val reply = "pong: $source"
client.writeUtf8(reply + "\n")
client.flush()
reply
} finally {
client.close()
}
}
val serverJob: Deferred = launch {
var handlers: List<Deferred> = List()
try {
for( i in 0..<1000 ) {
val client: TcpSocket = server.accept() as TcpSocket
handlers += launch {
handleClient(client)
}
}
handlers.joinAll()
} finally {
server.close()
}
}
val clientJobs: List<Deferred> = (0..<clientCount).map { index ->
val payload = payloadFor(index)
launch {
val socket: TcpSocket = Net.tcpConnect(host, port) as TcpSocket
try {
socket.writeUtf8(payload + "\n")
socket.flush()
val reply = socket.readLine()
if( reply == null ) {
"client-eof:$payload"
}
else {
assertEquals("pong: $payload", reply)
reply
}
} finally {
socket.close()
}
}
}
val replies = clientJobs.joinAll()
val serverReplies = serverJob.await() as List<Object>
assertEquals(clientCount, replies.size)
assertEquals(clientCount, serverReplies.size)
assertEquals(replies.toSet, serverReplies.toSet)
val summary = "OK: $clientCount concurrent tcp clients"
println(summary)
summary