GatewaySessionHandlerTest createWeakMap() test simplified
This commit is contained in:
parent
4705be61f1
commit
6949d5bca7
@ -15,47 +15,21 @@
|
|||||||
*/
|
*/
|
||||||
package org.thingsboard.server.transport.mqtt.session;
|
package org.thingsboard.server.transport.mqtt.session;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||||
|
|
||||||
import java.util.WeakHashMap;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
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.BDDMockito.willCallRealMethod;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
public class GatewaySessionHandlerTest {
|
public class GatewaySessionHandlerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenWeakHashMap_WhenGC_thenMapIsEmpty() {
|
public void givenGatewaySessionHandler_WhenCreateWeakMap_thenConcurrentReferenceHashMapClass() {
|
||||||
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);
|
GatewaySessionHandler gsh = mock(GatewaySessionHandler.class);
|
||||||
willCallRealMethod().given(gsh).createWeakMap();
|
willCallRealMethod().given(gsh).createWeakMap();
|
||||||
|
|
||||||
ConcurrentMap<String, Lock> map = gsh.createWeakMap();
|
assertThat(gsh.createWeakMap()).isInstanceOf(ConcurrentReferenceHashMap.class);
|
||||||
map.put("device", new ReentrantLock());
|
|
||||||
assertTrue(map.containsKey("device"));
|
|
||||||
|
|
||||||
System.gc();
|
|
||||||
|
|
||||||
await().atMost(10, TimeUnit.SECONDS).until(() -> !map.containsKey("device"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user