Skip to content

test(mqtt): use real repositories in SubscribeTest and cover the session update - #8093

Merged
yuluo-yx merged 1 commit into
apache:masterfrom
cloudbase-ai:test/mqtt-subscribe-fixtures
Oct 24, 2026
Merged

yuluo-yx merged 1 commit into
apache:masterfrom
cloudbase-ai:test/mqtt-subscribe-fixtures

Conversation

@cloudbase-ai

Copy link
Copy Markdown
Contributor

Problem

SubscribeTest registered ChannelRepository and SessionRepository as Mockito mocks:

@Mock private ChannelRepository channelRepository;
@Mock private SessionRepository sessionRepository;

A mock answers null for every lookup, so in Subscribe.subscribe():

String clientId = Singleton.INST.get(ChannelRepository.class).get(ctx.channel());   // null
MqttSession session = Singleton.INST.get(SessionRepository.class).get(clientId);    // null
if (Objects.nonNull(session)) { ... }                                              // never entered

the branch that stores the granted subscriptions on the session never ran. All three tests only
looked at the SUBACK, so that half of subscribe() had no coverage at all — and the mocks are also
why the null-key crash described in #8092 stayed invisible from this test class.

What this changes

  • ChannelRepository and SessionRepository are real instances; the connected channel is registered
    in the channel repository, so subscribe() resolves a real client id.
  • New test testSubscribeStoresSubscriptionsInSession asserts the subscription reaches the session.
  • SubscribeRepository and TopicRepository stay mocked: subscribe() writes to the first, and the
    second is only observed through verify(topicRepository, never()).

Verification

mvn -pl shenyu-protocol/shenyu-protocol-mqtt -Dtest=SubscribeTest test
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
BUILD SUCCESS

Full module: 147 tests, and the only errors are the three pre-existing
MqttTransportHandlerTest ones described below — the same three that a clean master produces
locally, no new failure.

Notes

…ion update

SubscribeTest mocked ChannelRepository and SessionRepository, and a mock
answers null for every lookup. subscribe() therefore always took the
"no session" branch: the client id was null, the session was null, and the
code that stores the granted subscriptions on the session never ran. The
three assertions only covered SUBACK, so a regression there would have gone
unnoticed.

The channel and session repositories are now real instances, the connected
channel is registered in the channel repository, and a fourth test asserts
that the subscriptions land on the session. SubscribeRepository and
TopicRepository stay mocked, because subscribe() writes to the first and the
second is only observed through verify().

This also supersedes apache#8089, which adds the same real instances but keeps the
@mock fields with the same names and does not compile.
@yuluo-yx
yuluo-yx merged commit 7cb5e48 into apache:master Oct 24, 2026
31 checks passed
This was referenced Oct 25, 2026
@Aias00 Aias00 added this to the 2.7.2 milestone Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants