@@ -69,12 +69,15 @@ static int mock_net_disconnect(void *context)
6969 return MQTT_CODE_SUCCESS ;
7070}
7171
72+ static int test_client_inited ;
73+
7274static void setup (void )
7375{
7476 XMEMSET (& test_client , 0 , sizeof (test_client ));
7577 XMEMSET (& test_net , 0 , sizeof (test_net ));
7678 XMEMSET (test_tx_buf , 0 , sizeof (test_tx_buf ));
7779 XMEMSET (test_rx_buf , 0 , sizeof (test_rx_buf ));
80+ test_client_inited = 0 ;
7881
7982 /* Setup mock network callbacks */
8083 test_net .connect = mock_net_connect ;
@@ -85,7 +88,23 @@ static void setup(void)
8588
8689static void teardown (void )
8790{
88- MqttClient_DeInit (& test_client );
91+ /* Only DeInit if Init succeeded — DeInit calls MqttProps_ShutDown
92+ * which decrements a ref counter that must be balanced with Init. */
93+ if (test_client_inited ) {
94+ MqttClient_DeInit (& test_client );
95+ }
96+ }
97+
98+ static int test_init_client (void )
99+ {
100+ int rc = MqttClient_Init (& test_client , & test_net , NULL ,
101+ test_tx_buf , TEST_TX_BUF_SIZE ,
102+ test_rx_buf , TEST_RX_BUF_SIZE ,
103+ TEST_CMD_TIMEOUT_MS );
104+ if (rc == MQTT_CODE_SUCCESS ) {
105+ test_client_inited = 1 ;
106+ }
107+ return rc ;
89108}
90109
91110/* ============================================================================
@@ -151,16 +170,13 @@ TEST(init_success)
151170{
152171 int rc ;
153172
154- rc = MqttClient_Init (& test_client , & test_net , NULL ,
155- test_tx_buf , TEST_TX_BUF_SIZE ,
156- test_rx_buf , TEST_RX_BUF_SIZE ,
157- TEST_CMD_TIMEOUT_MS );
173+ rc = test_init_client ();
158174 ASSERT_EQ (MQTT_CODE_SUCCESS , rc );
159175
160176 /* Verify client structure is set up correctly */
161- ASSERT_EQ ( test_tx_buf , test_client .tx_buf );
177+ ASSERT_TRUE ( test_client .tx_buf == test_tx_buf );
162178 ASSERT_EQ (TEST_TX_BUF_SIZE , test_client .tx_buf_len );
163- ASSERT_EQ ( test_rx_buf , test_client .rx_buf );
179+ ASSERT_TRUE ( test_client .rx_buf == test_rx_buf );
164180 ASSERT_EQ (TEST_RX_BUF_SIZE , test_client .rx_buf_len );
165181 ASSERT_EQ (TEST_CMD_TIMEOUT_MS , test_client .cmd_timeout_ms );
166182}
@@ -192,14 +208,12 @@ TEST(deinit_after_init)
192208{
193209 int rc ;
194210
195- rc = MqttClient_Init (& test_client , & test_net , NULL ,
196- test_tx_buf , TEST_TX_BUF_SIZE ,
197- test_rx_buf , TEST_RX_BUF_SIZE ,
198- TEST_CMD_TIMEOUT_MS );
211+ rc = test_init_client ();
199212 ASSERT_EQ (MQTT_CODE_SUCCESS , rc );
200213
201214 /* DeInit should not crash */
202215 MqttClient_DeInit (& test_client );
216+ test_client_inited = 0 ;
203217 ASSERT_TRUE (1 );
204218}
205219
@@ -222,10 +236,7 @@ TEST(connect_null_connect)
222236{
223237 int rc ;
224238
225- rc = MqttClient_Init (& test_client , & test_net , NULL ,
226- test_tx_buf , TEST_TX_BUF_SIZE ,
227- test_rx_buf , TEST_RX_BUF_SIZE ,
228- TEST_CMD_TIMEOUT_MS );
239+ rc = test_init_client ();
229240 ASSERT_EQ (MQTT_CODE_SUCCESS , rc );
230241
231242 rc = MqttClient_Connect (& test_client , NULL );
@@ -245,10 +256,7 @@ TEST(connect_with_mock_network)
245256 int rc ;
246257 MqttConnect connect ;
247258
248- rc = MqttClient_Init (& test_client , & test_net , NULL ,
249- test_tx_buf , TEST_TX_BUF_SIZE ,
250- test_rx_buf , TEST_RX_BUF_SIZE ,
251- TEST_CMD_TIMEOUT_MS );
259+ rc = test_init_client ();
252260 ASSERT_EQ (MQTT_CODE_SUCCESS , rc );
253261
254262 XMEMSET (& connect , 0 , sizeof (connect ));
@@ -283,10 +291,7 @@ TEST(get_protocol_version_default)
283291 int rc ;
284292 int version ;
285293
286- rc = MqttClient_Init (& test_client , & test_net , NULL ,
287- test_tx_buf , TEST_TX_BUF_SIZE ,
288- test_rx_buf , TEST_RX_BUF_SIZE ,
289- TEST_CMD_TIMEOUT_MS );
294+ rc = test_init_client ();
290295 ASSERT_EQ (MQTT_CODE_SUCCESS , rc );
291296
292297 version = MqttClient_GetProtocolVersion (& test_client );
@@ -304,10 +309,7 @@ TEST(get_protocol_version_string)
304309 int rc ;
305310 const char * version_str ;
306311
307- rc = MqttClient_Init (& test_client , & test_net , NULL ,
308- test_tx_buf , TEST_TX_BUF_SIZE ,
309- test_rx_buf , TEST_RX_BUF_SIZE ,
310- TEST_CMD_TIMEOUT_MS );
312+ rc = test_init_client ();
311313 ASSERT_EQ (MQTT_CODE_SUCCESS , rc );
312314
313315 version_str = MqttClient_GetProtocolVersionString (& test_client );
@@ -351,10 +353,7 @@ TEST(subscribe_null_subscribe)
351353{
352354 int rc ;
353355
354- rc = MqttClient_Init (& test_client , & test_net , NULL ,
355- test_tx_buf , TEST_TX_BUF_SIZE ,
356- test_rx_buf , TEST_RX_BUF_SIZE ,
357- TEST_CMD_TIMEOUT_MS );
356+ rc = test_init_client ();
358357 ASSERT_EQ (MQTT_CODE_SUCCESS , rc );
359358
360359 rc = MqttClient_Subscribe (& test_client , NULL );
@@ -380,10 +379,7 @@ TEST(unsubscribe_null_unsubscribe)
380379{
381380 int rc ;
382381
383- rc = MqttClient_Init (& test_client , & test_net , NULL ,
384- test_tx_buf , TEST_TX_BUF_SIZE ,
385- test_rx_buf , TEST_RX_BUF_SIZE ,
386- TEST_CMD_TIMEOUT_MS );
382+ rc = test_init_client ();
387383 ASSERT_EQ (MQTT_CODE_SUCCESS , rc );
388384
389385 rc = MqttClient_Unsubscribe (& test_client , NULL );
@@ -409,10 +405,7 @@ TEST(publish_null_publish)
409405{
410406 int rc ;
411407
412- rc = MqttClient_Init (& test_client , & test_net , NULL ,
413- test_tx_buf , TEST_TX_BUF_SIZE ,
414- test_rx_buf , TEST_RX_BUF_SIZE ,
415- TEST_CMD_TIMEOUT_MS );
408+ rc = test_init_client ();
416409 ASSERT_EQ (MQTT_CODE_SUCCESS , rc );
417410
418411 rc = MqttClient_Publish (& test_client , NULL );
@@ -474,10 +467,7 @@ TEST(client_flags_set_clear)
474467 int rc ;
475468 word32 flags ;
476469
477- rc = MqttClient_Init (& test_client , & test_net , NULL ,
478- test_tx_buf , TEST_TX_BUF_SIZE ,
479- test_rx_buf , TEST_RX_BUF_SIZE ,
480- TEST_CMD_TIMEOUT_MS );
470+ rc = test_init_client ();
481471 ASSERT_EQ (MQTT_CODE_SUCCESS , rc );
482472
483473 /* Initially no flags should be set */
0 commit comments