28
28
29
29
import okhttp3 .mockwebserver .MockResponse ;
30
30
import okhttp3 .mockwebserver .MockWebServer ;
31
- import okhttp3 .mockwebserver .QueueDispatcher ;
32
31
import okhttp3 .mockwebserver .RecordedRequest ;
33
- import org .junit .jupiter .api .AfterAll ;
34
- import org .junit .jupiter .api .BeforeAll ;
35
32
import org .junit .jupiter .api .BeforeEach ;
36
33
import org .junit .jupiter .api .Test ;
37
34
import zipkin2 .reporter .BytesMessageSender ;
52
49
*/
53
50
class ZipkinHttpClientSenderTests extends ZipkinHttpSenderTests {
54
51
55
- private static ClearableDispatcher dispatcher ;
52
+ private MockWebServer mockBackEnd ;
56
53
57
- private static MockWebServer mockBackEnd ;
58
-
59
- static String zipkinUrl ;
60
-
61
- @ BeforeAll
62
- static void beforeAll () throws IOException {
63
- dispatcher = new ClearableDispatcher ();
64
- mockBackEnd = new MockWebServer ();
65
- mockBackEnd .setDispatcher (dispatcher );
66
- mockBackEnd .start ();
67
- zipkinUrl = mockBackEnd .url ("/api/v2/spans" ).toString ();
68
- }
69
-
70
- @ AfterAll
71
- static void afterAll () throws IOException {
72
- mockBackEnd .shutdown ();
73
- }
54
+ private String zipkinUrl ;
74
55
75
56
@ Override
76
57
@ BeforeEach
77
58
void beforeEach () throws Exception {
59
+ this .mockBackEnd = new MockWebServer ();
60
+ this .mockBackEnd .start ();
61
+ this .zipkinUrl = this .mockBackEnd .url ("/api/v2/spans" ).toString ();
78
62
super .beforeEach ();
79
- clearResponses ();
80
- clearRequests ();
63
+ }
64
+
65
+ @ Override
66
+ void afterEach () throws IOException {
67
+ super .afterEach ();
68
+ this .mockBackEnd .shutdown ();
81
69
}
82
70
83
71
@ Override
@@ -92,12 +80,12 @@ ZipkinHttpClientSender createSender(Encoding encoding, Duration timeout) {
92
80
ZipkinHttpClientSender createSender (HttpEndpointSupplier .Factory endpointSupplierFactory , Encoding encoding ,
93
81
Duration timeout ) {
94
82
HttpClient httpClient = HttpClient .newBuilder ().connectTimeout (timeout ).build ();
95
- return new ZipkinHttpClientSender (encoding , endpointSupplierFactory , zipkinUrl , httpClient , timeout );
83
+ return new ZipkinHttpClientSender (encoding , endpointSupplierFactory , this . zipkinUrl , httpClient , timeout );
96
84
}
97
85
98
86
@ Test
99
87
void sendShouldSendSpansToZipkin () throws IOException , InterruptedException {
100
- mockBackEnd .enqueue (new MockResponse ());
88
+ this . mockBackEnd .enqueue (new MockResponse ());
101
89
List <byte []> encodedSpans = List .of (toByteArray ("span1" ), toByteArray ("span2" ));
102
90
this .sender .send (encodedSpans );
103
91
requestAssertions ((request ) -> {
@@ -109,7 +97,7 @@ void sendShouldSendSpansToZipkin() throws IOException, InterruptedException {
109
97
110
98
@ Test
111
99
void sendShouldSendSpansToZipkinInProto3 () throws IOException , InterruptedException {
112
- mockBackEnd .enqueue (new MockResponse ());
100
+ this . mockBackEnd .enqueue (new MockResponse ());
113
101
List <byte []> encodedSpans = List .of (toByteArray ("span1" ), toByteArray ("span2" ));
114
102
try (BytesMessageSender sender = createSender (Encoding .PROTO3 , Duration .ofSeconds (10 ))) {
115
103
sender .send (encodedSpans );
@@ -127,22 +115,22 @@ void sendShouldSendSpansToZipkinInProto3() throws IOException, InterruptedExcept
127
115
*/
128
116
@ Test
129
117
void sendUsesDynamicEndpoint () throws Exception {
130
- mockBackEnd .enqueue (new MockResponse ());
131
- mockBackEnd .enqueue (new MockResponse ());
132
- try (TestHttpEndpointSupplier httpEndpointSupplier = new TestHttpEndpointSupplier (zipkinUrl )) {
118
+ this . mockBackEnd .enqueue (new MockResponse ());
119
+ this . mockBackEnd .enqueue (new MockResponse ());
120
+ try (TestHttpEndpointSupplier httpEndpointSupplier = new TestHttpEndpointSupplier (this . zipkinUrl )) {
133
121
try (BytesMessageSender sender = createSender ((endpoint ) -> httpEndpointSupplier , Encoding .JSON ,
134
122
Duration .ofSeconds (10 ))) {
135
123
sender .send (Collections .emptyList ());
136
124
sender .send (Collections .emptyList ());
137
125
}
138
- assertThat (mockBackEnd .takeRequest ().getPath ()).endsWith ("/1" );
139
- assertThat (mockBackEnd .takeRequest ().getPath ()).endsWith ("/2" );
126
+ assertThat (this . mockBackEnd .takeRequest ().getPath ()).endsWith ("/1" );
127
+ assertThat (this . mockBackEnd .takeRequest ().getPath ()).endsWith ("/2" );
140
128
}
141
129
}
142
130
143
131
@ Test
144
132
void sendShouldHandleHttpFailures () throws InterruptedException {
145
- mockBackEnd .enqueue (new MockResponse ().setResponseCode (500 ));
133
+ this . mockBackEnd .enqueue (new MockResponse ().setResponseCode (500 ));
146
134
assertThatException ().isThrownBy (() -> this .sender .send (Collections .emptyList ()))
147
135
.withMessageContaining ("Expected HTTP status 2xx, got 500" );
148
136
requestAssertions ((request ) -> assertThat (request .getMethod ()).isEqualTo ("POST" ));
@@ -154,7 +142,7 @@ void sendShouldCompressData() throws IOException, InterruptedException {
154
142
// This is gzip compressed 10000 times 'a'
155
143
byte [] compressed = Base64 .getDecoder ()
156
144
.decode ("H4sIAAAAAAAA/+3BMQ0AAAwDIKFLj/k3UR8NcA8AAAAAAAAAAAADUsAZfeASJwAA" );
157
- mockBackEnd .enqueue (new MockResponse ());
145
+ this . mockBackEnd .enqueue (new MockResponse ());
158
146
this .sender .send (List .of (toByteArray (uncompressed )));
159
147
requestAssertions ((request ) -> {
160
148
assertThat (request .getMethod ()).isEqualTo ("POST" );
@@ -168,35 +156,15 @@ void sendShouldCompressData() throws IOException, InterruptedException {
168
156
void shouldTimeout () throws IOException {
169
157
try (BytesMessageSender sender = createSender (Encoding .JSON , Duration .ofMillis (1 ))) {
170
158
MockResponse response = new MockResponse ().setResponseCode (200 ).setHeadersDelay (100 , TimeUnit .MILLISECONDS );
171
- mockBackEnd .enqueue (response );
159
+ this . mockBackEnd .enqueue (response );
172
160
assertThatIOException ().isThrownBy (() -> sender .send (Collections .emptyList ()))
173
161
.withMessageContaining ("timed out" );
174
162
}
175
163
}
176
164
177
165
private void requestAssertions (Consumer <RecordedRequest > assertions ) throws InterruptedException {
178
- RecordedRequest request = mockBackEnd .takeRequest ();
166
+ RecordedRequest request = this . mockBackEnd .takeRequest ();
179
167
assertThat (request ).satisfies (assertions );
180
168
}
181
169
182
- private static void clearRequests () throws InterruptedException {
183
- RecordedRequest request ;
184
- do {
185
- request = mockBackEnd .takeRequest (0 , TimeUnit .SECONDS );
186
- }
187
- while (request != null );
188
- }
189
-
190
- private static void clearResponses () {
191
- dispatcher .clear ();
192
- }
193
-
194
- private static final class ClearableDispatcher extends QueueDispatcher {
195
-
196
- void clear () {
197
- getResponseQueue ().clear ();
198
- }
199
-
200
- }
201
-
202
170
}
0 commit comments