Skip to content

Commit 2c7abd0

Browse files
authored
Test: Reduce flaky-ness of CLIENT PAUSE tests (#3243)
1 parent 13321d2 commit 2c7abd0

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

src/test/java/redis/clients/jedis/commands/jedis/ControlCommandsTest.java

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redis.clients.jedis.commands.jedis;
22

33
import static org.hamcrest.Matchers.greaterThan;
4+
import static org.hamcrest.Matchers.lessThan;
45
import static org.junit.Assert.assertArrayEquals;
56
import static org.junit.Assert.assertEquals;
67
import static org.junit.Assert.assertFalse;
@@ -250,12 +251,9 @@ public void waitReplicas() {
250251
@Test
251252
public void clientPause() throws InterruptedException, ExecutionException {
252253
ExecutorService executorService = Executors.newFixedThreadPool(2);
253-
try {
254-
final Jedis jedisToPause1 = createJedis();
255-
final Jedis jedisToPause2 = createJedis();
254+
try (Jedis jedisToPause1 = createJedis(); Jedis jedisToPause2 = createJedis();) {
256255

257-
int pauseMillis = 1250;
258-
jedis.clientPause(pauseMillis);
256+
jedis.clientPause(1000L);
259257

260258
Future<Long> latency1 = executorService.submit(new Callable<Long>() {
261259
@Override
@@ -274,32 +272,23 @@ public Long call() throws Exception {
274272
}
275273
});
276274

277-
long latencyMillis1 = latency1.get();
278-
long latencyMillis2 = latency2.get();
279-
280-
int pauseMillisDelta = 100;
281-
assertTrue(pauseMillis <= latencyMillis1 && latencyMillis1 <= pauseMillis + pauseMillisDelta);
282-
assertTrue(pauseMillis <= latencyMillis2 && latencyMillis2 <= pauseMillis + pauseMillisDelta);
275+
assertThat(latency1.get(), greaterThan(100L));
276+
assertThat(latency2.get(), greaterThan(100L));
283277

284-
jedisToPause1.close();
285-
jedisToPause2.close();
286278
} finally {
287279
executorService.shutdown();
288-
if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
280+
if (!executorService.awaitTermination(2, TimeUnit.SECONDS)) {
289281
executorService.shutdownNow();
290282
}
291283
}
292284
}
293285

294286
@Test
295287
public void clientPauseAll() throws InterruptedException, ExecutionException {
296-
final int pauseMillis = 1250;
297-
final int pauseMillisDelta = 100;
298-
299288
ExecutorService executorService = Executors.newFixedThreadPool(1);
300289
try (Jedis jedisPause = createJedis()) {
301290

302-
jedis.clientPause(pauseMillis, ClientPauseMode.ALL);
291+
jedis.clientPause(1000L, ClientPauseMode.ALL);
303292

304293
Future<Long> latency = executorService.submit(new Callable<Long>() {
305294
@Override
@@ -310,8 +299,7 @@ public Long call() throws Exception {
310299
}
311300
});
312301

313-
long latencyMillis = latency.get();
314-
assertTrue(pauseMillis <= latencyMillis && latencyMillis <= pauseMillis + pauseMillisDelta);
302+
assertThat(latency.get(), greaterThan(100L));
315303

316304
} finally {
317305
executorService.shutdown();
@@ -323,13 +311,10 @@ public Long call() throws Exception {
323311

324312
@Test
325313
public void clientPauseWrite() throws InterruptedException, ExecutionException {
326-
final int pauseMillis = 1250;
327-
final int pauseMillisDelta = 100;
328-
329314
ExecutorService executorService = Executors.newFixedThreadPool(2);
330315
try (Jedis jedisRead = createJedis(); Jedis jedisWrite = createJedis();) {
331316

332-
jedis.clientPause(pauseMillis, ClientPauseMode.WRITE);
317+
jedis.clientPause(1000L, ClientPauseMode.WRITE);
333318

334319
Future<Long> latencyRead = executorService.submit(new Callable<Long>() {
335320
@Override
@@ -348,11 +333,9 @@ public Long call() throws Exception {
348333
}
349334
});
350335

351-
long latencyReadMillis = latencyRead.get();
352-
assertTrue(0 <= latencyReadMillis && latencyReadMillis <= pauseMillisDelta);
336+
assertThat(latencyRead.get(), lessThan(100L));
353337

354-
long latencyWriteMillis = latencyWrite.get();
355-
assertTrue(pauseMillis <= latencyWriteMillis && latencyWriteMillis <= pauseMillis + pauseMillisDelta);
338+
assertThat(latencyWrite.get(), greaterThan(100L));
356339

357340
} finally {
358341
executorService.shutdown();

0 commit comments

Comments
 (0)