@@ -23,16 +23,16 @@ void test_stream_hwm_disconnect ()
2323 // Connect a raw TCP socket to the ZMQ_STREAM socket
2424 fd_t fd = connect_socket (endpoint);
2525
26- // STREAM socket receives two frames on connection:
26+ // STREAM socket receives two frames on connection:
2727 // 1. The routing ID of the new peer
2828 zmq_msg_t routing_id;
2929 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&routing_id));
3030 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&routing_id, stream, 0 ));
31-
31+
3232 // Store routing ID for later use in disconnection
3333 size_t id_size = zmq_msg_size (&routing_id);
3434 void *id_data = zmq_msg_data (&routing_id);
35-
35+
3636 // 2. An empty frame (connection notification)
3737 TEST_ASSERT_TRUE (zmq_msg_more (&routing_id));
3838 zmq_msg_t empty;
@@ -45,7 +45,8 @@ void test_stream_hwm_disconnect ()
4545 // In ZMQ_STREAM, we send [Routing ID][Data].
4646 while (true ) {
4747 // Send Routing ID frame
48- int rc = zmq_send (stream, id_data, id_size, ZMQ_DONTWAIT | ZMQ_SNDMORE);
48+ int rc =
49+ zmq_send (stream, id_data, id_size, ZMQ_DONTWAIT | ZMQ_SNDMORE);
4950 if (rc == -1 )
5051 break ;
5152
@@ -61,19 +62,26 @@ void test_stream_hwm_disconnect ()
6162 // Verify that we actually reached the HWM
6263 TEST_ASSERT_EQUAL_INT (EAGAIN, errno);
6364
64- // TEST: Attempt to disconnect the client by sending the Routing ID
65- // followed by a 0-byte payload.
66- // Before the fix, the first frame (Routing ID) would fail with EAGAIN .
65+ // TEST: Attempt to disconnect the client.
66+ // If the loop above ended after the ID frame but before the data frame,
67+ // the socket is in a 'more' state. We handle both scenarios .
6768 int rc = zmq_send (stream, id_data, id_size, ZMQ_DONTWAIT | ZMQ_SNDMORE);
68- TEST_ASSERT_EQUAL_INT ((int ) id_size, rc);
6969
70- // The second frame (0-byte) should trigger the termination logic
71- rc = zmq_send (stream, NULL , 0 , ZMQ_DONTWAIT);
72- TEST_ASSERT_EQUAL_INT (0 , rc);
70+ if (rc == -1 && errno == EAGAIN) {
71+ // Socket is mid-message (waiting for payload).
72+ // Send 0-byte frame to disconnect.
73+ rc = zmq_send (stream, NULL , 0 , ZMQ_DONTWAIT);
74+ TEST_ASSERT_EQUAL_INT (0 , rc);
75+ } else {
76+ // Socket was at message boundary. Send ID then 0-byte frame.
77+ TEST_ASSERT_EQUAL_INT ((int ) id_size, rc);
78+ rc = zmq_send (stream, NULL , 0 , ZMQ_DONTWAIT);
79+ TEST_ASSERT_EQUAL_INT (0 , rc);
80+ }
7381
7482 // Cleanup resources
7583 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&routing_id));
76- close (fd); // Standard POSIX close as seen in other test files
84+ close (fd);
7785 test_context_socket_close (stream);
7886}
7987
0 commit comments