-
Notifications
You must be signed in to change notification settings - Fork 818
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use session expiry from CONNECT message properties (#753)
Leverage the existing global expiry harness introduced with #739, reading the expiration time from the CONNECT's MQTT property SESSION_EXPIRY_INTERVAL.
- Loading branch information
Showing
3 changed files
with
73 additions
and
7 deletions.
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
52 changes: 52 additions & 0 deletions
52
broker/src/test/java/io/moquette/broker/SessionRegistryMQTT5Test.java
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,52 @@ | ||
package io.moquette.broker; | ||
|
||
import io.netty.handler.codec.mqtt.MqttConnectMessage; | ||
import io.netty.handler.codec.mqtt.MqttMessageBuilders; | ||
import io.netty.handler.codec.mqtt.MqttProperties; | ||
import io.netty.handler.codec.mqtt.MqttVersion; | ||
import org.awaitility.Awaitility; | ||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class SessionRegistryMQTT5Test extends SessionRegistryTest { | ||
private static final Logger LOG = LoggerFactory.getLogger(SessionRegistryMQTT5Test.class); | ||
|
||
@Test | ||
public void givenSessionWithConnectionExpireTimeWhenAfterExpirationIsPassedThenSessionIsRemoved() { | ||
LOG.info("givenSessionWithExpireTimeWhenAfterExpirationIsPassedThenSessionIsRemoved"); | ||
|
||
// insert a not clean session that should expire in connect selected expiry time | ||
final String clientId = "client_to_be_removed"; | ||
final MqttProperties connectProperties = new MqttProperties(); | ||
int customExpirySeconds = 60; | ||
connectProperties.add(new MqttProperties.IntegerProperty(MqttProperties.MqttPropertyType.SESSION_EXPIRY_INTERVAL.value(), customExpirySeconds)); | ||
final MqttConnectMessage connectMessage = MqttMessageBuilders.connect() | ||
.protocolVersion(MqttVersion.MQTT_5) | ||
.cleanSession(false) | ||
.properties(connectProperties) | ||
.build(); | ||
final SessionRegistry.SessionCreationResult res = sut.createOrReopenSession(connectMessage, clientId, "User"); | ||
assertEquals(SessionRegistry.CreationModeEnum.CREATED_CLEAN_NEW, res.mode, "Not clean session must be created"); | ||
|
||
// remove it, so that it's tracked in the inner delay queue | ||
sut.connectionClosed(res.session); | ||
assertEquals(1, sessionRepository.list().size(), "Not clean session must be persisted"); | ||
|
||
// move time forward | ||
Duration moreThenSessionExpiration = Duration.ofSeconds(customExpirySeconds).plusSeconds(10); | ||
slidingClock.forward(moreThenSessionExpiration); | ||
|
||
// check the session has been removed | ||
Awaitility | ||
.await() | ||
.atMost(3 * SessionRegistry.EXPIRED_SESSION_CLEANER_TASK_INTERVAL.toMillis(), TimeUnit.MILLISECONDS) | ||
.until(sessionsList(), Matchers.empty()); | ||
} | ||
} |
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