gateway session test added for a weak map with awaitility

This commit is contained in:
Sergey Matvienko 2021-07-30 17:30:02 +03:00 committed by Andrew Shvayka
parent 89e3ba253c
commit 2cad1f9912
2 changed files with 51 additions and 0 deletions

View File

@ -88,6 +88,11 @@
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>

View File

@ -0,0 +1,46 @@
package org.thingsboard.server.transport.mqtt.session;
import org.junit.Test;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.willCallRealMethod;
import static org.mockito.Mockito.mock;
public class GatewaySessionHandlerTest {
@Test
public void givenWeakHashMap_WhenGC_thenMapIsEmpty() {
WeakHashMap<String, Lock> map = new WeakHashMap<>();
String deviceName = new String("device"); //constants are static and doesn't affected by GC, so use new instead
map.put(deviceName, new ReentrantLock());
assertTrue(map.containsKey(deviceName));
deviceName = null;
System.gc();
await().atMost(10, TimeUnit.SECONDS).until(() -> !map.containsKey("device"));
}
@Test
public void givenConcurrentReferenceHashMap_WhenGC_thenMapIsEmpty() {
GatewaySessionHandler gsh = mock(GatewaySessionHandler.class);
willCallRealMethod().given(gsh).createWeakMap();
ConcurrentMap<String, Lock> map = gsh.createWeakMap();
map.put("device", new ReentrantLock());
assertTrue(map.containsKey("device"));
System.gc();
await().atMost(10, TimeUnit.SECONDS).until(() -> !map.containsKey("device"));
}
}