ramanathan1504 commented on code in PR #4518:
URL: https://github.com/apache/logging-log4j2/pull/4518#discussion_r4660197052


##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/net/DatagramOutputStreamTest.java:
##########
@@ -17,115 +17,159 @@
 package org.apache.logging.log4j.core.net;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import java.net.DatagramPacket;
 import java.net.DatagramSocket;
 import java.net.InetAddress;
-import java.net.SocketTimeoutException;
+import java.util.Arrays;
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.test.ListStatusListener;
 import org.apache.logging.log4j.test.junit.UsingStatusListener;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 
 class DatagramOutputStreamTest {
 
     private static final String LOOPBACK = "127.0.0.1";
     private static final int ENCODER_CHUNK_SIZE = 8 * 1024;
     private static final int MAX_DATAGRAM_PAYLOAD = 65_507;
 
+    /**
+     * Returns {@code length} bytes with a repeating pattern, so that 
truncation at the wrong offset is detected.
+     */
+    private static byte[] pattern(final int length) {
+        final byte[] bytes = new byte[length];
+        for (int i = 0; i < length; i++) {
+            bytes[i] = (byte) (i % 251);
+        }
+        return bytes;
+    }
+
+    private static DatagramSocket createReceiver() throws Exception {
+        final DatagramSocket receiver = new DatagramSocket(0, 
InetAddress.getByName(LOOPBACK));
+        receiver.setSoTimeout(2000);

Review Comment:
   On macOS with Java 8 a new `DatagramSocket` gets a 65,507-byte receive 
buffer. The kernel then drops the 65,507-byte datagrams, and four tests time 
out in the `macos-latest` job. A larger receive buffer makes all 11 pass on 
Java 8 and 17:
   
   ```suggestion
           receiver.setSoTimeout(2000);
           receiver.setReceiveBufferSize(4 * MAX_DATAGRAM_PAYLOAD);
   ```



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/net/DatagramOutputStreamTest.java:
##########
@@ -17,115 +17,159 @@
 package org.apache.logging.log4j.core.net;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import java.net.DatagramPacket;
 import java.net.DatagramSocket;
 import java.net.InetAddress;
-import java.net.SocketTimeoutException;
+import java.util.Arrays;
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.test.ListStatusListener;
 import org.apache.logging.log4j.test.junit.UsingStatusListener;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 
 class DatagramOutputStreamTest {
 
     private static final String LOOPBACK = "127.0.0.1";
     private static final int ENCODER_CHUNK_SIZE = 8 * 1024;
     private static final int MAX_DATAGRAM_PAYLOAD = 65_507;
 
+    /**
+     * Returns {@code length} bytes with a repeating pattern, so that 
truncation at the wrong offset is detected.
+     */
+    private static byte[] pattern(final int length) {
+        final byte[] bytes = new byte[length];
+        for (int i = 0; i < length; i++) {
+            bytes[i] = (byte) (i % 251);
+        }
+        return bytes;
+    }
+
+    private static DatagramSocket createReceiver() throws Exception {
+        final DatagramSocket receiver = new DatagramSocket(0, 
InetAddress.getByName(LOOPBACK));
+        receiver.setSoTimeout(2000);
+        return receiver;
+    }
+
+    private static byte[] receive(final DatagramSocket receiver) throws 
Exception {
+        // One byte more than the maximum, to detect datagrams that are too 
long
+        final byte[] buffer = new byte[MAX_DATAGRAM_PAYLOAD + 1];
+        final DatagramPacket packet = new DatagramPacket(buffer, 
buffer.length);
+        receiver.receive(packet);
+        return Arrays.copyOf(buffer, packet.getLength());
+    }
+
+    private static void assertTruncationWarning(final ListStatusListener 
statusListener) {
+        assertThat(statusListener.getStatusData())
+                .anyMatch(data -> data.getLevel() == Level.WARN
+                        && 
data.getMessage().getFormattedMessage().contains("truncated"));
+    }

Review Comment:
   The tests still pass if the warning prints `truncatedLength` instead of the 
original length. Can the check compare the whole message? The callers would 
pass `event.length` (or `event.length + footer.length`):
   
   ```suggestion
       private static void assertTruncationWarning(final ListStatusListener 
statusListener, final long originalLength) {
           assertThat(statusListener.getStatusData())
                   .anyMatch(data -> data.getLevel() == Level.WARN
                           && data.getMessage()
                                   .getFormattedMessage()
                                   .equals("UDP datagram truncated from " + 
originalLength + " to 65507 bytes"));
       }
   ```



##########
src/changelog/.2.x.x/4333_grow_datagram_output_stream_buffer.xml:
##########
@@ -7,7 +7,8 @@
        type="fixed">
   <issue id="4333" 
link="https://github.com/apache/logging-log4j2/issues/4333"/>
   <issue id="4336" link="https://github.com/apache/logging-log4j2/pull/4336"/>
+  <issue id="4517" 
link="https://github.com/apache/logging-log4j2/issues/4517"/>
   <description format="asciidoc">
-    Grow `DatagramOutputStream` buffer instead of copying on every write, and 
drop oversized UDP payloads
+    Grow `DatagramOutputStream` buffer instead of copying on every write, and 
truncate UDP datagrams longer than 65,507 bytes instead of failing to send them

Review Comment:
   The entry needs the `pull/4518` link after the issue line. The description 
could also be one short line:
   
   ```suggestion
     <issue id="4517" 
link="https://github.com/apache/logging-log4j2/issues/4517"/>
     <issue id="4518" 
link="https://github.com/apache/logging-log4j2/pull/4518"/>
     <description format="asciidoc">
       Speed up UDP datagram writes and truncate oversized datagrams.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to