-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from nim-lang/feat_non_block_and_nim2
Feat non block and nim2
- Loading branch information
Showing
8 changed files
with
324 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import ../zmq | ||
import std/[asyncdispatch] | ||
|
||
proc asyncpoll() = | ||
# test "asyncZPoller": | ||
block: | ||
const zaddr = "tcp://127.0.0.1:15571" | ||
const zaddr2 = "tcp://127.0.0.1:15572" | ||
var pusher = listen(zaddr, PUSH) | ||
var puller = connect(zaddr, PULL) | ||
|
||
var pusher2 = listen(zaddr2, PUSH) | ||
var puller2 = connect(zaddr2, PULL) | ||
var poller: AsyncZPoller | ||
|
||
var i = 0 | ||
# Register the callback | ||
# assert message received are correct (should be even integer in string format) | ||
var msglist = @["0", "2", "4", "6", "8"] | ||
var msgCount = 0 | ||
poller.register( | ||
puller2, | ||
ZMQ_POLLIN, | ||
proc(x: ZSocket) = | ||
let msg = x.receive() | ||
inc(msgCount) | ||
if msglist.contains(msg): | ||
msglist.delete(0) | ||
assert true | ||
else: | ||
assert false | ||
) | ||
# assert message received are correct (should be even integer in string format) | ||
var msglist2 = @["0", "2", "4", "6", "8"] | ||
var msgCount2 = 0 | ||
poller.register( | ||
puller, | ||
ZMQ_POLLIN, | ||
proc(x: ZSocket) = | ||
let msg = x.receive() | ||
inc(msgCount2) | ||
if msglist2.contains(msg): | ||
msglist2.delete(0) | ||
assert true | ||
else: | ||
assert false | ||
) | ||
|
||
let | ||
N = 10 | ||
N_MAX_TIMEOUT = 5 | ||
|
||
var sndCount = 0 | ||
# A client send some message | ||
for i in 0..<N: | ||
if (i mod 2) == 0: | ||
# Can periodically send stuff | ||
pusher.send($i) | ||
pusher2.send($i) | ||
inc(sndCount) | ||
|
||
# N_MAX_TIMEOUT is the number of time the poller can timeout before exiting the loop | ||
while i < N_MAX_TIMEOUT: | ||
|
||
# I don't recommend a high timeout because it's going to poll for the duration if there is no message in queue | ||
var fut = poller.pollAsync(1) | ||
let r = waitFor fut | ||
if r < 0: | ||
break # error case | ||
elif r == 0: | ||
inc(i) | ||
|
||
# No longer polling but some callback may not have finished | ||
while hasPendingOperations(): | ||
drain() | ||
|
||
assert msgCount == msgCount2 | ||
assert msgCount == sndCount | ||
|
||
pusher.close() | ||
puller.close() | ||
pusher2.close() | ||
puller2.close() | ||
|
||
when isMainModule: | ||
asyncpoll() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import ../zmq | ||
import std/unittest | ||
|
||
proc sockHandler(req, rep: ZConnection, pong: string) = | ||
req.send(pong) | ||
let r = rep.receive() | ||
check r == pong | ||
|
||
proc testDestroy() = | ||
const sockaddr = "tcp://127.0.0.1:55001" | ||
|
||
test "Destroy & Copy": | ||
let | ||
ping = "ping" | ||
pong = "pong" | ||
|
||
var rep = listen(sockaddr, REP) | ||
var req = connect(sockaddr, REQ) | ||
|
||
sockHandler(req, rep, ping) | ||
sockHandler(rep, req, pong) | ||
|
||
block: | ||
var req2 = req | ||
req2.send(ping) | ||
let r = rep.receive() | ||
check r == ping | ||
|
||
rep.send(pong) | ||
block: | ||
var req2 = req | ||
let r = req2.receive() | ||
check r == pong | ||
|
||
when isMainModule: | ||
testDestroy() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
{.deadCodeElim: on.} | ||
when defined(windows): | ||
const | ||
zmqdll* = "(lib|)zmq.dll" | ||
|
Oops, something went wrong.