Skip to content

Commit

Permalink
Bugfix WillMessage in HiveMqttClient (#441)
Browse files Browse the repository at this point in the history
* Adding call to applyWillPublish method so that will changes are reflected on the object that is beeing sent with connection.

* Add test for MQTT V5 Will Message handling

This commit introduces a new test class `V5WillMessageSpec` to verify MQTT V5 Will Message functionality. It includes a configuration setup, MQTT client actions, and a subscriber to assert the reception of the will message.

* Add a second context to verify will message delivery

This change introduces an additional application context to subscribe to the will message topic, ensuring the message is published when the first application disconnects. Removed unnecessary properties in the test and included initiating another context for proper validation.
  • Loading branch information
VytautasCapgemini authored Oct 2, 2024
1 parent 0cd0a62 commit 5e0bdf0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ Mqtt5AsyncClient mqttClient(final Mqtt5ClientConfiguration configuration, @Nulla
.topic(willMessage.getTopic())
.payload(willMessage.getPayload())
.qos(Objects.requireNonNull(MqttQos.fromCode(willMessage.getQos())))
.retain(willMessage.isRetained());
.retain(willMessage.isRetained())
.applyWillPublish();
}

final var client = clientBuilder.buildAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.micronaut.mqtt.hivemq.v5.client

import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient
import com.hivemq.client.mqtt.mqtt5.message.disconnect.Mqtt5DisconnectReasonCode
import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Requires
import io.micronaut.core.util.StringUtils
import io.micronaut.mqtt.annotation.MqttSubscriber
import io.micronaut.mqtt.annotation.Topic
import io.micronaut.mqtt.test.AbstractMQTTTest
import io.micronaut.mqtt.test.MQTT5Test
import spock.util.concurrent.PollingConditions

class V5WillMessageSpec extends AbstractMQTTTest implements MQTT5Test {

void "test will message"() {
// we create first application context that is going to disconnect.
ApplicationContext ctx = startContext([
"mqtt.client.will-message.payload":"last will",
"mqtt.client.will-message.retained":true,
"mqtt.client.will-message.topic":"will/topic",
])
def client = ctx.getBean(Mqtt5AsyncClient)
def polling = new PollingConditions(timeout: 3)

// second application is going to subscribe to the will message topic
// and track it to see if it gets published when first application disconnects
ApplicationContext ctx2 = startContext([
"willmessagetest": true])
def subscriber = ctx2.getBean(LastWillSubscriber)


when:
client.disconnectWith()
.reasonCode(Mqtt5DisconnectReasonCode.DISCONNECT_WITH_WILL_MESSAGE) // send the will message
.sessionExpiryInterval(0) // we want to clear the session
.send()


then:
polling.eventually {
assert subscriber.payload == "last will"
}

cleanup:
ctx.close()
ctx2.close()
}

@Requires(property = "willmessagetest", value = StringUtils.TRUE)
@MqttSubscriber
static class LastWillSubscriber {

String payload

@Topic("will/topic")
void get(String payload) {
this.payload = payload
}
}
}

0 comments on commit 5e0bdf0

Please sign in to comment.