Skip to content

Commit

Permalink
Add test for concurrent connections in ClientPresenceManager
Browse files Browse the repository at this point in the history
  • Loading branch information
eager-signal committed Oct 15, 2024
1 parent 73fb1fc commit 4622729
Showing 1 changed file with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;

import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.cluster.event.ClusterTopologyChangedEvent;
import java.time.Duration;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand All @@ -28,10 +26,13 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.whispersystems.textsecuregcm.redis.RedisClusterExtension;

@Timeout(value = 10, threadMode = Timeout.ThreadMode.SEPARATE_THREAD)
class ClientPresenceManagerTest {

@RegisterExtension
Expand Down Expand Up @@ -129,7 +130,7 @@ void testRemoteDisplacement() {
connection -> connection.sync().set(ClientPresenceManager.getPresenceKey(accountUuid, deviceId),
UUID.randomUUID().toString()));

assertTimeoutPreemptively(Duration.ofSeconds(10), displaced::join);
displaced.join();
}

@Test
Expand All @@ -151,7 +152,7 @@ void testRemoteDisplacementAfterTopologyChange() {
connection -> connection.sync().set(ClientPresenceManager.getPresenceKey(accountUuid, deviceId),
UUID.randomUUID().toString()));

assertTimeoutPreemptively(Duration.ofSeconds(10), displaced::join);
displaced.join();
}

@Test
Expand Down Expand Up @@ -354,7 +355,7 @@ void testSetPresentRemotely() {

server2.setPresent(uuid1, deviceId, connectedElsewhere -> {});

assertTimeoutPreemptively(Duration.ofSeconds(10), displaced::join);
displaced.join();
}

@Test
Expand All @@ -368,7 +369,7 @@ void testDisconnectPresenceLocally() {

server1.disconnectPresence(uuid1, deviceId);

assertTimeoutPreemptively(Duration.ofSeconds(10), displaced::join);
displaced.join();
}

@Test
Expand All @@ -382,7 +383,25 @@ void testDisconnectPresenceRemotely() {

server2.disconnectPresence(uuid1, deviceId);

displaced.join();
}

@RepeatedTest(value = 10, failureThreshold = 1)
void testConcurrentConnection() {
final UUID uuid1 = UUID.randomUUID();
final byte deviceId = 1;

final CompletableFuture<?> displaced = new CompletableFuture<>();
final DisplacedPresenceListener listener1 = connectedElsewhere -> displaced.complete(null);

final Thread server1Thread = new Thread(() -> server1.setPresent(uuid1, deviceId, listener1));
final Thread server2Thread = new Thread(() -> server2.setPresent(uuid1, deviceId, listener1));

server1Thread.start();
server2Thread.start();

assertTimeoutPreemptively(Duration.ofSeconds(10), displaced::join);
}

}
}

0 comments on commit 4622729

Please sign in to comment.