Skip to content

Commit

Permalink
Support AnyIO
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Oct 29, 2024
1 parent 30e3189 commit e40a028
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 241 deletions.
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ classifiers = [
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]
dependencies = ["cffi; implementation_name == 'pypy'"]
dependencies = [
"cffi; implementation_name == 'pypy'",
"anyioutils >=0.4.1"
]
description = "Python bindings for 0MQ"
readme = "README.md"

Expand Down
29 changes: 10 additions & 19 deletions tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import zmq.asyncio as zaio


pytestmark = pytest.mark.anyio


@pytest.fixture
def Context(event_loop):
return zaio.Context
Expand Down Expand Up @@ -46,23 +49,17 @@ def test_instance_subclass_second(context):
async def test_recv_multipart(context, create_bound_pair):
a, b = create_bound_pair(zmq.PUSH, zmq.PULL)
f = b.recv_multipart()
assert not f.done()
await a.send(b"hi")
recvd = await f
assert recvd == [b"hi"]
assert await f == [b"hi"]


async def test_recv(create_bound_pair):
a, b = create_bound_pair(zmq.PUSH, zmq.PULL)
f1 = b.recv()
f2 = b.recv()
assert not f1.done()
assert not f2.done()
await a.send_multipart([b"hi", b"there"])
recvd = await f2
assert f1.done()
assert f1.result() == b"hi"
assert recvd == b"there"
assert await f1 == b"hi"
assert await f2 == b"there"


@mark.skipif(not hasattr(zmq, "RCVTIMEO"), reason="requires RCVTIMEO")
Expand All @@ -72,50 +69,44 @@ async def test_recv_timeout(push_pull):
f1 = b.recv()
b.rcvtimeo = 1000
f2 = b.recv_multipart()
with pytest.raises(zmq.Again):
with pytest.raises(ExceptionGroup) as excinfo:
await f1
assert excinfo.group_contains(zmq.Again)
await a.send_multipart([b"hi", b"there"])
recvd = await f2
assert f2.done()
assert recvd == [b"hi", b"there"]


@mark.skipif(not hasattr(zmq, "SNDTIMEO"), reason="requires SNDTIMEO")
async def test_send_timeout(socket):
s = socket(zmq.PUSH)
s.sndtimeo = 100
with pytest.raises(zmq.Again):
with pytest.raises(ExceptionGroup) as excinfo:
await s.send(b"not going anywhere")
assert excinfo.group_contains(zmq.Again)


async def test_recv_string(push_pull):
a, b = push_pull
f = b.recv_string()
assert not f.done()
msg = "πøøπ"
await a.send_string(msg)
recvd = await f
assert f.done()
assert f.result() == msg
assert recvd == msg


async def test_recv_json(push_pull):
a, b = push_pull
f = b.recv_json()
assert not f.done()
obj = dict(a=5)
await a.send_json(obj)
recvd = await f
assert f.done()
assert f.result() == obj
assert recvd == obj


async def test_recv_json_cancelled(push_pull):
a, b = push_pull
f = b.recv_json()
assert not f.done()
f.cancel()
# cycle eventloop to allow cancel events to fire
await asyncio.sleep(0)
Expand Down
Loading

0 comments on commit e40a028

Please sign in to comment.