KeeperContainerService { public RedisKeeperServer add(KeeperTransMeta keeperTransMeta) { KeeperMeta keeperMeta = keeperTransMeta.getKeeperMeta(); enrichKeeperMetaFromKeeperTransMeta(keeperMeta, keeperTransMeta); String keeperServerKey = assembleKeeperServerKey(keeperTransMeta); if (!redisKeeperServers.containsKey(keeperServerKey)) { synchronized (this) { if (!redisKeeperServers.containsKey(keeperServerKey)) { if (runningPorts.contains(keeperMeta.getPort())) { throw new RedisKeeperRuntimeException( new ErrorMessage<>(KeeperContainerErrorCode.KEEPER_ALREADY_EXIST, String.format("Add keeper for cluster %s shard %s failed since port %d is already used", keeperTransMeta.getClusterId(), keeperTransMeta.getShardId(), keeperMeta .getPort())), null); } try { RedisKeeperServer redisKeeperServer = doAdd(keeperTransMeta, keeperMeta); cacheKeeper(keeperServerKey, redisKeeperServer); return redisKeeperServer; } catch (Throwable ex) { throw new RedisKeeperRuntimeException( new ErrorMessage<>(KeeperContainerErrorCode.INTERNAL_EXCEPTION, String.format("Add keeper for cluster %s shard %s failed", keeperTransMeta.getClusterId(), keeperTransMeta.getShardId())), ex); } } } } throw new RedisKeeperRuntimeException( new ErrorMessage<>(KeeperContainerErrorCode.KEEPER_ALREADY_EXIST, String.format("Keeper already exists for cluster %s shard %s", keeperTransMeta.getClusterId(), keeperTransMeta.getShardId())), null); }  RedisKeeperServer add(KeeperTransMeta keeperTransMeta); RedisKeeperServer addOrStart(KeeperTransMeta keeperTransMeta); List<RedisKeeperServer> list(); void start(String clusterId, String shardId); void stop(String clusterId, String shardId); void remove(String clusterId, String shardId);  }
@Test public void testAddKeeper() throws Exception { RedisKeeperServer redisKeeperServer = keeperContainerService.add(someKeeperTransMeta); verify(componentRegistry, times(1)).add(redisKeeperServer); assertEquals(someCluster, redisKeeperServer.getClusterId()); assertEquals(someShard, redisKeeperServer.getShardId()); assertEquals(somePort, redisKeeperServer.getListeningPort()); }
@Test(expected = RedisKeeperRuntimeException.class) public void testAddKeeperWithSameClusterAndShardMultipleTimes() throws Exception { keeperContainerService.add(someKeeperTransMeta); keeperContainerService.add(someKeeperTransMeta); }
@Test(expected = RedisKeeperRuntimeException.class) public void testAddKeeperWithSamePortMultipleTimes() throws Exception { String anotherShard = "anotherShard"; KeeperMeta anotherKeeperMeta = new KeeperMeta(); anotherKeeperMeta.setPort(somePort); KeeperTransMeta anotherKeeperTransMeta = new KeeperTransMeta(); anotherKeeperTransMeta.setKeeperMeta(anotherKeeperMeta); anotherKeeperTransMeta.setClusterId(someCluster); anotherKeeperTransMeta.setShardId(anotherShard); keeperContainerService.add(someKeeperTransMeta); keeperContainerService.add(anotherKeeperTransMeta); }
@Test public void testAddKeeperWithError() throws Exception { Exception someException = new Exception("some error happened"); when(componentRegistry.add(any(RedisKeeperServer.class))).thenThrow(someException); Exception cause = null; try { keeperContainerService.add(someKeeperTransMeta); } catch (RedisKeeperRuntimeException ex) { cause = (Exception) ex.getCause(); } assertEquals(someException, cause); }