Browse Source

Improve SSL reload test assertions

pull/15301/head
Andrii Landiak 6 months ago
parent
commit
ba8d9af362
  1. 7
      common/transport/mqtt/src/test/java/org/thingsboard/server/transport/mqtt/MqttSslHandlerProviderTest.java
  2. 11
      common/transport/transport-api/src/test/java/org/thingsboard/server/common/transport/service/CertificateReloadManagerTest.java

7
common/transport/mqtt/src/test/java/org/thingsboard/server/transport/mqtt/MqttSslHandlerProviderTest.java

@ -33,6 +33,8 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@ -119,13 +121,13 @@ public class MqttSslHandlerProviderTest {
CountDownLatch startLatch = new CountDownLatch(1);
CountDownLatch doneLatch = new CountDownLatch(5);
List<SslHandler> handlers = new CopyOnWriteArrayList<>();
for (int i = 0; i < 5; i++) {
new Thread(() -> {
try {
startLatch.await();
SslHandler handler = sslHandlerProvider.getSslHandler();
assertThat(handler).isNotNull();
handlers.add(sslHandlerProvider.getSslHandler());
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
@ -138,6 +140,7 @@ public class MqttSslHandlerProviderTest {
boolean completed = doneLatch.await(5, TimeUnit.SECONDS);
assertThat(completed).isTrue();
assertThat(handlers).hasSize(5).allSatisfy(h -> assertThat(h).isNotNull());
// Concurrent handshakes read the same pre-built context without the old sync bottleneck.
SSLContext contextAfter = (SSLContext) ReflectionTestUtils.getField(sslHandlerProvider, "sslContext");
assertThat(contextAfter).isSameAs(contextBefore);

11
common/transport/transport-api/src/test/java/org/thingsboard/server/common/transport/service/CertificateReloadManagerTest.java

@ -24,7 +24,10 @@ import org.springframework.test.util.ReflectionTestUtils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@ -99,6 +102,9 @@ public class CertificateReloadManagerTest {
certificateReloadManager.registerWatcher("test-cert", certFile, reloadCount::incrementAndGet);
long bumpedMtime = Files.getLastModifiedTime(certFile).toMillis() + 5_000L;
Files.setLastModifiedTime(certFile, FileTime.fromMillis(bumpedMtime));
ReflectionTestUtils.invokeMethod(certificateReloadManager, "checkCertificates");
assertThat(reloadCount.get()).isEqualTo(0);
@ -120,10 +126,13 @@ public class CertificateReloadManagerTest {
@Test
public void givenWatcherRegistered_whenShutdown_thenShouldStopScheduler() throws Exception {
certificateReloadManager.registerWatcher("test-cert", certFile, () -> {});
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
ReflectionTestUtils.setField(certificateReloadManager, "scheduler", scheduler);
certificateReloadManager.destroy();
assertThat(certificateReloadManager).isNotNull();
assertThat(scheduler.isShutdown()).isTrue();
assertThat(scheduler.isTerminated()).isTrue();
}
@Test

Loading…
Cancel
Save