aboutsummaryrefslogtreecommitdiff
path: root/kamon-core-tests/src/test/scala/kamon/util/DifferentialSourceSpec.scala
blob: 0a2f83e5796cdd1c4f4f18e36d542502ffe1aaed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package kamon.util

import org.scalatest.{Matchers, WordSpec}

class DifferentialSourceSpec extends WordSpec with Matchers {

  "a Differential Source" should {
    "get the difference between the last two observations" in {
      val source = sourceOf(0, 0, 1, 1, 2, 3, 4, 6, 8, 10, 12, 16, 18)
      val expectedDiffs = Seq(0, 1, 0, 1, 1, 1, 2, 2, 2, 2, 4, 2)

      values(expectedDiffs.length, source) should contain theSameElementsInOrderAs(expectedDiffs)
    }

    "ignore decrements in observations" in {
      val source = sourceOf(10, 10, 5, 5, 10, 10)
      val expectedDiffs = Seq(0, 0, 0, 5, 0)

      values(expectedDiffs.length, source) should contain theSameElementsInOrderAs(expectedDiffs)
    }
  }

  def sourceOf(numbers: Long*): DifferentialSource = DifferentialSource(new (() => Long) {
    var remaining = numbers.toList

    override def apply(): Long = {
      if(remaining.isEmpty) 0 else {
        val head = remaining.head
        remaining = remaining.tail
        head
      }
    }
  })

  def values(count: Int, source: DifferentialSource): Seq[Long] = {
    for(_ <- 1 to count) yield source.get()
  }
}