-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircuitTest.scala
158 lines (122 loc) · 4.75 KB
/
CircuitTest.scala
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.lprakashv.resiliency
import org.scalatest.FunSuite
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
class CircuitTest extends FunSuite {
//TODO: - make this FuncSpec based test
//TODO: - improve coverage
//TODO: - small unit tests first, try to make as few composites as possible.
import com.lprakashv.resiliency.CircuitImplicits._
test("test successes with CircuitImplicits") {
implicit val sampleCircuit: Circuit[Int] =
new Circuit[Int]("sample-circuit", 5, 5.seconds, 1, -1, println)
// to test concurrent executions
val resultsF: Future[Seq[CircuitResult[Int]]] =
Future.sequence((1 to 500).map(_ => (2 + 2).executeAsync))
val results: Seq[CircuitResult[Int]] =
Await.result(resultsF, 10.minutes).toList
assert((1 * 34 / 3).execute.isSuccess)
assert(results.flatten.count(_ == 4) == 500)
}
test(
"[on continuous invalid execution] " +
"failures 5 times"
) {
implicit val sampleCircuit: Circuit[Int] =
new Circuit[Int]("sample-circuit", 5, 5.seconds, 1, -1, println)
val resultsF: Future[IndexedSeq[CircuitResult[Int]]] = Future.sequence(for {
_ <- 1 to 5
} yield (1 / 0).executeAsync)
val results: Seq[CircuitResult[Int]] =
Await.result(resultsF, 1.minutes).toList
assert(results.forall(_.isFailed), s"got $results")
}
test(
"[on continuous invalid execution different circuits] " +
"failures 5 times"
) {
implicit val sampleIntCircuit: Circuit[Int] =
new Circuit[Int]("sample-int-circuit", 5, 5.seconds, 1, -1, println)
implicit val sampleStringCircuit: Circuit[String] =
new Circuit[String](
"sample-str-circuit",
5,
5.seconds,
1,
"default",
println
)
val resultsF: Future[IndexedSeq[CircuitResult[Int]]] = Future.sequence(for {
_ <- 1 to 5
} yield (1 / 0).executeAsync)
val results: Seq[CircuitResult[Int]] =
Await.result(resultsF, 1.minutes).toList
assert(results.forall(_.isFailed), s"got $results")
}
test(
"[on continuous invalid execution] " +
"success with default answer after failure 5 times"
) {
implicit val sampleCircuit: Circuit[Int] =
new Circuit[Int]("sample-circuit", 5, 5.seconds, 1, -1, println)
val resultsF: Future[IndexedSeq[CircuitResult[Int]]] =
Future.sequence((1 to 12).map(_ => (1 / 0).executeAsync))
val results: Seq[CircuitResult[Int]] =
Await.result(resultsF, 1.minutes).toList
assert(
results.flatten.count(_ == -1) == 7,
"failed to verify 7 successes (default case in open circuit) after 5 failures on 12 invalid executions"
)
}
test(
"[on continuous invalid execution] " +
"failure 5 times and then successes then failure after timeout (5 sec) and then successes again"
) {
implicit val sampleCircuit: Circuit[Int] =
new Circuit[Int]("sample-circuit", 5, 5.seconds, 1, -1, println)
// 5 failures and then 5 successes
val resultsF: Future[IndexedSeq[CircuitResult[Int]]] =
Future.sequence((1 to 10).map(_ => (1 / 0).executeAsync))
val results: Seq[CircuitResult[Int]] = Await.result(resultsF, 10.minutes)
assert(
results.flatten.count(_ == -1) == 5,
"failed to verify - 5 failures and 5 successes for 10 invalid executions"
)
Thread.sleep(5100)
assert(
(1 / 0).execute.isFailed,
"failed to verify failure after timeout (showing half-open try)"
)
assert(
(1 / 0).execute.toOption.contains(-1),
"failed to verify success showing open (default case) after half-open failure"
)
Thread.sleep(5200)
val resultsF2: Future[IndexedSeq[CircuitResult[Int]]] =
Future.sequence((1 to 5).map(_ => (7 * 7).executeAsync))
val results2: Seq[CircuitResult[Int]] = Await.result(resultsF2, 10.minutes)
assert(
results2.flatten.forall(_ == 49),
"failed to verify successes with valid results after timeout showing closed after half-open success"
)
}
test(
"failure 5 times and then default successes even after having valid execution"
) {
implicit val sampleCircuit: Circuit[Int] =
new Circuit[Int]("sample-circuit", 5, 5.seconds, 1, -1, println)
Await.result(
Future.sequence((1 to 5).map(_ => (1 / 0).executeAsync)),
10.minutes
)
assert(
(1 / 0).execute.toOption.contains(-1),
"failed to validate circuit failure after reaching failure threshold"
)
val resultsF: Future[IndexedSeq[CircuitResult[Int]]] =
Future.sequence((1 to 5).map(_ => Future.successful(7 * 7).executeAsync))
val results: Seq[CircuitResult[Int]] = Await.result(resultsF, 10.minutes)
assert(results.flatten.forall(_ == -1))
}
}