|
8 | 8 | import org.junit.Assert; |
9 | 9 | import org.junit.Test; |
10 | 10 |
|
| 11 | +import java.util.HashMap; |
11 | 12 | import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.concurrent.atomic.AtomicInteger; |
| 15 | +import java.util.stream.Stream; |
12 | 16 |
|
13 | 17 | public class BuildForestReplicaTest extends Assert { |
14 | 18 |
|
@@ -37,6 +41,56 @@ public void multipleForestsOnEachHost() { |
37 | 41 | assertEquals("host2", forests.get(5).getForestReplica().get(0).getHost()); |
38 | 42 | } |
39 | 43 |
|
| 44 | + /** |
| 45 | + * Test was added for https://github.com/marklogic-community/ml-app-deployer/issues/423, where a bug was detected |
| 46 | + * when the "host pointer" in the implementation code could exceed the number of hosts. This test reproduced the bug. |
| 47 | + * Just doing some basic assertions on the number of forests and replicas created, but the key is that the test no |
| 48 | + * longer fails. |
| 49 | + */ |
| 50 | + @Test |
| 51 | + public void sameNumberOfForestsAsHosts() { |
| 52 | + AppConfig appConfig = newAppConfig("mlForestsPerHost", "db,4"); |
| 53 | + |
| 54 | + List<Forest> forests = builder.buildForests( |
| 55 | + new ForestPlan("db", "host1", "host2", "host3", "host4").withReplicaCount(3), appConfig); |
| 56 | + |
| 57 | + assertEquals(16, forests.size()); |
| 58 | + forests.forEach(forest -> { |
| 59 | + assertEquals(3, forest.getForestReplica().size()); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Similar to the above test, just even more forests. |
| 65 | + */ |
| 66 | + @Test |
| 67 | + public void numberOfForestsPerHostIsMoreThanDoubleTheNumberOfHosts() { |
| 68 | + AppConfig appConfig = newAppConfig("mlForestsPerHost", "db,10"); |
| 69 | + |
| 70 | + List<Forest> forests = builder.buildForests( |
| 71 | + new ForestPlan("db", "host1", "host2", "host3", "host4").withReplicaCount(3), appConfig); |
| 72 | + |
| 73 | + assertEquals(40, forests.size()); |
| 74 | + |
| 75 | + Map<String, AtomicInteger> hostToReplicaCounts = new HashMap<>(); |
| 76 | + Stream.of("host1", "host2", "host3", "host4").forEach(host -> hostToReplicaCounts.put(host, new AtomicInteger(0))); |
| 77 | + |
| 78 | + AtomicInteger replicaCount = new AtomicInteger(0); |
| 79 | + |
| 80 | + forests.forEach(forest -> { |
| 81 | + assertEquals(3, forest.getForestReplica().size()); |
| 82 | + forest.getForestReplica().forEach(replica -> { |
| 83 | + hostToReplicaCounts.get(replica.getHost()).getAndIncrement(); |
| 84 | + replicaCount.getAndIncrement(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + assertEquals("Expecting 120 replicas; we have 40 forests, and we expect 3 replicas for each", 120, replicaCount.get()); |
| 89 | + Stream.of("host1", "host2", "host3", "host4").forEach(host -> { |
| 90 | + assertEquals("Each host should have 30 replicas, as there are 120 total", 30, hostToReplicaCounts.get(host).get()); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
40 | 94 | @Test |
41 | 95 | public void customNamingStrategyWithDistributedStrategy() { |
42 | 96 | AppConfig appConfig = newAppConfig("mlForestsPerHost", "my-database,2"); |
|
0 commit comments