generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bugfix WillMessage in HiveMqttClient (#441)
* 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
1 parent
0cd0a62
commit 5e0bdf0
Showing
2 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
mqtt-hivemq/src/test/groovy/io/micronaut/mqtt/hivemq/v5/client/V5WillMessageSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |