|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch 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 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.elasticsearch.common.logging; |
| 21 | + |
| 22 | +import org.elasticsearch.test.ESTestCase; |
| 23 | +import org.junit.Before; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.PrintStream; |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static org.elasticsearch.common.logging.LoggingOutputStream.DEFAULT_BUFFER_LENGTH; |
| 32 | +import static org.elasticsearch.common.logging.LoggingOutputStream.MAX_BUFFER_LENGTH; |
| 33 | +import static org.hamcrest.Matchers.contains; |
| 34 | +import static org.hamcrest.Matchers.containsString; |
| 35 | +import static org.hamcrest.Matchers.equalTo; |
| 36 | + |
| 37 | +public class LoggingOutputStreamTests extends ESTestCase { |
| 38 | + |
| 39 | + class TestLoggingOutputStream extends LoggingOutputStream { |
| 40 | + List<String> lines = new ArrayList<>(); |
| 41 | + |
| 42 | + TestLoggingOutputStream() { |
| 43 | + super(null, null); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + void log(String msg) { |
| 48 | + lines.add(msg); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + TestLoggingOutputStream loggingStream; |
| 53 | + PrintStream printStream; |
| 54 | + |
| 55 | + @Before |
| 56 | + public void createStream() { |
| 57 | + loggingStream = new TestLoggingOutputStream(); |
| 58 | + printStream = new PrintStream(loggingStream, false, StandardCharsets.UTF_8); |
| 59 | + } |
| 60 | + |
| 61 | + public void testEmptyLine() { |
| 62 | + printStream.println(""); |
| 63 | + assertTrue(loggingStream.lines.isEmpty()); |
| 64 | + printStream.flush(); |
| 65 | + assertTrue(loggingStream.lines.isEmpty()); |
| 66 | + } |
| 67 | + |
| 68 | + public void testNull() { |
| 69 | + printStream.write(0); |
| 70 | + printStream.flush(); |
| 71 | + assertTrue(loggingStream.lines.isEmpty()); |
| 72 | + } |
| 73 | + |
| 74 | + public void testFlushOnNewline() { |
| 75 | + printStream.println("hello"); |
| 76 | + printStream.println("world"); |
| 77 | + assertThat(loggingStream.lines, contains("hello", "world")); |
| 78 | + } |
| 79 | + |
| 80 | + public void testBufferExtension() { |
| 81 | + String longStr = randomAlphaOfLength(DEFAULT_BUFFER_LENGTH); |
| 82 | + String extraLongStr = randomAlphaOfLength(DEFAULT_BUFFER_LENGTH + 1); |
| 83 | + printStream.println(longStr); |
| 84 | + assertThat(loggingStream.threadLocal.get().bytes.length, equalTo(DEFAULT_BUFFER_LENGTH)); |
| 85 | + printStream.println(extraLongStr); |
| 86 | + assertThat(loggingStream.lines, contains(longStr, extraLongStr)); |
| 87 | + assertThat(loggingStream.threadLocal.get().bytes.length, equalTo(DEFAULT_BUFFER_LENGTH)); |
| 88 | + } |
| 89 | + |
| 90 | + public void testMaxBuffer() { |
| 91 | + String longStr = randomAlphaOfLength(MAX_BUFFER_LENGTH); |
| 92 | + String extraLongStr = longStr + "OVERFLOW"; |
| 93 | + printStream.println(longStr); |
| 94 | + printStream.println(extraLongStr); |
| 95 | + assertThat(loggingStream.lines, contains(longStr, longStr, "OVERFLOW")); |
| 96 | + } |
| 97 | + |
| 98 | + public void testClosed() { |
| 99 | + loggingStream.close(); |
| 100 | + IOException e = expectThrows(IOException.class, () -> loggingStream.write('a')); |
| 101 | + assertThat(e.getMessage(), containsString("buffer closed")); |
| 102 | + } |
| 103 | + |
| 104 | + public void testThreadIsolation() throws Exception { |
| 105 | + printStream.print("from thread 1"); |
| 106 | + Thread thread2 = new Thread(() -> { |
| 107 | + printStream.println("from thread 2"); |
| 108 | + }); |
| 109 | + thread2.start(); |
| 110 | + thread2.join(); |
| 111 | + printStream.flush(); |
| 112 | + assertThat(loggingStream.lines, contains("from thread 2", "from thread 1")); |
| 113 | + } |
| 114 | +} |
0 commit comments