Skip to content

Commit

Permalink
Merge pull request #3 from claws/upstream
Browse files Browse the repository at this point in the history
Add zstr test
  • Loading branch information
Michel Pelletier committed Oct 14, 2013
2 parents d7e6b7c + a066670 commit 1ab02f8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
13 changes: 7 additions & 6 deletions pyczmq/zstr.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,29 @@ def recv_nowait(sock):


@cdef('int zstr_send (void *socket, const char *format, ...);')
def send(sock, fmt):
def send(sock, string):
"""
Send a formatted string to a socket
"""
C.zstr_send(sock, fmt)
return C.zstr_send(sock, string)


@cdef('int zstr_sendm (void *socket, const char *format, ...);')
def sendm(sock, fmt):
def sendm(sock, string):
"""
Send a formatted string to a socket, with MORE flag
"""
return C.zstr_sendm(sock, fmt)
return C.zstr_sendm(sock, string)


@cdef('int zstr_sendx (void *socket, const char *string, ...);')
def sendx(sock, string):
def sendx(sock, *strings):
"""
Send a series of strings (until NULL) as multipart data
Returns 0 if the strings could be sent OK, or -1 on error.
"""
return C.zstr_sendx(sock, string)
varargs = [ffi.new('char[]', s) for s in strings] + [ffi.NULL]
return C.zstr_sendx(sock, *varargs)


@cdef('int zstr_recvx (void *socket, char **string_p, ...);')
Expand Down
29 changes: 29 additions & 0 deletions tests/test_zstr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Replicates czmq test_zstr
"""

from pyczmq import zmq, zctx, zsocket, zstr

def test_zstr():
ctx = zctx.new()
test_endpoint = 'inproc://zstr.test'
output_s = zsocket.new(ctx, zmq.PAIR)
zsocket.bind(output_s, test_endpoint)
input_s = zsocket.new(ctx, zmq.PAIR)
zsocket.connect(input_s, test_endpoint)

# Send ten strings, five strings with MORE flag and then END
for i in range(0, 10):
rc = zstr.send(output_s, "this is string {}".format(i))
assert(rc == 0)
zstr.sendx(output_s, "This", "is", "almost", "the", "very", "END")

# Read and count until we receive END
string_nbr = 0
while True:
string = zstr.recv(input_s)
if string == "END":
break
string_nbr += 1
assert(string_nbr == 15)

0 comments on commit 1ab02f8

Please sign in to comment.