-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathHedgedClientRunner.scala
109 lines (89 loc) · 3.35 KB
/
HedgedClientRunner.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
package app.impl.http
import app.impl.http.HttpHedgedClient.{HttpClientInfo, Service}
import org.junit.Test
import zio.{Has, ZIO, ZLayer}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
/**
* Here we create our programs using the DSL of HttpHedgedClient, and using the different provide
* like finagle, Akka http or even a Custom client engine.
*/
class HedgedClientRunner {
/**
* DSL program to be used by different engines using sugar for comprehension.
*/
val programWithSugar: ZIO[Has[HttpHedgedClient.Service], Nothing, Future[Any]] =
for {
_ <- HttpHedgedClient.Get()
_ <- HttpHedgedClient.Host("www.google.com:80")
_ <- HttpHedgedClient.Timeout(1000)
_ <- HttpHedgedClient.Hedged(4)
future <- HttpHedgedClient.Run()
} yield future
/**
* DSL program to be used by different engines.
*/
val programWithoutSugar: ZIO[Has[HttpHedgedClient.Service], Nothing, Future[Any]] =
HttpHedgedClient.Get()
.flatMap(_ => HttpHedgedClient.Uri("/"))
.flatMap(_ => HttpHedgedClient.Host("www.google.com:80"))
.flatMap(_ => HttpHedgedClient.Timeout(1000))
.flatMap(_ => HttpHedgedClient.Hedged(4))
.flatMap(_ => HttpHedgedClient.Run())
/**
* In this example we use programWithSugar structure with finagleEngine as behavior
*/
@Test
def finagleEngineWithSugarSyntax(): Unit = {
val programResponse: Future[Any] = HttpHedgedClient.runtime.unsafeRun {
programWithSugar.provideCustomLayer(HttpHedgedClient.finagleEngine)
}
println(Await.ready(programResponse, 10 seconds))
}
@Test
def finagleEngineWithoutSugarSyntax(): Unit = {
val programResponse: Future[Any] = HttpHedgedClient.runtime.unsafeRun {
programWithoutSugar.provideCustomLayer(HttpHedgedClient.finagleEngine)
}
println(Await.ready(programResponse, 10 seconds))
}
/**
* In this example we use programWithSugar structure with akkaEngine as behavior
*/
@Test
def akkaEngine(): Unit = {
val programResponse: Future[Any] = HttpHedgedClient.runtime.unsafeRun {
programWithSugar.provideCustomLayer(HttpHedgedClient.akkaEngine)
}
println(Await.ready(programResponse, 10 seconds))
}
/**
* One of the coolest feature of this connector it's to allow a custom engine implementation
* by the consumer without have to change the DSL or Program already created.
*/
@Test
def customEngine(): Unit = {
val customEngine = ZLayer.succeed(new Service {
override def getHttpClient: HttpHedgedClient.HttpClientInfo = new HttpClientInfo {
override def getHedged: Int = 1
}
override def withUri(uri: String): Unit = {}
override def withHost(host: String): Unit = {}
override def withGetMethod(): Unit = {}
override def withPostMethod(): Unit = {}
override def withBody(body: String): Unit = {}
override def withTimeout(time: Long): Unit = {}
override def withHedged(times: Int): Unit = {}
override def run(): Future[Any] = {
Future {
"Hello from custom engine. Pretty cool for testing right?"
}
}
})
val programResponse: Future[Any] = HttpHedgedClient.runtime.unsafeRun {
programWithSugar.provideCustomLayer(customEngine)
}
println(Await.ready(programResponse, 10 seconds))
}
}