forked from ozimov/embedded-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encapsulated redis exceptions into a ... well a redis exception; simp…
…le redis cluster introduced and tested (minimally)
- Loading branch information
Showing
7 changed files
with
170 additions
and
23 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
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
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,14 @@ | ||
package redis.embedded; | ||
|
||
import redis.embedded.exceptions.EmbeddedRedisException; | ||
|
||
/** | ||
* Created by piotrturek on 22/01/15. | ||
*/ | ||
public interface Redis { | ||
boolean isActive(); | ||
|
||
void start() throws EmbeddedRedisException; | ||
|
||
void stop() throws EmbeddedRedisException; | ||
} |
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,36 @@ | ||
package redis.embedded; | ||
|
||
import redis.embedded.exceptions.EmbeddedRedisException; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by piotrturek on 22/01/15. | ||
*/ | ||
public class RedisCluster implements Redis { | ||
private final List<Redis> sentinels = new LinkedList<>(); | ||
private final List<Redis> servers = new LinkedList<>(); | ||
|
||
public RedisCluster(List<Redis> sentinels, List<Redis> servers) { | ||
this.servers.addAll(servers); | ||
this.sentinels.addAll(sentinels); | ||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return sentinels.stream().allMatch(Redis::isActive) && servers.stream().allMatch(Redis::isActive); | ||
} | ||
|
||
@Override | ||
public void start() throws EmbeddedRedisException { | ||
sentinels.parallelStream().forEach(Redis::start); | ||
servers.parallelStream().forEach(Redis::start); | ||
} | ||
|
||
@Override | ||
public void stop() throws EmbeddedRedisException { | ||
servers.parallelStream().forEach(Redis::stop); | ||
sentinels.parallelStream().forEach(Redis::stop); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
src/main/java/redis/embedded/exceptions/EmbeddedRedisException.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,14 @@ | ||
package redis.embedded.exceptions; | ||
|
||
/** | ||
* Created by piotrturek on 22/01/15. | ||
*/ | ||
public class EmbeddedRedisException extends RuntimeException { | ||
public EmbeddedRedisException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public EmbeddedRedisException(String message) { | ||
super(message); | ||
} | ||
} |
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,78 @@ | ||
package redis.embedded; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public class RedisClusterTest { | ||
private Redis sentinel1; | ||
private Redis sentinel2; | ||
private Redis master1; | ||
private Redis master2; | ||
|
||
private RedisCluster instance; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
sentinel1 = mock(Redis.class); | ||
sentinel2 = mock(Redis.class); | ||
master1 = mock(Redis.class); | ||
master2 = mock(Redis.class); | ||
} | ||
|
||
|
||
@Test | ||
public void stopShouldStopEntireCluster() throws Exception { | ||
//given | ||
final List<Redis> sentinels = Arrays.asList(sentinel1, sentinel2); | ||
final List<Redis> servers = Arrays.asList(master1, master2); | ||
instance = new RedisCluster(sentinels, servers); | ||
|
||
//when | ||
instance.stop(); | ||
|
||
//then | ||
sentinels.stream().forEach(s -> verify(s).stop()); | ||
servers.stream().forEach(m -> verify(m).stop()); | ||
} | ||
|
||
@Test | ||
public void startShouldStartEntireCluster() throws Exception { | ||
//given | ||
final List<Redis> sentinels = Arrays.asList(sentinel1, sentinel2); | ||
final List<Redis> servers = Arrays.asList(master1, master2); | ||
instance = new RedisCluster(sentinels, servers); | ||
|
||
//when | ||
instance.start(); | ||
|
||
//then | ||
sentinels.stream().forEach(s -> verify(s).start()); | ||
servers.stream().forEach(m -> verify(m).start()); | ||
} | ||
|
||
@Test | ||
public void isActiveShouldCheckEntireClusterIfAllActive() throws Exception { | ||
//given | ||
given(sentinel1.isActive()).willReturn(true); | ||
given(sentinel2.isActive()).willReturn(true); | ||
given(master1.isActive()).willReturn(true); | ||
given(master2.isActive()).willReturn(true); | ||
final List<Redis> sentinels = Arrays.asList(sentinel1, sentinel2); | ||
final List<Redis> servers = Arrays.asList(master1, master2); | ||
instance = new RedisCluster(sentinels, servers); | ||
|
||
//when | ||
instance.isActive(); | ||
|
||
//then | ||
sentinels.stream().forEach(s -> verify(s).isActive()); | ||
servers.stream().forEach(m -> verify(m).isActive()); | ||
} | ||
} |