RedisServiceImpl extends AbstractConsoleService<RedisTblDao> implements RedisService { protected void notifyClusterUpdate(String dcName, String clusterName) { ClusterTbl cluster = clusterService.find(clusterName); if (null == cluster) throw new IllegalArgumentException("not exist cluster " + clusterName); if (ClusterType.lookup(cluster.getClusterType()).supportMultiActiveDC()) { List<DcTbl> dcTbls = dcService.findClusterRelatedDc(clusterName); if (null != dcTbls) notifier.notifyClusterUpdate(clusterName, dcTbls.stream().map(DcTbl::getDcName).collect(Collectors.toList())); } else { notifier.notifyClusterUpdate(clusterName, Collections.singletonList(dcName)); } }  @Override RedisTbl find(final long id); @Override List<RedisTbl> findAllRedisWithSameIP(String ip); @Override List<RedisTbl> findAllByDcClusterShard(final long dcClusterShardId); @Override List<RedisTbl> findAllRedisesByDcClusterName(String dcId, String clusterId); @Override List<RedisTbl> findAllByDcClusterShard(String dcId, String clusterId, String shardId); @Override List<RedisTbl> findRedisesByDcClusterShard(String dcId, String clusterId, String shardId); @Override List<RedisTbl> findKeepersByDcClusterShard(String dcId, String clusterId, String shardId); @Override synchronized void insertRedises(String dcId, String clusterId, String shardId,
                                           List<Pair<String, Integer>> redisAddresses); @Override synchronized int insertKeepers(String dcId, String clusterId, String shardId,
                                          List<KeeperBasicInfo> keepers); @Override List<RedisTbl> deleteKeepers(String dcId, String clusterId, String shardId); @Override void deleteRedises(String dcId, String clusterId, String shardId, List<Pair<String, Integer>> redisAddresses); @Override void updateByPK(final RedisTbl redis); @Override void updateBatchMaster(List<RedisTbl> redises); @Override void updateBatchKeeperActive(List<RedisTbl> redises); @Override void updateRedises(String dcName, String clusterName, String shardName, ShardModel shardModel); @Override List<RedisTbl> findAllKeeperContainerCountInfo(); @Override List<Long> findClusterIdsByKeeperContainer(long keeperContainerId); void insertInstancesToDb(long dcClusterShardId, String role, Pair<String, Integer>... addrs);  }
@Test public void testNotifyBiDirectionClusterUpdate() { ClusterModel clusterModel = new ClusterModel(); clusterModel.setClusterTbl(new ClusterTbl().setClusterName("bi-test") .setClusterType(ClusterType.BI_DIRECTION.toString()) .setActivedcId(1) .setClusterDescription("desc") .setClusterAdminEmails("test@ctrip.com")); clusterModel.setDcs(Arrays.asList(new DcTbl().setDcName("jq"), new DcTbl().setDcName("oy"))); redisService.clusterService.createCluster(clusterModel); ClusterMetaModifiedNotifier notifier = Mockito.mock(ClusterMetaModifiedNotifier.class); redisService.notifier = notifier; Mockito.doAnswer(invocation -> { String clusterName = invocation.getArgumentAt(0, String.class); List<String> dcs = invocation.getArgumentAt(1, List.class); Assert.assertEquals("bi-test", clusterName); Assert.assertEquals(2, dcs.size()); Assert.assertTrue(dcs.contains("jq") && dcs.contains("oy")); return null; }).when(notifier).notifyClusterUpdate(Mockito.anyString(), Mockito.anyList()); redisService.notifyClusterUpdate("jq", "bi-test"); Mockito.verify(notifier, Mockito.times(1)).notifyClusterUpdate(Mockito.anyString(), Mockito.anyList()); }
@Test public void testNotifyOneWayClusterUpdate() { ClusterMetaModifiedNotifier notifier = Mockito.mock(ClusterMetaModifiedNotifier.class); redisService.notifier = notifier; Mockito.doAnswer(invocation -> { String paramClusterName = invocation.getArgumentAt(0, String.class); List<String> dcs = invocation.getArgumentAt(1, List.class); Assert.assertEquals(clusterName, paramClusterName); Assert.assertEquals(1, dcs.size()); Assert.assertTrue(dcs.contains("jq")); return null; }).when(notifier).notifyClusterUpdate(Mockito.anyString(), Mockito.anyList()); redisService.notifyClusterUpdate("jq", clusterName); Mockito.verify(notifier, Mockito.times(1)).notifyClusterUpdate(Mockito.anyString(), Mockito.anyList()); }