|
12 | 12 | import java.io.EOFException; |
13 | 13 | import java.io.IOException; |
14 | 14 | import java.net.Socket; |
15 | | -import java.util.ArrayList; |
16 | | -import java.util.HashMap; |
17 | | -import java.util.List; |
18 | | -import java.util.Map; |
| 15 | +import java.util.*; |
19 | 16 | import java.util.concurrent.ConcurrentLinkedQueue; |
20 | 17 | import java.util.concurrent.CountDownLatch; |
21 | 18 | import java.util.concurrent.ExecutorService; |
@@ -418,4 +415,54 @@ public void run() { |
418 | 415 | assertEquals(0, bufferFull.getCount()); |
419 | 416 | assertEquals(i, elist.size()); |
420 | 417 | } |
| 418 | + |
| 419 | + @Test |
| 420 | + public void testBufferOverflow() throws Exception { |
| 421 | + // start mock fluentd |
| 422 | + int port = MockFluentd.randomPort(); |
| 423 | + MockFluentd fluentd = new MockFluentd(port, new MockFluentd.MockProcess() { |
| 424 | + public void process(MessagePack msgpack, Socket socket) throws IOException { |
| 425 | + BufferedInputStream in = new BufferedInputStream(socket.getInputStream()); |
| 426 | + try { |
| 427 | + Unpacker unpacker = msgpack.createUnpacker(in); |
| 428 | + while (true) { |
| 429 | + unpacker.read(Event.class); |
| 430 | + } |
| 431 | + //socket.close(); |
| 432 | + } catch (EOFException e) { |
| 433 | + // ignore |
| 434 | + } |
| 435 | + } |
| 436 | + }); |
| 437 | + fluentd.start(); |
| 438 | + |
| 439 | + // start senders |
| 440 | + Sender sender = new RawSocketSender("localhost", port, 3000, 256); |
| 441 | + Map<String, Object> data = new HashMap<String, Object>(); |
| 442 | + data.put("large", randomString(512)); |
| 443 | + boolean success = sender.emit("tag.label1", data); |
| 444 | + assertFalse(success); |
| 445 | + |
| 446 | + // close sender sockets |
| 447 | + sender.close(); |
| 448 | + // close mock server sockets |
| 449 | + fluentd.close(); |
| 450 | + } |
| 451 | + |
| 452 | + private static final String CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ "; |
| 453 | + |
| 454 | + private String randomString(int len) { |
| 455 | + StringBuilder sb = new StringBuilder(len); |
| 456 | + |
| 457 | + Random rnd = new Random(); |
| 458 | + for (int i = 0; i < len; i++) { |
| 459 | + if (i != 0 && i % 128 == 0) { |
| 460 | + sb.append("\r\n"); |
| 461 | + } |
| 462 | + |
| 463 | + sb.append(CHARS.charAt(rnd.nextInt(CHARS.length()))); |
| 464 | + } |
| 465 | + |
| 466 | + return sb.toString(); |
| 467 | + } |
421 | 468 | } |
0 commit comments