|
| 1 | +# Copyright (C) DATADVANCE, 2010-2021 |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining |
| 4 | +# a copy of this software and associated documentation files (the |
| 5 | +# "Software"), to deal in the Software without restriction, including |
| 6 | +# without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +# distribute, sublicense, and/or sell copies of the Software, and to |
| 8 | +# permit persons to whom the Software is furnished to do so, subject to |
| 9 | +# the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be |
| 12 | +# included in all copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 | +# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | +# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 19 | +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 20 | +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +"""Test that intermediate notifications skipped.""" |
| 23 | + |
| 24 | +import time |
| 25 | + |
| 26 | +import graphene |
| 27 | +import pytest |
| 28 | + |
| 29 | +import channels_graphql_ws |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.asyncio |
| 33 | +async def test_notification_queue_limit(gql): |
| 34 | + """Test it is possible to skip intermediate notifications. |
| 35 | +
|
| 36 | + Here we start subscription which send 10 messages and server took |
| 37 | + 1 second to process each message. All messages except the first and |
| 38 | + the last will be skipped by the server. Because subscription class |
| 39 | + sets notification_queue_limit to 1. |
| 40 | + """ |
| 41 | + |
| 42 | + print("Prepare the test setup: GraphQL backend classes.") |
| 43 | + |
| 44 | + class SendMessages(graphene.Mutation): |
| 45 | + """Send message mutation.""" |
| 46 | + |
| 47 | + is_ok = graphene.Boolean() |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def mutate(root, info): |
| 51 | + """Broadcast 10 messages.""" |
| 52 | + del root, info |
| 53 | + for idx in range(10): |
| 54 | + OnNewMessage.broadcast(payload={"message": str(idx)}) |
| 55 | + return SendMessages(is_ok=True) |
| 56 | + |
| 57 | + class OnNewMessage(channels_graphql_ws.Subscription): |
| 58 | + """Triggered by `SendMessage` on every new message.""" |
| 59 | + |
| 60 | + # Leave only the last message in the server queue. |
| 61 | + notification_queue_limit = 1 |
| 62 | + |
| 63 | + message = graphene.String() |
| 64 | + |
| 65 | + @staticmethod |
| 66 | + def publish(payload, info): |
| 67 | + """Notify all clients except the author of the message.""" |
| 68 | + del info |
| 69 | + # Emulate server high load. It is bad to use sleep in the |
| 70 | + # tests but here it seems ok. If test is running within |
| 71 | + # high load builder it will behave the same and skip |
| 72 | + # notifications it is unable to process. |
| 73 | + time.sleep(1) |
| 74 | + return OnNewMessage(message=payload["message"]) |
| 75 | + |
| 76 | + class Subscription(graphene.ObjectType): |
| 77 | + """Root subscription.""" |
| 78 | + |
| 79 | + on_new_message = OnNewMessage.Field() |
| 80 | + |
| 81 | + class Mutation(graphene.ObjectType): |
| 82 | + """Root mutation.""" |
| 83 | + |
| 84 | + send_messages = SendMessages.Field() |
| 85 | + |
| 86 | + print("Establish & initialize WebSocket GraphQL connections.") |
| 87 | + |
| 88 | + comm1 = gql( |
| 89 | + mutation=Mutation, |
| 90 | + subscription=Subscription, |
| 91 | + consumer_attrs={"strict_ordering": True}, |
| 92 | + ) |
| 93 | + await comm1.connect_and_init() |
| 94 | + |
| 95 | + comm2 = gql( |
| 96 | + mutation=Mutation, |
| 97 | + subscription=Subscription, |
| 98 | + consumer_attrs={"strict_ordering": True}, |
| 99 | + ) |
| 100 | + await comm2.connect_and_init() |
| 101 | + |
| 102 | + print("Subscribe to receive a new message notifications.") |
| 103 | + |
| 104 | + sub_op_id = await comm2.send( |
| 105 | + msg_type="start", |
| 106 | + payload={ |
| 107 | + "query": "subscription op_name { on_new_message { message } }", |
| 108 | + "variables": {}, |
| 109 | + "operationName": "op_name", |
| 110 | + }, |
| 111 | + ) |
| 112 | + await comm2.assert_no_messages("Subscribe responded with a message!") |
| 113 | + |
| 114 | + print("Start sending notifications.") |
| 115 | + |
| 116 | + mut_op_id = await comm1.send( |
| 117 | + msg_type="start", |
| 118 | + payload={ |
| 119 | + "query": """mutation op_name { send_messages { is_ok } }""", |
| 120 | + "variables": {}, |
| 121 | + "operationName": "op_name", |
| 122 | + }, |
| 123 | + ) |
| 124 | + await comm1.receive(assert_id=mut_op_id, assert_type="data") |
| 125 | + await comm1.receive(assert_id=mut_op_id, assert_type="complete") |
| 126 | + |
| 127 | + await comm1.assert_no_messages("Self-notification happened!") |
| 128 | + |
| 129 | + # Client will receive only the first and the last notifications. |
| 130 | + resp = await comm2.receive(assert_id=sub_op_id, assert_type="data") |
| 131 | + assert resp["data"]["on_new_message"]["message"] == "0" |
| 132 | + |
| 133 | + resp = await comm2.receive(assert_id=sub_op_id, assert_type="data") |
| 134 | + assert resp["data"]["on_new_message"]["message"] == "9" |
| 135 | + |
| 136 | + await comm1.assert_no_messages( |
| 137 | + "Unexpected message received at the end of the test!" |
| 138 | + ) |
| 139 | + await comm2.assert_no_messages( |
| 140 | + "Unexpected message received at the end of the test!" |
| 141 | + ) |
| 142 | + await comm1.finalize() |
| 143 | + await comm2.finalize() |
0 commit comments