### Question:
CentralizedRule implements Rule, Comparable<CentralizedRule> { public boolean update(SamplingRule i) { boolean rebuild = false; Matchers m = new Matchers(i); lock.writeLock().lock(); try { fixedRate = i.getFixedRate(); if (priority != i.getPriority()) { rebuild = true; } priority = i.getPriority(); fixedRate = i.getFixedRate(); matchers = m; centralizedReservoir.update(i); } finally { lock.writeLock().unlock(); } return rebuild; } CentralizedRule(SamplingRule input, Rand rand); boolean update(SamplingRule i); boolean isStale(Instant now); static boolean isValid(SamplingRule rule); void update(SamplingTargetDocument t, Instant now); SamplingStatisticsDocument snapshot(Date now); boolean match(SamplingRequest r); @Override SamplingResponse sample(Instant now); @Override int compareTo(CentralizedRule other); String getName(); @Override boolean equals(@Nullable Object o); @Override int hashCode(); static final String DEFAULT_RULE_NAME; }### Answer:
@Test public void testRuleUpdateWithInvalidation() { SamplingRule input = createInput("r1", 300, 10, 0.0) .withHTTPMethod("POST") .withServiceName("s1") .withURLPath("/foo/bar"); CentralizedRule r = new CentralizedRule(input, new RandImpl()); SamplingRule update = createInput("r1", 301, 5, 0.5) .withHTTPMethod("GET") .withServiceName("s2") .withURLPath("/bar/foo"); boolean invalidate = r.update(update); Matchers m = Whitebox.getInternalState(r, "matchers", CentralizedRule.class); Assert.assertEquals("GET", Whitebox.getInternalState(m, "method", Matchers.class)); Assert.assertEquals("s2", Whitebox.getInternalState(m, "service", Matchers.class)); Assert.assertEquals("/bar/foo", Whitebox.getInternalState(m, "url", Matchers.class)); Assert.assertTrue(invalidate); }
@Test public void testRuleUpdateWithoutInvalidation() { SamplingRule input = createInput("r1", 300, 10, 0.0) .withHTTPMethod("POST") .withServiceName("s1") .withURLPath("/foo/bar"); CentralizedRule r = new CentralizedRule(input, new RandImpl()); SamplingRule update = createInput("r1", 300, 10, 0.0) .withHTTPMethod("GET") .withServiceName("s2") .withURLPath("/bar/foo"); boolean invalidate = r.update(update); Matchers m = Whitebox.getInternalState(r, "matchers", CentralizedRule.class); Assert.assertEquals("GET", Whitebox.getInternalState(m, "method", Matchers.class)); Assert.assertEquals("s2", Whitebox.getInternalState(m, "service", Matchers.class)); Assert.assertEquals("/bar/foo", Whitebox.getInternalState(m, "url", Matchers.class)); Assert.assertFalse(invalidate); }
@Test public void testTargetUpdate() { SamplingRule input = createInput("r1", 300, 10, 0.0); CentralizedRule r = new CentralizedRule(input, new RandImpl()); SamplingTargetDocument update = new SamplingTargetDocument() .withRuleName("r1") .withFixedRate(0.5) .withInterval(20); r.update(update, Instant.now()); double fixedRate = Whitebox.getInternalState(r, "fixedRate", CentralizedRule.class); Assert.assertEquals(0.5, fixedRate, 0); }