Skip to content

Commit c6e0b87

Browse files
Adds some retries for the flaky tests (#1601)
* Adds some retries for the flaky tests * Logs each retry * Replaces logging system in tests
1 parent 8b0cbc3 commit c6e0b87

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

modules/tests/src/test/scala-2/higherkindness/mu/rpc/fs2/RPCTests.scala

+43-4
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,71 @@ import fs2.Stream
2121
import higherkindness.mu.rpc.common._
2222
import higherkindness.mu.rpc.fs2.Utils.service.ProtoRPCService
2323
import munit.CatsEffectSuite
24+
import org.log4s.{getLogger, Logger}
2425

26+
import java.io.IOException
2527
import scala.concurrent.duration._
2628

2729
class RPCTests extends CatsEffectSuite {
2830

31+
private val logger: Logger = getLogger
32+
2933
import higherkindness.mu.rpc.fs2.Utils.database._
3034
import higherkindness.mu.rpc.fs2.Utils.implicits._
3135

36+
val retryFiveTimes: retry.RetryPolicy[IO] =
37+
retry.RetryPolicies.limitRetries[IO](5)
38+
39+
implicit class IOOps[Result](private val io: IO[Result]) {
40+
def withRetry: IO[Result] =
41+
retry
42+
.retryingOnSomeErrors[Result]
43+
.apply[IO, Throwable](
44+
retryFiveTimes,
45+
{
46+
case _: IOException => IO.pure(true)
47+
case _ => IO.pure(false)
48+
},
49+
(e, details) =>
50+
details match {
51+
case retry.RetryDetails.WillDelayAndRetry(_, retries: Int, _) =>
52+
IO(logger.info(e)(s"Failed, retried $retries times"))
53+
case _ => IO.unit
54+
}
55+
)(io)
56+
}
57+
3258
val behaviourOf: String = "mu-rpc client with fs2.Stream"
3359

3460
test("mu-rpc server should allow to startup a server and check if it's alive") {
35-
grpcServer.use(_.isShutdown).assertEquals(false)
61+
grpcServer.use(_.isShutdown).withRetry.assertEquals(false)
3662
}
3763

3864
test("mu-rpc server should allow to get the port where it's running") {
39-
grpcServer.use(_.getPort).assertEquals(SC.port)
65+
grpcServer.use(_.getPort).withRetry.assertEquals(SC.port)
4066
}
4167

4268
test(behaviourOf + " be able to run unary services") {
4369
grpcServer
4470
.flatMap(_ => muAvroRPCServiceClient)
4571
.use(_.unary(a1))
72+
.withRetry
4673
.assertEquals(c1)
4774
}
4875

4976
test(behaviourOf + " be able to run unary services with avro schemas") {
5077
grpcServer
5178
.flatMap(_ => muAvroWithSchemaRPCServiceClient)
5279
.use(_.unaryWithSchema(a1))
80+
.withRetry
5381
.assertEquals(c1)
5482
}
5583

5684
test(behaviourOf + " be able to run server streaming services") {
5785
grpcServer
5886
.flatMap(_ => muProtoRPCServiceClient)
5987
.use(_.serverStreaming(b1).flatMap(_.compile.toList))
88+
.withRetry
6089
.assertEquals(cList)
6190
}
6291

@@ -79,12 +108,14 @@ class RPCTests extends CatsEffectSuite {
79108
clientProgram("Thrown", s)
80109
.assertEquals(List(C("UNKNOWN", a1)))
81110
}
111+
.withRetry
82112
}
83113

84114
test(behaviourOf + " be able to run client streaming services") {
85115
grpcServer
86116
.flatMap(_ => muProtoRPCServiceClient)
87117
.use(_.clientStreaming(Stream.fromIterator[IO](aList.iterator, 1)))
118+
.withRetry
88119
.assertEquals(dResult33)
89120
}
90121

@@ -94,6 +125,7 @@ class RPCTests extends CatsEffectSuite {
94125
.use(
95126
_.biStreaming(Stream.fromIterator[IO](eList.iterator, 1)).flatMap(_.compile.toList)
96127
)
128+
.withRetry
97129
.map(_.distinct)
98130
.assertEquals(eList)
99131
}
@@ -107,6 +139,7 @@ class RPCTests extends CatsEffectSuite {
107139
_.biStreamingWithSchema(Stream.fromIterator[IO](eList.iterator, 1))
108140
.flatMap(_.compile.toList)
109141
)
142+
.withRetry
110143
.map(_.distinct)
111144
.assertEquals(eList)
112145
}
@@ -147,7 +180,7 @@ class RPCTests extends CatsEffectSuite {
147180
.clientStreaming(Stream.fromIterator[IO](aList.iterator, 1))
148181
.assertEquals(dResult33)
149182
)
150-
} yield ()).use_.timeoutTo(opTimeout, IO.println("ERROR on multi-op fs2!"))
183+
} yield ()).use_.withRetry.timeoutTo(opTimeout, IO.println("ERROR on multi-op fs2!"))
151184
}
152185

153186
val behaviourOfC: String = behaviourOf + " and compression enabled"
@@ -156,27 +189,31 @@ class RPCTests extends CatsEffectSuite {
156189
grpcServer
157190
.flatMap(_ => muCompressedAvroRPCServiceClient)
158191
.use(_.unaryCompressed(a1))
192+
.withRetry
159193
.assertEquals(c1)
160194
}
161195

162196
test(behaviourOfC + " be able to run unary services with avro schema") {
163197
grpcServer
164198
.flatMap(_ => muCompressedAvroWithSchemaRPCServiceClient)
165199
.use(_.unaryCompressedWithSchema(a1))
200+
.withRetry
166201
.assertEquals(c1)
167202
}
168203

169204
test(behaviourOfC + " be able to run server streaming services") {
170205
grpcServer
171206
.flatMap(_ => muCompressedProtoRPCServiceClient)
172207
.use(_.serverStreamingCompressed(b1).flatMap(_.compile.toList))
208+
.withRetry
173209
.assertEquals(cList)
174210
}
175211

176212
test(behaviourOfC + " be able to run client streaming services") {
177213
grpcServer
178214
.flatMap(_ => muCompressedProtoRPCServiceClient)
179215
.use(_.clientStreamingCompressed(Stream.fromIterator[IO](aList.iterator, 1)))
216+
.withRetry
180217
.assertEquals(dResult33)
181218
}
182219

@@ -187,6 +224,7 @@ class RPCTests extends CatsEffectSuite {
187224
_.biStreamingCompressed(Stream.fromIterator[IO](eList.iterator, 1))
188225
.flatMap(_.compile.toList)
189226
)
227+
.withRetry
190228
.map(_.distinct)
191229
.assertEquals(eList)
192230
}
@@ -200,6 +238,7 @@ class RPCTests extends CatsEffectSuite {
200238
_.biStreamingCompressedWithSchema(Stream.fromIterator[IO](eList.iterator, 1))
201239
.flatMap(_.compile.toList)
202240
)
241+
.withRetry
203242
.map(_.distinct)
204243
.assertEquals(eList)
205244
}
@@ -244,7 +283,7 @@ class RPCTests extends CatsEffectSuite {
244283
.clientStreamingCompressed(Stream.fromIterator[IO](aList.iterator, 1))
245284
.assertEquals(dResult33)
246285
)
247-
} yield ()).use_.timeout(opTimeout)
286+
} yield ()).use_.withRetry.timeout(opTimeout)
248287
}
249288

250289
}

project/ProjectPlugin.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ object ProjectPlugin extends AutoPlugin {
1919
lazy val V = new {
2020
val avro4s: String = "4.1.0"
2121
val catsEffect: String = "3.4.7"
22+
val catsRetry: String = "3.1.0"
2223
val dockerItScala = "0.11.0"
2324
val dropwizard: String = "4.2.15"
2425
val enumeratum: String = "1.7.2"
@@ -226,7 +227,8 @@ object ProjectPlugin extends AutoPlugin {
226227
"org.scalameta" %% "munit-scalacheck" % V.munit % Test,
227228
"org.typelevel" %% "munit-cats-effect-3" % V.munitCE % Test,
228229
"org.typelevel" %% "cats-effect-testkit" % V.catsEffect % Test,
229-
"ch.qos.logback" % "logback-classic" % V.logback % Test
230+
"ch.qos.logback" % "logback-classic" % V.logback % Test,
231+
"com.github.cb372" %% "cats-retry" % V.catsRetry % Test
230232
)
231233
)
232234

0 commit comments

Comments
 (0)