Skip to content

Commit 9b8ade4

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

File tree

2 files changed

+156
-22
lines changed

2 files changed

+156
-22
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: 155 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -453,28 +453,8 @@ async def resolves_to_an_error_if_variables_were_wrong_type():
453453
"""
454454
)
455455

456-
pubsub = EventEmitter()
457-
root_value = {
458-
"inbox": {
459-
"emails": [
460-
{
461-
"from": "joe@graphql.org",
462-
"subject": "Hello",
463-
"message": "Hello World",
464-
"unread": False,
465-
}
466-
]
467-
},
468-
"importantEmail": lambda _info: EventEmitterAsyncIterator(
469-
pubsub, "importantEmail"
470-
),
471-
}
472-
473456
result = await subscribe(
474-
schema=email_schema,
475-
document=ast,
476-
root_value=root_value,
477-
variable_values={"priority": "meow"},
457+
schema=email_schema, document=ast, variable_values={"priority": "meow"},
478458
)
479459

480460
assert result == (
@@ -601,6 +581,160 @@ async def produces_a_payload_per_subscription_event():
601581
with raises(StopAsyncIteration):
602582
assert await anext(subscription)
603583

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

0 commit comments

Comments
 (0)