Skip to content

Commit 1689b66

Browse files
chore: remove async/await from websocket-client send operations and convert the test to use synchronous library (#1918)
Co-authored-by: Harsh Gupta <harsh16official@gmail.com> Co-authored-by: Lukasz Gornicki <lpgornicki@gmail.com>
1 parent f890c48 commit 1689b66

File tree

9 files changed

+98
-68
lines changed

9 files changed

+98
-68
lines changed

packages/components/src/components/SendOperations.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const websocketSendOperationConfig = {
2828
const methodName = toSnakeCase(operation.id());
2929
const staticMethodName = `${methodName}_static`;
3030
return {
31-
nonStaticMethod: `async def ${methodName}(self, message):
31+
nonStaticMethod: `def ${methodName}(self, message):
3232
"""
3333
Send a ${methodName} message using the WebSocket connection attached to this instance.
3434
@@ -38,9 +38,9 @@ const websocketSendOperationConfig = {
3838
Raises:
3939
Exception: If sending fails or the socket is not connected.
4040
"""
41-
await self._send(message, self.ws_app)`,
41+
self._send(message, self.ws_app)`,
4242
staticMethod: `@staticmethod
43-
async def ${staticMethodName}(message, socket):
43+
def ${staticMethodName}(message, socket):
4444
"""
4545
Send a ${methodName} message using a provided WebSocket connection, without needing an instance.
4646
@@ -51,7 +51,7 @@ async def ${staticMethodName}(message, socket):
5151
Raises:
5252
Exception: If sending fails or the socket is not connected.
5353
"""
54-
await ${clientName}._send(message, socket)`
54+
${clientName}._send(message, socket)`
5555
};
5656
},
5757
javascript: (operation, clientName) => {

packages/components/test/components/__snapshots__/SendOperations.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ exports[`Testing of SendOperation function render js websockets without send ope
6464
6565
exports[`Testing of SendOperation function render websockets without send operations 1`] = `
6666
"@staticmethod
67-
async def send_user_signedup_static(message, socket):
67+
def send_user_signedup_static(message, socket):
6868
\\"\\"\\"
6969
Send a send_user_signedup message using a provided WebSocket connection, without needing an instance.
7070
@@ -75,9 +75,9 @@ exports[`Testing of SendOperation function render websockets without send operat
7575
Raises:
7676
Exception: If sending fails or the socket is not connected.
7777
\\"\\"\\"
78-
await AccountServiceAPI._send(message, socket)
78+
AccountServiceAPI._send(message, socket)
7979
80-
async def send_user_signedup(self, message):
80+
def send_user_signedup(self, message):
8181
\\"\\"\\"
8282
Send a send_user_signedup message using the WebSocket connection attached to this instance.
8383
@@ -87,5 +87,5 @@ exports[`Testing of SendOperation function render websockets without send operat
8787
Raises:
8888
Exception: If sending fails or the socket is not connected.
8989
\\"\\"\\"
90-
await self._send(message, self.ws_app)"
90+
self._send(message, self.ws_app)"
9191
`;

packages/templates/clients/websocket/python/components/ReceiveOperationsDiscriminators.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ import { getMessageDiscriminatorsFromOperations } from '@asyncapi/generator-help
99
* @returns {React.Element|null} Rendered initialization code or null.
1010
*/
1111
export function ReceiveOperationsDiscriminators({ receiveOperations }) {
12-
const hasOperations = Array.isArray(receiveOperations) && receiveOperations.length > 0;
13-
if (!hasOperations) {
14-
return null;
15-
}
16-
17-
const operationDiscriminators = getMessageDiscriminatorsFromOperations(receiveOperations);
12+
const operationDiscriminators = Array.isArray(receiveOperations) && receiveOperations.length > 0
13+
? getMessageDiscriminatorsFromOperations(receiveOperations)
14+
: [];
1815
const serializedDiscriminators = JSON.stringify(operationDiscriminators);
1916

2017
return (

packages/templates/clients/websocket/python/components/Send.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function Send({ sendOperations }) {
1010
{
1111
`
1212
@staticmethod
13-
async def _send(message, socket):
13+
def _send(message, socket):
1414
"""
1515
Internal helper to handle the actual sending logic.
1616
@@ -24,7 +24,7 @@ async def _send(message, socket):
2424
try:
2525
if isinstance(message, dict):
2626
message = json.dumps(message)
27-
await socket.send(message)
27+
socket.send(message)
2828
except Exception as e:
2929
print("Error sending:", e)`
3030
}

packages/templates/clients/websocket/python/test/components/__snapshots__/Constructor.test.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ exports[`Constructor component (integration with AsyncAPI document) renders with
1616
self.outgoing_processors = [] # Callables to process outgoing messages
1717
self._stop_event = threading.Event()
1818
19+
self.receive_operation_handlers = {}
20+
self.receive_operation_discriminators = []
21+
1922
2023
2124
self.url = url"
@@ -37,6 +40,9 @@ exports[`Constructor component (integration with AsyncAPI document) renders with
3740
self.outgoing_processors = [] # Callables to process outgoing messages
3841
self._stop_event = threading.Event()
3942
43+
self.receive_operation_handlers = {}
44+
self.receive_operation_discriminators = []
45+
4046
4147
4248
self.url = url"
@@ -62,6 +68,9 @@ exports[`Constructor component (integration with AsyncAPI document) renders with
6268
self.outgoing_processors = [] # Callables to process outgoing messages
6369
self._stop_event = threading.Event()
6470
params = {}
71+
self.receive_operation_handlers = {}
72+
self.receive_operation_discriminators = []
73+
6574
heartbeat = heartbeat or os.getenv(\\"HEARTBEAT\\")
6675
if heartbeat is not None:
6776
params[\\"heartbeat\\"] = heartbeat
@@ -95,6 +104,9 @@ exports[`Constructor component (integration with AsyncAPI document) renders with
95104
self.outgoing_processors = [] # Callables to process outgoing messages
96105
self._stop_event = threading.Event()
97106
107+
self.receive_operation_handlers = {}
108+
self.receive_operation_discriminators = []
109+
98110
99111
100112
self.url = url"

packages/templates/clients/websocket/python/test/components/__snapshots__/Requires.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Requires component (integration with AsyncAPI document) renders correctly with null query 1`] = `
44
"import json

packages/templates/clients/websocket/python/test/components/__snapshots__/Send.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`Testing of Send component render Send component with send operations 1`] = `
44
"@staticmethod
5-
async def _send(message, socket):
5+
def _send(message, socket):
66
\\"\\"\\"
77
Internal helper to handle the actual sending logic.
88
@@ -16,7 +16,7 @@ exports[`Testing of Send component render Send component with send operations 1`
1616
try:
1717
if isinstance(message, dict):
1818
message = json.dumps(message)
19-
await socket.send(message)
19+
socket.send(message)
2020
except Exception as e:
2121
print(\\"Error sending:\\", e)"
2222
`;

packages/templates/clients/websocket/test/integration-test/__snapshots__/integration.test.js.snap

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8752,7 +8752,7 @@ class HoppscotchClient:
87528752
self.receive_operation_discriminators.append(discriminator_entry)
87538753
87548754
@staticmethod
8755-
async def send_echo_message_static(message, socket):
8755+
def send_echo_message_static(message, socket):
87568756
\\"\\"\\"
87578757
Send a send_echo_message message using a provided WebSocket connection, without needing an instance.
87588758
@@ -8763,9 +8763,9 @@ class HoppscotchClient:
87638763
Raises:
87648764
Exception: If sending fails or the socket is not connected.
87658765
\\"\\"\\"
8766-
await HoppscotchClient._send(message, socket)
8766+
HoppscotchClient._send(message, socket)
87678767
8768-
async def send_echo_message(self, message):
8768+
def send_echo_message(self, message):
87698769
\\"\\"\\"
87708770
Send a send_echo_message message using the WebSocket connection attached to this instance.
87718771
@@ -8775,10 +8775,10 @@ class HoppscotchClient:
87758775
Raises:
87768776
Exception: If sending fails or the socket is not connected.
87778777
\\"\\"\\"
8778-
await self._send(message, self.ws_app)
8778+
self._send(message, self.ws_app)
87798779
87808780
@staticmethod
8781-
async def _send(message, socket):
8781+
def _send(message, socket):
87828782
\\"\\"\\"
87838783
Internal helper to handle the actual sending logic.
87848784
@@ -8792,7 +8792,7 @@ class HoppscotchClient:
87928792
try:
87938793
if isinstance(message, dict):
87948794
message = json.dumps(message)
8795-
await socket.send(message)
8795+
socket.send(message)
87968796
except Exception as e:
87978797
print(\\"Error sending:\\", e)
87988798
@@ -9133,7 +9133,7 @@ class HoppscotchEchoWebSocketClient:
91339133
self.receive_operation_discriminators.append(discriminator_entry)
91349134
91359135
@staticmethod
9136-
async def send_echo_message_static(message, socket):
9136+
def send_echo_message_static(message, socket):
91379137
\\"\\"\\"
91389138
Send a send_echo_message message using a provided WebSocket connection, without needing an instance.
91399139
@@ -9144,9 +9144,9 @@ class HoppscotchEchoWebSocketClient:
91449144
Raises:
91459145
Exception: If sending fails or the socket is not connected.
91469146
\\"\\"\\"
9147-
await HoppscotchEchoWebSocketClient._send(message, socket)
9147+
HoppscotchEchoWebSocketClient._send(message, socket)
91489148
9149-
async def send_echo_message(self, message):
9149+
def send_echo_message(self, message):
91509150
\\"\\"\\"
91519151
Send a send_echo_message message using the WebSocket connection attached to this instance.
91529152
@@ -9156,10 +9156,10 @@ class HoppscotchEchoWebSocketClient:
91569156
Raises:
91579157
Exception: If sending fails or the socket is not connected.
91589158
\\"\\"\\"
9159-
await self._send(message, self.ws_app)
9159+
self._send(message, self.ws_app)
91609160
91619161
@staticmethod
9162-
async def _send(message, socket):
9162+
def _send(message, socket):
91639163
\\"\\"\\"
91649164
Internal helper to handle the actual sending logic.
91659165
@@ -9173,7 +9173,7 @@ class HoppscotchEchoWebSocketClient:
91739173
try:
91749174
if isinstance(message, dict):
91759175
message = json.dumps(message)
9176-
await socket.send(message)
9176+
socket.send(message)
91779177
except Exception as e:
91789178
print(\\"Error sending:\\", e)
91799179
@@ -9314,6 +9314,9 @@ class PostmanEchoWebSocketClientClient:
93149314
self.outgoing_processors = [] # Callables to process outgoing messages
93159315
self._stop_event = threading.Event()
93169316
9317+
self.receive_operation_handlers = {}
9318+
self.receive_operation_discriminators = []
9319+
93179320
93189321
93199322
self.url = url
@@ -9429,7 +9432,7 @@ class PostmanEchoWebSocketClientClient:
94299432
handler(error)
94309433
94319434
@staticmethod
9432-
async def send_echo_message_static(message, socket):
9435+
def send_echo_message_static(message, socket):
94339436
\\"\\"\\"
94349437
Send a send_echo_message message using a provided WebSocket connection, without needing an instance.
94359438
@@ -9440,9 +9443,9 @@ class PostmanEchoWebSocketClientClient:
94409443
Raises:
94419444
Exception: If sending fails or the socket is not connected.
94429445
\\"\\"\\"
9443-
await PostmanEchoWebSocketClientClient._send(message, socket)
9446+
PostmanEchoWebSocketClientClient._send(message, socket)
94449447
9445-
async def send_echo_message(self, message):
9448+
def send_echo_message(self, message):
94469449
\\"\\"\\"
94479450
Send a send_echo_message message using the WebSocket connection attached to this instance.
94489451
@@ -9452,10 +9455,10 @@ class PostmanEchoWebSocketClientClient:
94529455
Raises:
94539456
Exception: If sending fails or the socket is not connected.
94549457
\\"\\"\\"
9455-
await self._send(message, self.ws_app)
9458+
self._send(message, self.ws_app)
94569459
94579460
@staticmethod
9458-
async def _send(message, socket):
9461+
def _send(message, socket):
94599462
\\"\\"\\"
94609463
Internal helper to handle the actual sending logic.
94619464
@@ -9469,7 +9472,7 @@ class PostmanEchoWebSocketClientClient:
94699472
try:
94709473
if isinstance(message, dict):
94719474
message = json.dumps(message)
9472-
await socket.send(message)
9475+
socket.send(message)
94739476
except Exception as e:
94749477
print(\\"Error sending:\\", e)
94759478

0 commit comments

Comments
 (0)