|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.logstash.common; |
| 21 | + |
| 22 | +import org.apache.logging.log4j.core.test.appender.ListAppender; |
| 23 | +import org.apache.logging.log4j.core.test.junit.LoggerContextRule; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.ClassRule; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | +import static org.hamcrest.Matchers.containsString; |
| 32 | +import static org.junit.Assert.assertEquals; |
| 33 | +import static org.junit.Assert.assertThrows; |
| 34 | +import static org.junit.Assert.assertTrue; |
| 35 | + |
| 36 | +/** |
| 37 | + * Verifies that BufferedTokenizer logs a WARN message with the dropped byte count whenever |
| 38 | + * buffered data is discarded due to the sizeLimit being exceeded. |
| 39 | + * |
| 40 | + * Two trigger points are tested: |
| 41 | + * 1. When a separator arrives after a sequence of dropped fragments (recovery in append). |
| 42 | + * 2. When flush() is called while dropped data is outstanding. |
| 43 | + */ |
| 44 | +public final class BufferedTokenizerDroppedDataLoggingTest { |
| 45 | + |
| 46 | + private static final String CONFIG = "log4j2-test1.xml"; |
| 47 | + |
| 48 | + @ClassRule |
| 49 | + public static LoggerContextRule CTX = new LoggerContextRule(CONFIG); |
| 50 | + |
| 51 | + private ListAppender appender; |
| 52 | + private BufferedTokenizer sut; |
| 53 | + |
| 54 | + @Before |
| 55 | + public void setUp() { |
| 56 | + appender = CTX.getListAppender("EventLogger").clear(); |
| 57 | + sut = new BufferedTokenizer("\n", 10); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void givenDroppedFragmentsWhenSeparatorArrivesInAppendThenWarnIsLoggedWithDroppedByteCount() { |
| 62 | + // "01234567890" (11 chars) — NOT dropped because lastFragmentSize starts at 0 |
| 63 | + sut.extract("01234567890"); |
| 64 | + |
| 65 | + // "AAAAA" (5 chars, no sep) — dropped: lastFragmentSize(11) > sizeLimit(10) |
| 66 | + sut.extract("AAAAA"); |
| 67 | + |
| 68 | + // "BBBBB" (5 chars, no sep) — dropped: still 11 > 10 |
| 69 | + sut.extract("BBBBB"); |
| 70 | + |
| 71 | + // No warn yet — separator hasn't arrived |
| 72 | + assertTrue("No warning should be emitted before a separator is seen", |
| 73 | + warnMessages().stream().noneMatch(m -> m.contains("dropped"))); |
| 74 | + |
| 75 | + // "\nCC" contains a separator → recovery triggers the warn with 10 dropped bytes |
| 76 | + sut.extract("\nCC"); |
| 77 | + |
| 78 | + assertEquals(1, warnMessages().size()); |
| 79 | + assertThat("Warning must report 10 dropped bytes (AAAAA + BBBBB)", first(warnMessages()), containsString("dropped 10 bytes")); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void givenMultipleBatchesOfDroppedDataWhenSeparatorArrivesRepeatedly_ThenEachBatchIsLoggedSeparately() { |
| 84 | + // First overrun: 11 chars, no sep → accumulated (lastFragmentSize = 11) |
| 85 | + sut.extract("01234567890"); |
| 86 | + |
| 87 | + // Drop 3 bytes |
| 88 | + sut.extract("AAA"); |
| 89 | + // Recovery: separator seen → warn("3 bytes dropped") |
| 90 | + sut.extract("\n"); |
| 91 | + |
| 92 | + assertEquals(1, warnMessages().size()); |
| 93 | + assertThat("First warn should report 3 dropped bytes", first(warnMessages()), containsString("dropped 3 bytes")); |
| 94 | + |
| 95 | + // Start a new overrun: 11 chars again → accumulated on top of existing content |
| 96 | + sut.extract("01234567890"); |
| 97 | + // Drop 7 bytes |
| 98 | + sut.extract("BBBBBBB"); |
| 99 | + // Recovery: separator seen → warn("7 bytes dropped") |
| 100 | + sut.extract("\n"); |
| 101 | + |
| 102 | + assertEquals(2, warnMessages().size()); |
| 103 | + assertThat("Second warn should report 7 dropped bytes", last(warnMessages()), containsString("dropped 7 bytes")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void givenDroppedFragmentsWhenFlushIsInvokedThenWarnIsLoggedWithDroppedByteCountBeforeThrowing() { |
| 108 | + // "01234567890" (11 chars) — accumulated, lastFragmentSize = 11 |
| 109 | + sut.extract("01234567890"); |
| 110 | + |
| 111 | + // Drop 4 bytes |
| 112 | + sut.extract("CCCC"); |
| 113 | + // Drop 6 bytes |
| 114 | + sut.extract("DDDDDD"); |
| 115 | + |
| 116 | + // No warn yet — separator hasn't arrived and flush not called |
| 117 | + assertTrue("No warning before flush", warnMessages().stream().noneMatch(m -> m.contains("dropped"))); |
| 118 | + |
| 119 | + // flush() must warn about dropped data then throw for the overrun partial token |
| 120 | + assertThrows(IllegalStateException.class, () -> sut.flush()); |
| 121 | + |
| 122 | + assertEquals(1, warnMessages().size()); |
| 123 | + assertThat("Warning must report 10 dropped bytes (CCCC + DDDDDD)", first(warnMessages()), containsString("dropped 10 bytes")); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void givenDroppedFragmentsWhenFlushIsInvokedThenWarnPrecedesTheException() { |
| 128 | + sut.extract("01234567890"); // accumulated, lastFragmentSize = 11 |
| 129 | + sut.extract("EEE"); // dropped (3 bytes) |
| 130 | + |
| 131 | + assertThrows(IllegalStateException.class, () -> sut.flush()); |
| 132 | + |
| 133 | + // The warn must have been emitted (before the exception propagated) |
| 134 | + assertEquals(1, warnMessages().size()); |
| 135 | + assertThat("Dropped-data warn must be logged even when flush throws", first(warnMessages()), containsString("dropped 3 bytes")); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void givenNoDroppedDataWhenFlushIsInvokedThenNoWarnIsLogged() { |
| 140 | + sut.extract("short"); |
| 141 | + sut.flush(); |
| 142 | + |
| 143 | + assertTrue("No dropped-data warn should appear when nothing was dropped", |
| 144 | + warnMessages().stream().noneMatch(m -> m.contains("dropped"))); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void givenNoDroppedDataWhenSeparatorArrivesNoWarnIsLogged() { |
| 149 | + sut.extract("hello\nworld"); |
| 150 | + |
| 151 | + assertTrue("No dropped-data warn should appear for normal tokenization", |
| 152 | + warnMessages().stream().noneMatch(m -> m.contains("dropped"))); |
| 153 | + } |
| 154 | + |
| 155 | + private List<String> warnMessages() { |
| 156 | + return appender.getMessages(); |
| 157 | + } |
| 158 | + |
| 159 | + private static String first(List<String> list) { |
| 160 | + return list.get(0); |
| 161 | + } |
| 162 | + |
| 163 | + private static String last(List<String> list) { |
| 164 | + return list.get(list.size() - 1); |
| 165 | + } |
| 166 | +} |
0 commit comments