forked from zxl0714/redis-mock
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement isRunning and fix a minor thread leak on RedisServer::stop (#…
…483)
- Loading branch information
1 parent
4246c43
commit cd0602c
Showing
2 changed files
with
89 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/test/java/com/github/fppt/jedismock/TestStartStop.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.github.fppt.jedismock; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import redis.clients.jedis.Jedis; | ||
|
||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class TestStartStop { | ||
@Test | ||
void testMultipleServerReuse() throws IOException { | ||
Set<String> oldThreads = threadNames(); | ||
RedisServer server = RedisServer.newRedisServer(); | ||
for (int i = 0; i < 2000; i++) { | ||
startCheckStop(server); | ||
} | ||
Set<String> newThreads = threadNames(); | ||
newThreads.removeAll(oldThreads); | ||
Assertions.assertThat(newThreads).hasSizeLessThan(10); | ||
} | ||
|
||
@Test | ||
void testMultipleServerCreation() throws IOException { | ||
Set<String> oldThreads = threadNames(); | ||
for (int i = 0; i < 2000; i++) { | ||
RedisServer server = RedisServer.newRedisServer(); | ||
startCheckStop(server); | ||
} | ||
Set<String> newThreads = threadNames(); | ||
newThreads.removeAll(oldThreads); | ||
Assertions.assertThat(newThreads).hasSizeLessThan(10); | ||
} | ||
|
||
@Test | ||
void testStartAlreadyStarted() throws IOException { | ||
RedisServer server = RedisServer.newRedisServer(); | ||
server.start(); | ||
try { | ||
Assertions.assertThatThrownBy(server::start).isInstanceOf(IllegalStateException.class); | ||
} finally { | ||
server.stop(); | ||
} | ||
} | ||
|
||
@Test | ||
void testStopNotStarted() { | ||
RedisServer server = RedisServer.newRedisServer(); | ||
Assertions.assertThatThrownBy(server::stop).hasMessageContaining("is not running"); | ||
} | ||
|
||
private static Set<String> threadNames() { | ||
return Thread.getAllStackTraces() | ||
.keySet() | ||
.stream() | ||
.map(Thread::getName) | ||
.collect(Collectors.toCollection(HashSet::new)); | ||
} | ||
|
||
private static void startCheckStop(RedisServer server) throws IOException { | ||
Assertions.assertThat(server.isRunning()).isFalse(); | ||
server.start(); | ||
Assertions.assertThat(server.isRunning()).isTrue(); | ||
try (Jedis jedis = new Jedis(server.getHost(), server.getBindPort())) { | ||
Assertions.assertThat(jedis.ping()).isEqualTo("PONG"); | ||
} | ||
server.stop(); | ||
Assertions.assertThat(server.isRunning()).isFalse(); | ||
} | ||
|
||
} |