@@ -1596,4 +1596,40 @@ TEST(ConsumerTest, testCloseAfterSeek) {
15961596 anotherClient.close ();
15971597}
15981598
1599+ TEST (ConsumerTest, testIsConnectedFalsePositiveAfterSubscribeRejectedOnReconnect) {
1600+ // A reconnect SUBSCRIBE failure happens after the initial subscribe has already completed.
1601+ const std::string topic =
1602+ " persistent://public/default/test-false-positive-" + std::to_string (time (nullptr ));
1603+ Client client (lookupUrl);
1604+ Consumer consumer;
1605+ ASSERT_EQ (ResultOk, client.subscribe (topic, " sub" , consumer));
1606+ ASSERT_TRUE (consumer.isConnected ()) << " Precondition: consumer should be connected" ;
1607+
1608+ auto & consumerImpl = PulsarFriend::getConsumerImpl (consumer);
1609+
1610+ // Capture the current live connection.
1611+ auto cnx = consumerImpl.getCnx ().lock ();
1612+ ASSERT_TRUE (cnx != nullptr ) << " Precondition: cnx should be non-null" ;
1613+ LOG_INFO (" Step 1 passed: consumer subscribed, cnx=" << cnx);
1614+
1615+ // Simulate the broker rejecting the SUBSCRIBE command during reconnect.
1616+ Result handleResult = PulsarFriend::consumerHandleCreateConsumer (consumer, cnx, ResultAuthorizationError);
1617+ LOG_INFO (" Step 2: handleCreateConsumer returned " << handleResult);
1618+ EXPECT_EQ (ResultRetryable, handleResult)
1619+ << " handleCreateConsumer should return ResultRetryable for an already-created consumer" ;
1620+
1621+ // The failed SUBSCRIBE must clear the connection set before SUBSCRIBE.
1622+ auto cnxAfter = consumerImpl.getCnx ().lock ();
1623+ LOG_INFO (" Step 3: cnx after handleCreateConsumer failure = " << cnxAfter);
1624+ LOG_INFO (" Step 3: isConnected() = " << consumer.isConnected ());
1625+
1626+ EXPECT_EQ (nullptr , cnxAfter)
1627+ << " After fix: connection_ must be cleared by resetCnx() so grabCnx() can retry" ;
1628+ EXPECT_FALSE (consumer.isConnected ())
1629+ << " After fix: isConnected() must return false after SUBSCRIBE rejection" ;
1630+
1631+ consumer.close ();
1632+ client.close ();
1633+ }
1634+
15991635} // namespace pulsar
0 commit comments