Skip to content

Commit 9a35f65

Browse files
committed
Refactor REST API tests to use consistent async syntax
Updated test syntax to use `in` for better readability and migrated to fully asynchronous assertions where applicable. Removed redundant `.futureValue` statements, ensuring non-blocking behavior across test methods.
1 parent 737fce1 commit 9a35f65

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

rest/.jvm/src/test/scala/io/udash/rest/SttpRestCallTest.scala

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,27 @@ trait SttpClientRestTest extends ServletBasedRestApiTest {
3030
}
3131

3232
class SttpRestCallTest extends SttpClientRestTest with RestApiTestScenarios {
33-
test("too large binary request") {
34-
val future = proxy.binaryEcho(Array.fill[Byte](maxPayloadSize + 1)(5))
35-
val exception = future.failed.futureValue
36-
assert(exception == HttpErrorException.plain(413, "Payload is larger than maximum 1048576 bytes (1048577)"))
33+
"too large binary request" in {
34+
proxy.binaryEcho(Array.fill[Byte](maxPayloadSize + 1)(5))
35+
.failed
36+
.map { exception =>
37+
assert(exception == HttpErrorException.plain(413, "Payload is larger than maximum 1048576 bytes (1048577)"))
38+
}
3739
}
3840
}
3941

4042
class ServletTimeoutTest extends SttpClientRestTest {
4143
override def serverTimeout: FiniteDuration = 500.millis
4244

43-
test("rest method timeout") {
44-
val exception = proxy.neverGet.failed.futureValue
45-
assert(exception == HttpErrorException.plain(500, "server operation timed out after 500 milliseconds"))
45+
"rest method timeout" in {
46+
proxy.neverGet
47+
.failed
48+
.map { exception =>
49+
assert(exception == HttpErrorException.plain(500, "server operation timed out after 500 milliseconds"))
50+
}
4651
}
4752

48-
test("subsequent requests with timeout") {
53+
"subsequent requests with timeout" in {
4954
assertThrows[HttpErrorException](Await.result(proxy.wait(600), Duration.Inf))
5055
assertThrows[HttpErrorException](Await.result(proxy.wait(600), Duration.Inf))
5156
assertThrows[HttpErrorException](Await.result(proxy.wait(600), Duration.Inf))

rest/src/test/scala/io/udash/rest/RestApiTest.scala

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package io.udash
22
package rest
33

4+
import cats.implicits.catsSyntaxTuple2Semigroupal
45
import com.avsystem.commons.*
56
import com.avsystem.commons.misc.ScalaDurationExtensions.durationIntOps
67
import io.udash.rest.raw.RawRest
78
import io.udash.rest.raw.RawRest.HandleRequest
9+
import io.udash.testing.AsyncUdashSharedTest
810
import monix.eval.Task
911
import monix.execution.Scheduler
1012
import org.scalactic.source.Position
11-
import org.scalatest.BeforeAndAfterEach
12-
import org.scalatest.concurrent.ScalaFutures
13-
import org.scalatest.funsuite.AnyFunSuite
1413
import org.scalatest.time.{Millis, Seconds, Span}
14+
import org.scalatest.{Assertion, BeforeAndAfterEach}
1515

1616
import scala.concurrent.duration.FiniteDuration
1717

18-
abstract class RestApiTest extends AnyFunSuite with ScalaFutures with BeforeAndAfterEach {
18+
abstract class RestApiTest extends AsyncUdashSharedTest with BeforeAndAfterEach {
1919
implicit def scheduler: Scheduler = Scheduler.global
2020

2121
protected final val MaxConnections: Int = 1 // to timeout quickly
@@ -38,11 +38,10 @@ abstract class RestApiTest extends AnyFunSuite with ScalaFutures with BeforeAndA
3838
lazy val proxy: RestTestApi =
3939
RawRest.fromHandleRequest[RestTestApi](clientHandle)
4040

41-
def testCall[T](call: RestTestApi => Future[T])(implicit pos: Position): Unit =
42-
assert(
43-
call(proxy).wrapToTry.futureValue.map(mkDeep) ==
44-
call(impl).catchFailures.wrapToTry.futureValue.map(mkDeep)
45-
)
41+
def testCall[T](call: RestTestApi => Future[T])(implicit pos: Position): Future[Assertion] =
42+
(call(proxy).wrapToTry, call(impl).catchFailures.wrapToTry).mapN { (proxyResult, implResult) =>
43+
assert(proxyResult.map(mkDeep) == implResult.map(mkDeep))
44+
}
4645

4746
def mkDeep(value: Any): Any = value match {
4847
case arr: Array[_] => IArraySeq.empty[AnyRef] ++ arr.iterator.map(mkDeep)
@@ -53,72 +52,71 @@ abstract class RestApiTest extends AnyFunSuite with ScalaFutures with BeforeAndA
5352
trait RestApiTestScenarios extends RestApiTest {
5453
override implicit val patienceConfig: PatienceConfig = PatienceConfig(scaled(Span(10, Seconds)), scaled(Span(50, Millis)))
5554

56-
test("trivial GET") {
55+
"trivial GET" in {
5756
testCall(_.trivialGet)
5857
}
5958

60-
test("failing GET") {
59+
"failing GET" in {
6160
testCall(_.failingGet)
6261
}
6362

64-
test("JSON failing GET") {
63+
"JSON failing GET" in {
6564
testCall(_.jsonFailingGet)
6665
}
6766

68-
test("more failing GET") {
67+
"more failing GET" in {
6968
testCall(_.moreFailingGet)
7069
}
7170

72-
test("complex GET") {
71+
"complex GET" in {
7372
testCall(_.complexGet(0, "a/ +&", 1, "b/ +&", 2, "ć/ +&", Opt(3), 4, "ó /&f"))
7473
testCall(_.complexGet(0, "a/ +&", 1, "b/ +&", 2, "ć/ +&", Opt.Empty, 3, "ó /&f"))
7574
}
7675

77-
test("multi-param body POST") {
76+
"multi-param body POST" in {
7877
testCall(_.multiParamPost(0, "a/ +&", 1, "b/ +&", 2, "ć/ +&", 3, "l\"l"))
7978
}
8079

81-
test("single body PUT") {
80+
"single body PUT" in {
8281
testCall(_.singleBodyPut(RestEntity(RestEntityId("id"), "señor")))
8382
}
8483

85-
test("form POST") {
84+
"form POST" in {
8685
testCall(_.formPost("ó", "ą=ę", 42))
8786
}
8887

89-
test("prefixed GET") {
88+
"prefixed GET" in {
9089
testCall(_.prefix("p0", "h0", "q0").subget(0, 1, 2))
9190
}
9291

93-
test("transparent prefix GET") {
92+
"transparent prefix GET" in {
9493
testCall(_.transparentPrefix.subget(0, 1, 2))
9594
}
9695

97-
test("custom response with headers") {
96+
"custom response with headers" in {
9897
testCall(_.customResponse("walue"))
9998
}
10099

101-
test("binary request and response") {
100+
"binary request and response" in {
102101
testCall(_.binaryEcho(Array.fill[Byte](5)(5)))
103102
}
104103

105-
test("large binary request and response") {
104+
"large binary request and response" in {
106105
testCall(_.binaryEcho(Array.fill[Byte](1024 * 1024)(5)))
107106
}
108107

109-
test("body using third party type") {
108+
"body using third party type" in {
110109
testCall(_.thirdPartyBody(HasThirdParty(ThirdParty(5))))
111110
}
112111

113-
test("close connection on monix task timeout") {
112+
"close connection on monix task timeout" in {
114113
Task
115114
.traverse(List.range(0, Connections))(_ => Task.deferFuture(proxy.neverGet).timeout(CallTimeout).failed)
116115
.map(_ => assertResult(expected = Connections)(actual = impl.counterValue())) // neverGet should be called Connections times
117116
.runToFuture
118-
.futureValue
119117
}
120118

121-
test("close connection on monix task cancellation") {
119+
"close connection on monix task cancellation" in {
122120
Task
123121
.traverse(List.range(0, Connections)) { i =>
124122
val cancelable = Task.deferFuture(proxy.neverGet).runAsync(_ => ())
@@ -128,7 +126,6 @@ trait RestApiTestScenarios extends RestApiTest {
128126
}
129127
.map(_ => assertResult(expected = Connections)(actual = impl.counterValue())) // neverGet should be called Connections times
130128
.runToFuture
131-
.futureValue
132129
}
133130
}
134131

0 commit comments

Comments
 (0)