aboutsummaryrefslogtreecommitdiff
path: root/circuit-breaker/hystrix-backend/src/test/scala/com/softwaremill/sttp/hystrix/HystrixBackendTest.scala
blob: 8772d4512cbdad7239afaba5f2a54dcac3546465 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.softwaremill.sttp.hystrix

import com.netflix.hystrix.{HystrixCommandKey, HystrixCommandMetrics, HystrixCommandProperties}
import com.softwaremill.sttp.testing.SttpBackendStub
import com.softwaremill.sttp.{sttp, _}
import org.scalatest.concurrent.{Eventually, IntegrationPatience, ScalaFutures}
import org.scalatest.{BeforeAndAfter, FlatSpec, Matchers, OptionValues}

import scala.concurrent.Future

class HystrixBackendTest
    extends FlatSpec
    with Matchers
    with BeforeAndAfter
    with Eventually
    with OptionValues
    with ScalaFutures
    with IntegrationPatience {

  it should "use default hystrix commands on async backend" in {
    // given
    val backendStub = SttpBackendStub.asynchronousFuture.whenAnyRequest.thenRespondOk()

    val backend = HystrixBackend[Future, Nothing](backendStub)(
      "TestAsyncCMD",
      HystrixCommandProperties.Setter().withMetricsHealthSnapshotIntervalInMilliseconds(10))
    val requestsNumber = 10

    // when
    (0 until requestsNumber).map(_ => backend.send(sttp.get(uri"http://localhost:8080/get")).futureValue)

    // then
    val metrics = HystrixCommandMetrics.getInstance(HystrixCommandKey.Factory.asKey("AsyncSttpCMD"))

    Thread.sleep(100) // wait for the health metrics

    metrics.getHealthCounts.getErrorPercentage shouldBe 0
    metrics.getHealthCounts.getTotalRequests shouldBe 10
  }

  it should "use default hystrix commands on sync backend" in {
    // given
    val backendStub = SttpBackendStub.synchronous.whenAnyRequest.thenRespondOk()

    val backend = HystrixBackend[Id, Nothing](backendStub)(
      "TestSyncCMD",
      HystrixCommandProperties.Setter().withMetricsHealthSnapshotIntervalInMilliseconds(10))
    val requestsNumber = 10

    // when
    (0 until requestsNumber).map(_ => backend.send(sttp.get(uri"http://localhost:8080/get")))

    // then
    val metrics = HystrixCommandMetrics.getInstance(HystrixCommandKey.Factory.asKey("SyncSttpCMD"))

    Thread.sleep(100) // wait for the health metrics

    metrics.getHealthCounts.getErrorPercentage shouldBe 0
    metrics.getHealthCounts.getTotalRequests shouldBe 10
  }

}