Skip to content

Commit c9878df

Browse files
committed
subscribe: improve coverage
Also fix an issue revealed in added test. Replicates graphql/graphql-js@4d0601b
1 parent 9b2cab3 commit c9878df

File tree

2 files changed

+155
-1
lines changed

2 files changed

+155
-1
lines changed

src/graphql/subscription/map_async_iterator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async def athrow(self, type_, value=None, traceback=None):
7070
if athrow:
7171
await athrow(type_, value, traceback)
7272
else:
73-
self.is_closed = True
73+
await self.aclose()
7474
if value is None:
7575
if traceback is None:
7676
raise type_

tests/subscription/test_subscribe.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,160 @@ async def produces_a_payload_per_subscription_event():
593593
with raises(StopAsyncIteration):
594594
assert await anext(subscription)
595595

596+
@mark.asyncio
597+
async def produces_a_payload_when_there_are_multiple_events():
598+
pubsub = EventEmitter()
599+
send_important_email, subscription = await create_subscription(pubsub)
600+
payload = anext(subscription)
601+
602+
# A new email arrives!
603+
assert (
604+
send_important_email(
605+
{
606+
"from": "yuzhi@graphql.org",
607+
"subject": "Alright",
608+
"message": "Tests are good",
609+
"unread": True,
610+
}
611+
)
612+
is True
613+
)
614+
615+
assert await payload == (
616+
{
617+
"importantEmail": {
618+
"email": {"from": "yuzhi@graphql.org", "subject": "Alright"},
619+
"inbox": {"unread": 1, "total": 2},
620+
}
621+
},
622+
None,
623+
)
624+
625+
payload = anext(subscription)
626+
627+
# A new email arrives!
628+
assert (
629+
send_important_email(
630+
{
631+
"from": "yuzhi@graphql.org",
632+
"subject": "Alright 2",
633+
"message": "Tests are good 2",
634+
"unread": True,
635+
}
636+
)
637+
is True
638+
)
639+
640+
assert await payload == (
641+
{
642+
"importantEmail": {
643+
"email": {"from": "yuzhi@graphql.org", "subject": "Alright 2"},
644+
"inbox": {"unread": 2, "total": 3},
645+
}
646+
},
647+
None,
648+
)
649+
650+
@mark.asyncio
651+
async def should_not_trigger_when_subscription_is_already_done():
652+
pubsub = EventEmitter()
653+
send_important_email, subscription = await create_subscription(pubsub)
654+
payload = anext(subscription)
655+
656+
# A new email arrives!
657+
assert (
658+
send_important_email(
659+
{
660+
"from": "yuzhi@graphql.org",
661+
"subject": "Alright",
662+
"message": "Tests are good",
663+
"unread": True,
664+
}
665+
)
666+
is True
667+
)
668+
669+
assert await payload == (
670+
{
671+
"importantEmail": {
672+
"email": {"from": "yuzhi@graphql.org", "subject": "Alright"},
673+
"inbox": {"unread": 1, "total": 2},
674+
}
675+
},
676+
None,
677+
)
678+
679+
payload = anext(subscription)
680+
await subscription.aclose()
681+
682+
# A new email arrives!
683+
assert (
684+
send_important_email(
685+
{
686+
"from": "yuzhi@graphql.org",
687+
"subject": "Alright 2",
688+
"message": "Tests are good 2",
689+
"unread": True,
690+
}
691+
)
692+
is False
693+
)
694+
695+
with raises(StopAsyncIteration):
696+
await payload
697+
698+
@mark.asyncio
699+
async def should_not_trigger_when_subscription_is_thrown():
700+
pubsub = EventEmitter()
701+
send_important_email, subscription = await create_subscription(pubsub)
702+
payload = anext(subscription)
703+
704+
# A new email arrives!
705+
assert (
706+
send_important_email(
707+
{
708+
"from": "yuzhi@graphql.org",
709+
"subject": "Alright",
710+
"message": "Tests are good",
711+
"unread": True,
712+
}
713+
)
714+
is True
715+
)
716+
717+
assert await payload == (
718+
{
719+
"importantEmail": {
720+
"email": {"from": "yuzhi@graphql.org", "subject": "Alright"},
721+
"inbox": {"unread": 1, "total": 2},
722+
}
723+
},
724+
None,
725+
)
726+
727+
payload = anext(subscription)
728+
729+
# Throw error
730+
with raises(RuntimeError) as exc_info:
731+
await subscription.athrow(RuntimeError("ouch"))
732+
assert str(exc_info.value) == "ouch"
733+
734+
# A new email arrives!
735+
assert (
736+
send_important_email(
737+
{
738+
"from": "yuzhi@graphql.org",
739+
"subject": "Alright 2",
740+
"message": "Tests are good 2",
741+
"unread": True,
742+
}
743+
)
744+
is False
745+
)
746+
747+
with raises(StopAsyncIteration):
748+
await payload
749+
596750
@mark.asyncio
597751
async def event_order_is_correct_for_multiple_publishes():
598752
pubsub = EventEmitter()

0 commit comments

Comments
 (0)