kstyrc / embedded-redis

Redis embedded server for Java integration testing
854 stars 371 forks source link

Added Sentinel support as well as an ability to create arbitrary clusters on arbitrary or >>ephemeral<< ports #22

Closed turu closed 9 years ago

turu commented 9 years ago

Following changes have been introduced (updated on 01.02.2015):

Basically, now you can write a test like:

public class SomeIntegrationTestThatRequiresRedis {
  private RedisCluster cluster;

  @Before
  public void setup() throws Exception {
    //creates a cluster with 3 sentinels, quorum size of 2 and 3 replication groups, each with one master and one slave
    cluster = RedisCluster.builder().sentinelCount(3).quorumSize(2)
                    .replicationGroup("master1", 1)
                    .replicationGroup("master2", 1)
                    .replicationGroup("master3", 1)
                    .build();
    cluster.start();
  }

  @Test
  public void test() throws Exception {
    // testing code that requires redis running
  }

  @After
  public void tearDown() throws Exception {
    cluster.stop();
  }
}

You can create an arbitrary Redis cluster that way. The one above is a Redis HA cluster with 3 sentinels (quorum size 2) and 3 masters, each having their own replication slave.

To achieve that, a major refactor was needed as well. Tell me what you think.

EDIT on 01.02.2015: We encountered a scenario when you can have many builds using embedded-redis in tests, running simultaneously on CI server. Therefore I implemented a feature that allows one to create clusters on predefined or free ephemeral ports that are selected automatically to avoid possible clashes or race conditions.

Example usage is shown in updated README.md (see Setting up a cluster section).

kstyrc commented 9 years ago

That's a really good job!

I'd like to see this PR + support for defining executable per OS in the next release. My concern:

Let me look through the code and new Redis concepts (new to me).

turu commented 9 years ago

Hello there, See the updated title and description with additional features that have been added to this pull request. Cheers

kstyrc commented 9 years ago

Thanks a lot and sorry for the delay!