-
Notifications
You must be signed in to change notification settings - Fork 791
/
VertxRxCircuitBreakerHttpServerTest.groovy
165 lines (154 loc) · 5.6 KB
/
VertxRxCircuitBreakerHttpServerTest.groovy
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
159
160
161
162
163
164
165
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package server
import io.opentelemetry.instrumentation.test.base.HttpServerTest
import io.vertx.circuitbreaker.CircuitBreakerOptions
import io.vertx.core.Promise
import io.vertx.reactivex.circuitbreaker.CircuitBreaker
import io.vertx.reactivex.core.AbstractVerticle
import io.vertx.reactivex.ext.web.Router
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.CAPTURE_HEADERS
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.ERROR
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.EXCEPTION
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.INDEXED_CHILD
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.PATH_PARAM
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.REDIRECT
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.SUCCESS
class VertxRxCircuitBreakerHttpServerTest extends VertxRxHttpServerTest {
@Override
protected Class<AbstractVerticle> verticle() {
return VertxRxCircuitBreakerWebTestServer
}
static class VertxRxCircuitBreakerWebTestServer extends AbstractVerticle {
@Override
void start(Promise<Void> startPromise) {
int port = config().getInteger(CONFIG_HTTP_SERVER_PORT)
Router router = Router.router(super.@vertx)
CircuitBreaker breaker =
CircuitBreaker.create(
"my-circuit-breaker",
super.@vertx,
new CircuitBreakerOptions()
.setTimeout(-1) // Disable the timeout otherwise it makes each test take this long.
)
router.route(SUCCESS.path).handler { ctx ->
breaker.execute({ future ->
future.complete(SUCCESS)
}, { it ->
if (it.failed()) {
throw it.cause()
}
HttpServerTest.ServerEndpoint endpoint = it.result()
controller(endpoint) {
ctx.response().setStatusCode(endpoint.status).end(endpoint.body)
}
})
}
router.route(INDEXED_CHILD.path).handler { ctx ->
breaker.execute({ future ->
future.complete(INDEXED_CHILD)
}, { it ->
if (it.failed()) {
throw it.cause()
}
HttpServerTest.ServerEndpoint endpoint = it.result()
controller(endpoint) {
endpoint.collectSpanAttributes { ctx.request().params().get(it) }
ctx.response().setStatusCode(endpoint.status).end()
}
})
}
router.route(QUERY_PARAM.path).handler { ctx ->
breaker.execute({ future ->
future.complete(QUERY_PARAM)
}, { it ->
if (it.failed()) {
throw it.cause()
}
HttpServerTest.ServerEndpoint endpoint = it.result()
controller(endpoint) {
ctx.response().setStatusCode(endpoint.status).end(ctx.request().query())
}
})
}
router.route(REDIRECT.path).handler { ctx ->
breaker.execute({ future ->
future.complete(REDIRECT)
}, {
if (it.failed()) {
throw it.cause()
}
HttpServerTest.ServerEndpoint endpoint = it.result()
controller(endpoint) {
ctx.response().setStatusCode(endpoint.status).putHeader("location", endpoint.body).end()
}
})
}
router.route(ERROR.path).handler { ctx ->
breaker.execute({ future ->
future.complete(ERROR)
}, {
if (it.failed()) {
throw it.cause()
}
HttpServerTest.ServerEndpoint endpoint = it.result()
controller(endpoint) {
ctx.response().setStatusCode(endpoint.status).end(endpoint.body)
}
})
}
router.route(EXCEPTION.path).handler { ctx ->
breaker.execute({ future ->
future.fail(new Exception(EXCEPTION.body))
}, {
try {
def cause = it.cause()
controller(EXCEPTION) {
throw cause
}
} catch (Exception ex) {
ctx.response().setStatusCode(EXCEPTION.status).end(ex.message)
}
})
}
router.route("/path/:id/param").handler { ctx ->
breaker.execute({ future ->
future.complete(PATH_PARAM)
}, {
if (it.failed()) {
throw it.cause()
}
HttpServerTest.ServerEndpoint endpoint = it.result()
controller(endpoint) {
ctx.response().setStatusCode(endpoint.status).end(ctx.request().getParam("id"))
}
})
}
router.route(CAPTURE_HEADERS.path).handler { ctx ->
breaker.execute({ future ->
future.complete(CAPTURE_HEADERS)
}, {
if (it.failed()) {
throw it.cause()
}
HttpServerTest.ServerEndpoint endpoint = it.result()
controller(endpoint) {
ctx.response().setStatusCode(endpoint.status)
.putHeader("X-Test-Response", ctx.request().getHeader("X-Test-Request"))
.end(endpoint.body)
}
})
}
.requestHandler(router)
.listen(port) { startPromise.complete() }
}
}
@Override
boolean hasExceptionOnServerSpan(HttpServerTest.ServerEndpoint endpoint) {
return endpoint != EXCEPTION && super.hasExceptionOnServerSpan(endpoint)
}
}