|
16 | 16 | import java.util.Random; |
17 | 17 | import java.util.concurrent.Callable; |
18 | 18 | import java.util.concurrent.CountDownLatch; |
| 19 | +import java.util.concurrent.ArrayBlockingQueue; |
| 20 | +import java.util.concurrent.LinkedBlockingQueue; |
| 21 | +import java.util.concurrent.BlockingQueue; |
19 | 22 | import java.util.logging.Logger; |
20 | 23 |
|
21 | 24 | import static org.hamcrest.MatcherAssert.assertThat; |
@@ -1337,4 +1340,59 @@ public SlowStatsDNonBlockingStatsDClient build() throws StatsDClientException { |
1337 | 1340 | } |
1338 | 1341 | } |
1339 | 1342 | } |
| 1343 | + |
| 1344 | + @Test(timeout = 5000L) |
| 1345 | + public void failed_write_buffer() throws Exception { |
| 1346 | + final BlockingQueue sync = new ArrayBlockingQueue(1); |
| 1347 | + final IOException marker = new IOException(); |
| 1348 | + NonBlockingStatsDClientBuilder builder = new NonBlockingStatsDClientBuilder() { |
| 1349 | + @Override |
| 1350 | + public NonBlockingStatsDClient build() { |
| 1351 | + this.originDetectionEnabled(false); |
| 1352 | + this.bufferPoolSize(1); |
| 1353 | + return new NonBlockingStatsDClient(resolve()) { |
| 1354 | + @Override |
| 1355 | + ClientChannel createByteChannel(Callable<SocketAddress> addressLookup, int timeout, int bufferSize) throws Exception { |
| 1356 | + return new DatagramClientChannel(addressLookup.call()) { |
| 1357 | + @Override |
| 1358 | + public int write(ByteBuffer data) throws IOException { |
| 1359 | + try { |
| 1360 | + sync.put(new Object()); |
| 1361 | + } catch (InterruptedException e) { |
| 1362 | + } |
| 1363 | + throw marker; |
| 1364 | + } |
| 1365 | + }; |
| 1366 | + } |
| 1367 | + }; |
| 1368 | + } |
| 1369 | + }; |
| 1370 | + |
| 1371 | + final BlockingQueue errors = new LinkedBlockingQueue(); |
| 1372 | + NonBlockingStatsDClient client = builder |
| 1373 | + .hostname("localhost") |
| 1374 | + .blocking(true) |
| 1375 | + .errorHandler(new StatsDClientErrorHandler() { |
| 1376 | + @Override |
| 1377 | + public void handle(Exception ex) { |
| 1378 | + if (ex == marker) { |
| 1379 | + return; |
| 1380 | + } |
| 1381 | + System.out.println(ex.toString()); |
| 1382 | + try { |
| 1383 | + errors.put(ex); |
| 1384 | + } catch (InterruptedException e) { |
| 1385 | + } |
| 1386 | + } |
| 1387 | + |
| 1388 | + }) |
| 1389 | + .build(); |
| 1390 | + |
| 1391 | + client.gauge("test", 1); |
| 1392 | + sync.take(); |
| 1393 | + client.gauge("test", 1); |
| 1394 | + client.stop(); |
| 1395 | + |
| 1396 | + assertEquals(0, errors.size()); |
| 1397 | + } |
1340 | 1398 | } |
0 commit comments