Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finatra/inject: Simpler customizing timeout for inMemoryStats
Problem: The only way to set a custom timeout for a metric is with a predicate `T => Boolean`. If users want to check whether a metric equals a specific value, it is simpler to pass the expected value instead of wrapping it in a predicate like `(_ = 1.0f)`. Solution: Add a new, non-predicate version of waitFor that waits for an expected value within a specific timeout. This non-dynamic case keeps waitFor consistent with other methods that assert or validate expected values. JIRA Issues: CSL-9951 Differential Revision: https://phabricator.twitter.biz/D576147
- Loading branch information
1 parent
9667845
commit 306b719
Showing
7 changed files
with
216 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...nject-core/src/test/scala/com/twitter/inject/tests/InMemoryStatsReceiverUtilityTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.twitter.inject.tests | ||
|
||
import com.twitter.finagle.stats.InMemoryStatsReceiver | ||
import com.twitter.inject.{InMemoryStatsReceiverUtility, Test} | ||
import com.twitter.conversions.DurationOps._ | ||
import com.twitter.util.FuturePool | ||
import org.scalatest.exceptions.TestFailedDueToTimeoutException | ||
|
||
class InMemoryStatsReceiverUtilityTest extends Test { | ||
test("InMemoryStatsReceiverUtility#waitFor will fail if the metric is the incorrect value") { | ||
val inMemoryStatsReceiver = new InMemoryStatsReceiver | ||
val statUtils = new InMemoryStatsReceiverUtility(inMemoryStatsReceiver) | ||
// A short timeout is set because the metric will never reach the expected value | ||
val timeout = 10.millis | ||
|
||
inMemoryStatsReceiver.counter("counter").incr() | ||
intercept[TestFailedDueToTimeoutException] { | ||
statUtils.counters.waitFor("counter", 2, timeout) | ||
} | ||
|
||
inMemoryStatsReceiver.stats(Seq("stats")) = Seq(1.0f, 2.0f, 3.0f) | ||
intercept[TestFailedDueToTimeoutException] { | ||
statUtils.stats.waitFor("stats", Seq(1.0f), timeout) | ||
} | ||
|
||
inMemoryStatsReceiver.addGauge("gauge") { 1.0f } | ||
intercept[TestFailedDueToTimeoutException] { | ||
statUtils.gauges.waitFor("gauge", 2.0f, timeout) | ||
} | ||
} | ||
|
||
test("InMemoryStatsReceiverUtility#waitFor will fail if metric does not have a declared value") { | ||
val inMemoryStatsReceiver = new InMemoryStatsReceiver | ||
val statUtils = new InMemoryStatsReceiverUtility(inMemoryStatsReceiver) | ||
// A short timeout is set because the metric does not have a declared value | ||
val timeout = 20.millis | ||
|
||
inMemoryStatsReceiver.stats(Seq("stats")) = Seq() | ||
intercept[TestFailedDueToTimeoutException] { | ||
statUtils.stats.waitFor("stats", Seq(1.0f), timeout) | ||
} | ||
} | ||
|
||
test( | ||
"InMemoryStatsReceiverUtility#waitFor will accept metrics that are already the expected value") { | ||
val inMemoryStatsReceiver = new InMemoryStatsReceiver | ||
val statUtils = new InMemoryStatsReceiverUtility(inMemoryStatsReceiver) | ||
val timeout = 20.millis | ||
|
||
inMemoryStatsReceiver.counter("counter").incr(2) | ||
statUtils.counters.waitFor("counter", 2, timeout) | ||
|
||
inMemoryStatsReceiver.stats(Seq("stat")) = Seq(1.0f, 2.0f, 3.0f) | ||
statUtils.stats.waitFor("stat", Seq(1.0f, 2.0f, 3.0f), timeout) | ||
|
||
inMemoryStatsReceiver.addGauge("gauge") { 2.0f } | ||
statUtils.gauges.waitFor("gauge", 2.0f, timeout) | ||
} | ||
|
||
test( | ||
"InMemoryStatsReceiverUtility#waitFor will succeed when metric changes to the expected value within the timeout window") { | ||
val inMemoryStatsReceiver = new InMemoryStatsReceiver | ||
val sr = new InMemoryStatsReceiverUtility(inMemoryStatsReceiver) | ||
val timeout = 100.millis | ||
|
||
val counter = inMemoryStatsReceiver.counter("counter") | ||
val stat = inMemoryStatsReceiver.stat("stat") | ||
var gaugeValue: Float = 0.0f | ||
val gauge = inMemoryStatsReceiver.addGauge("gauge") { gaugeValue } | ||
|
||
// Change metrics every 20 millis | ||
val increaseCounter = FuturePool.unboundedPool { | ||
for (_ <- 0 to 2) { | ||
Thread.sleep(20) | ||
// counter values: 0L, 1L, 2L, 3L | ||
counter.incr() | ||
} | ||
} | ||
sr.counters.waitFor("counter", 1L, timeout) | ||
|
||
val addToHistogram = FuturePool.unboundedPool { | ||
for (s <- 0 to 2) { | ||
Thread.sleep(20) | ||
// histogram values: Seq(0.0f, 1.0f, 2.0f) | ||
stat.add(s) | ||
} | ||
} | ||
sr.stats.waitFor("stat", Seq(0.0f, 1.0f), timeout) | ||
|
||
val increaseGauge = FuturePool.unboundedPool { | ||
for (g <- 0 to 2) { | ||
Thread.sleep(20) | ||
// gauge values: 0.0f, 1.0f, 2.0f | ||
gaugeValue = g | ||
} | ||
} | ||
sr.gauges.waitFor("gauge", 1.0f, timeout) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.