blob: 57de3271d5c188472451731932c18b25a57c7c8f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
discard """
cmd: "nim $target --hints:on --define:ssl $options $file"
disabled: osx
"""
import asyncdispatch, asyncnet, net, strutils
import stdtest/testutils
when defined(ssl):
var port0: Port
var msgCount = 0
const
swarmSize = 10
messagesToSend = 50
var clientCount = 0
proc sendMessages(client: AsyncSocket) {.async.} =
for i in 0 ..< messagesToSend:
await send(client, "Message " & $i & "\c\L")
proc launchSwarm(port: Port) {.async.} =
for i in 0 ..< swarmSize:
var sock = newAsyncSocket()
var clientContext = newContext(verifyMode = CVerifyNone)
clientContext.wrapSocket(sock)
await connect(sock, "localhost", port)
await sendMessages(sock)
close(sock)
proc readMessages(client: AsyncSocket) {.async.} =
while true:
var line = await recvLine(client)
if line == "":
close(client)
inc(clientCount)
break
else:
if line.startsWith("Message "):
inc(msgCount)
else:
doAssert false
proc createServer() {.async.} =
let serverContext = newContext(verifyMode = CVerifyNone,
certFile = "tests/testdata/mycert.pem",
keyFile = "tests/testdata/mycert.pem")
var server = newAsyncSocket()
serverContext.wrapSocket(server)
server.setSockOpt(OptReuseAddr, true)
bindAddr(server)
port0 = getLocalAddr(server)[1]
server.listen()
while true:
let client = await accept(server)
serverContext.wrapConnectedSocket(client, handshakeAsServer)
asyncCheck readMessages(client)
asyncCheck createServer()
asyncCheck launchSwarm(port0)
while true:
poll()
if clientCount == swarmSize: break
template cond(): bool = msgCount == swarmSize * messagesToSend
when defined(windows):
# currently: msgCount == 0
flakyAssert cond()
elif defined(linux) and int.sizeof == 8:
# currently: msgCount == 10
flakyAssert cond()
doAssert msgCount > 0
else: doAssert cond(), $msgCount
|