2017-02-01 16:23:49 +01:00
|
|
|
|
2016-07-17 14:24:19 +02:00
|
|
|
package io.moquette.spi;
|
|
|
|
|
|
2017-03-25 16:05:45 +01:00
|
|
|
import io.moquette.persistence.MemoryStorageService;
|
2016-07-17 14:24:19 +02:00
|
|
|
import io.moquette.spi.impl.subscriptions.Subscription;
|
|
|
|
|
import io.moquette.spi.impl.subscriptions.SubscriptionsStore;
|
2017-02-14 17:23:04 +01:00
|
|
|
import io.moquette.spi.impl.subscriptions.Topic;
|
2017-01-28 10:06:30 +01:00
|
|
|
import static io.netty.handler.codec.mqtt.MqttQoS.*;
|
2016-07-17 14:24:19 +02:00
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
|
|
|
|
public class ClientSessionTest {
|
|
|
|
|
|
|
|
|
|
ClientSession session1;
|
|
|
|
|
ClientSession session2;
|
|
|
|
|
ISessionsStore sessionsStore;
|
|
|
|
|
SubscriptionsStore store;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() {
|
|
|
|
|
store = new SubscriptionsStore();
|
|
|
|
|
MemoryStorageService storageService = new MemoryStorageService();
|
|
|
|
|
this.sessionsStore = storageService.sessionsStore();
|
|
|
|
|
store.init(sessionsStore);
|
|
|
|
|
|
|
|
|
|
session1 = sessionsStore.createNewSession("SESSION_ID_1", true);
|
|
|
|
|
session2 = sessionsStore.createNewSession("SESSION_ID_2", true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void overridingSubscriptions() {
|
|
|
|
|
// Subscribe on /topic with QOSType.MOST_ONE
|
2017-02-14 17:23:04 +01:00
|
|
|
Subscription oldSubscription = new Subscription(session1.clientID, new Topic("/topic"), AT_MOST_ONCE);
|
2016-07-17 14:24:19 +02:00
|
|
|
session1.subscribe(oldSubscription);
|
|
|
|
|
store.add(oldSubscription.asClientTopicCouple());
|
|
|
|
|
|
|
|
|
|
// Subscribe on /topic again that overrides the previous subscription.
|
2017-02-14 17:23:04 +01:00
|
|
|
Subscription overrindingSubscription = new Subscription(session1.clientID, new Topic("/topic"), EXACTLY_ONCE);
|
2016-07-17 14:24:19 +02:00
|
|
|
session1.subscribe(overrindingSubscription);
|
|
|
|
|
store.add(overrindingSubscription.asClientTopicCouple());
|
|
|
|
|
|
2017-02-01 16:23:49 +01:00
|
|
|
// Verify
|
2017-02-14 17:23:04 +01:00
|
|
|
List<Subscription> subscriptions = store.matches(new Topic("/topic"));
|
2016-07-17 14:24:19 +02:00
|
|
|
assertEquals(1, subscriptions.size());
|
|
|
|
|
Subscription sub = subscriptions.get(0);
|
|
|
|
|
assertEquals(overrindingSubscription.getRequestedQos(), sub.getRequestedQos());
|
|
|
|
|
}
|
|
|
|
|
}
|