AverageAllocationJobShardingStrategy implements JobShardingStrategy { @Override public Map<String, List<Integer>> sharding(final List<String> serversList, final JobShardingStrategyOption option) { if (serversList.isEmpty()) { return Collections.emptyMap(); } Map<String, List<Integer>> result = shardingAliquot(serversList, option.getShardingTotalCount()); addAliquant(serversList, option.getShardingTotalCount(), result); return result; }  @Override Map<String, List<Integer>> sharding(final List<String> serversList, final JobShardingStrategyOption option);  }
@Test public void shardingForZeroServer() { assertThat(jobShardingStrategy.sharding(Collections.<String>emptyList(), getJobShardingStrategyOption(3)), is(Collections.EMPTY_MAP)); }
@Test public void shardingForOneServer() { Map<String, List<Integer>> expected = new LinkedHashMap<>(1); expected.put("host0", Arrays.asList(0, 1, 2)); assertThat(jobShardingStrategy.sharding(Collections.singletonList("host0"), getJobShardingStrategyOption(3)), is(expected)); }
@Test public void shardingForServersMoreThanShardingCount() { Map<String, List<Integer>> expected = new LinkedHashMap<>(3); expected.put("host0", Collections.singletonList(0)); expected.put("host1", Collections.singletonList(1)); expected.put("host2", Collections.<Integer>emptyList()); assertThat(jobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), getJobShardingStrategyOption(2)), is(expected)); }
@Test public void shardingForServersLessThanShardingCountAliquot() { Map<String, List<Integer>> expected = new LinkedHashMap<>(3); expected.put("host0", Arrays.asList(0, 1, 2)); expected.put("host1", Arrays.asList(3, 4, 5)); expected.put("host2", Arrays.asList(6, 7, 8)); assertThat(jobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), getJobShardingStrategyOption(9)), is(expected)); }
@Test public void shardingForServersLessThanShardingCountAliquantFor8ShardingCountAnd3Servers() { Map<String, List<Integer>> expected = new LinkedHashMap<>(3); expected.put("host0", Arrays.asList(0, 1, 6)); expected.put("host1", Arrays.asList(2, 3, 7)); expected.put("host2", Arrays.asList(4, 5)); assertThat(jobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), getJobShardingStrategyOption(8)), is(expected)); }
@Test public void shardingForServersLessThanShardingCountAliquantFor10ShardingCountAnd3Servers() { Map<String, List<Integer>> expected = new LinkedHashMap<>(3); expected.put("host0", Arrays.asList(0, 1, 2, 9)); expected.put("host1", Arrays.asList(3, 4, 5)); expected.put("host2", Arrays.asList(6, 7, 8)); assertThat(jobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), getJobShardingStrategyOption(10)), is(expected)); }
@Test public void shardingForOneServer() { Map<String, List<Integer>> expected = new LinkedHashMap<String, List<Integer>>(1); expected.put("host0", Arrays.asList(0, 1, 2)); assertThat(jobShardingStrategy.sharding(Collections.singletonList("host0"), getJobShardingStrategyOption(3)), is(expected)); }
@Test public void shardingForServersMoreThanShardingCount() { Map<String, List<Integer>> expected = new LinkedHashMap<String, List<Integer>>(3); expected.put("host0", Collections.singletonList(0)); expected.put("host1", Collections.singletonList(1)); expected.put("host2", Collections.<Integer>emptyList()); assertThat(jobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), getJobShardingStrategyOption(2)), is(expected)); }
@Test public void shardingForServersLessThanShardingCountAliquot() { Map<String, List<Integer>> expected = new LinkedHashMap<String, List<Integer>>(3); expected.put("host0", Arrays.asList(0, 1, 2)); expected.put("host1", Arrays.asList(3, 4, 5)); expected.put("host2", Arrays.asList(6, 7, 8)); assertThat(jobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), getJobShardingStrategyOption(9)), is(expected)); }
@Test public void shardingForServersLessThanShardingCountAliquantFor8ShardingCountAnd3Servers() { Map<String, List<Integer>> expected = new LinkedHashMap<String, List<Integer>>(3); expected.put("host0", Arrays.asList(0, 1, 6)); expected.put("host1", Arrays.asList(2, 3, 7)); expected.put("host2", Arrays.asList(4, 5)); assertThat(jobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), getJobShardingStrategyOption(8)), is(expected)); }
@Test public void shardingForServersLessThanShardingCountAliquantFor10ShardingCountAnd3Servers() { Map<String, List<Integer>> expected = new LinkedHashMap<String, List<Integer>>(3); expected.put("host0", Arrays.asList(0, 1, 2, 9)); expected.put("host1", Arrays.asList(3, 4, 5)); expected.put("host2", Arrays.asList(6, 7, 8)); assertThat(jobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), getJobShardingStrategyOption(10)), is(expected)); }