aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog/src/test/java/com/yahoo/log/VespaLogHandlerTestCase.java
blob: 64aec98eca2265443c08b53c1ec8556b31e62171 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.log;

import com.yahoo.log.impl.LogUtils;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import static java.time.Instant.ofEpochSecond;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author  Bjorn Borud
 */
@SuppressWarnings("removal")
public class VespaLogHandlerTestCase {
    private final static String hostname;
    private final static String pid;

    static final LogRecord record1;
    static final String record1String;

    static final LogRecord record2;
    private static final String record2String;

    private static final LogRecord record3;
    private static final String record3String;

    private static final LogRecord record4;
    private static final String record4String;

    static {
        hostname = LogUtils.getHostName();
        pid = LogUtils.getPID();

        record1 = new LogRecord(Level.INFO, "This is a test");
        record1.setInstant(ofEpochSecond(1100011348L, 29_123_543));
        record1String = "1100011348.029123\t"
            + hostname
            + "\t"
	    + pid
            + "/"
            + record1.getLongThreadID()
            + "\tmy-test-config-id\tTST\tinfo\tThis is a test";

        record2 = new LogRecord(Level.FINE, "This is a test too");
        record2.setInstant(ofEpochSecond(1100021348L, 29_987_654));
        record2.setLoggerName("com.yahoo.log.test");
        record2String = "1100021348.029987\t"
            + hostname
            + "\t"
	    + pid
            + "/" + record2.getLongThreadID() + "\tmy-test-config-id\tTST.com.yahoo.log.test\tdebug\tThis is a test too";

        record3 = new LogRecord(Level.WARNING, "another test");
        record3.setLoggerName("com.yahoo.log.test");
        record3.setInstant(ofEpochSecond(1107011348L, 29_000_000L));
        record3String = "1107011348.029000\t"
            + hostname
            + "\t"
	    + pid
            + "/" + record3.getLongThreadID() + "\tmy-test-config-id\tTST.com.yahoo.log.test"
            + "\twarning\tanother test";

        record4 = new LogRecord(Level.WARNING, "unicode \u00E6\u00F8\u00E5 test \u7881 unicode");
        record4.setLoggerName("com.yahoo.log.test");
        record4.setInstant(ofEpochSecond(1107011348, 29_000_001L));
        record4String = "1107011348.029000\t"
            + hostname
            + "\t"
	    + pid
            + "/" + record4.getLongThreadID() + "\tmy-test-config-id\tTST.com.yahoo.log.test"
            + "\twarning\tunicode \u00E6\u00F8\u00E5 test \u7881 unicode";
    }

    @Before
    public void setUp() {
        // System.setProperty("vespa.log.level", "all");
        // System.setProperty("vespa.log.control.dir", ".");
        // System.setProperty("config.id", "my-test-config-id");
    }

    @After
    public void tearDown() {
        new File("./my-test-config-id.logcontrol").delete();
    }

    /**
     * Perform simple test
     */
    @Test
    public void testSimple () throws IOException {
        File logfile = new File("./test1");
        File ctlfile = new File("./my-test-config-id.logcontrol");
        try {
            logfile.delete();
            ctlfile.delete();
            VespaLogHandler h
                = new VespaLogHandler(new FileLogTarget(new File("test1")),
                    new VespaLevelControllerRepo(ctlfile.getName(), "all", "TST"),
                    "my-test-config-id", "TST");
            h.publish(record1);
            h.cleanup();
            h.close();

            File f = new File("test1");
            assertTrue(f.exists());

            String[] lines = readFile(f.getName());

            // make sure there is only one line in the file
            assertEquals(1, lines.length);

            // make sure that ine line matches what is expected
            assertEquals(record1String, lines[0]);
        }
        finally {
            logfile.delete();
            ctlfile.delete();
        }
    }

    @Test
    public void testFallback() {
        File file = new File("mydir2");
        file.delete();
        assertTrue(file.mkdir());
        try {
            new VespaLogHandler(new FileLogTarget(file), new VespaLevelControllerRepo("my-test-config-id.logcontrol", "all", "TST"), "my-test-config-id", "TST");
        } catch (Exception e) {
            fail("Should not throw exception: " + e.getMessage());
        }
    }

    @After
    public void cleanup() { new File("mydir2").delete(); }

    /**
     * Perform simple test
     */
    @Test
    public void testLogCtl () {
        MockLevelController ctl = new MockLevelController();
        MockLevelControllerRepo ctlRepo = new MockLevelControllerRepo(ctl);
        MockLogTarget target = new MockLogTarget();
        VespaLogHandler h = new VespaLogHandler(target,
                ctlRepo,
                "my-test-config-id", "TST");
        ctl.setShouldLog(Level.INFO);
        h.publish(record1);
        h.publish(record2);
        h.publish(record3);
        h.publish(record4);

        ctl.setShouldLog(Level.CONFIG);
        h.publish(record1);
        h.publish(record2);
        h.publish(record3);
        h.publish(record4);

        ctl.setShouldLog(Level.WARNING);
        h.publish(record1);
        h.publish(record2);
        h.publish(record3);
        h.publish(record4);

        ctl.setShouldLog(Level.FINE);
        h.publish(record1);
        h.publish(record2);
        h.publish(record3);
        h.publish(record4);

        h.close();
        String [] lines = target.getLines();
        assertEquals(4, lines.length);
        assertEquals(record1String, lines[0]);
        assertEquals(record3String, lines[1]);
        //assertEquals(record4String, lines[2]);
        assertEquals(record2String, lines[3]);
    }

    /**
     * Make sure rotation works
     */
    @Test
    public void testRotate () throws IOException {
        // Doesn't work in Windows. TODO: Fix the logging stuff
        if (System.getProperty("os.name").toLowerCase().contains("win"))
            return;
        try {
            VespaLogHandler h
                = new VespaLogHandler(new FileLogTarget(new File("test2")),
                    new VespaLevelControllerRepo("my-test-config-id.logcontrol", "all", "TST"), "my-test-config-id",
                                      "TST"
            );
            h.publish(record1);

            // before rename
            assertTrue(new File("test2").exists());
            assertFalse(new File("test2-rotated").exists());

            // rename file
            assertTrue("Could rename test2 to test2-rotated",
                       new File("test2").renameTo(new File("test2-rotated")));

            // log next entry
            h.publish(record2);
            h.cleanup();
            h.close();

            // now make sure both files exist
            assertTrue(new File("test2").exists());
            assertTrue(new File("test2-rotated").exists());

            // read both files
            String[] lines1 = readFile("test2");
            String[] lines2 = readFile("test2-rotated");

            // make sure they have the correct number of lines
            assertEquals(1, lines1.length);
            assertEquals(1, lines2.length);

            // make sure they match the correct strings
            assertEquals(record2String, lines1[0]);
            assertEquals(record1String, lines2[0]);
        }
        finally {
            new File("test2").delete();
            new File("test2-rotated").delete();
        }
    }


    /**
     * This test was made in order to look for a race condition
     * which occurs when file rotation in the VespaLogHandler
     * occurs concurrently (which it should never do).
     */
    @Test
    public void testRaceCondition () throws FileNotFoundException {
        int numThreads = 10;
        Thread[] t = new Thread[numThreads];
        final CyclicBarrier barrier = new CyclicBarrier(numThreads);
        final int numLogEntries = 100;

        try {
            final VespaLogHandler h =
                new VespaLogHandler(new FileLogTarget(new File("test4")),
                        new VespaLevelControllerRepo("my-test-config-id.logcontrol", "all", "TST"), "my-test-config-id",
                                    "TST"
                );

            class LogRacer implements Runnable {

                private LogRacer() {
                }

                public void run () {
                    try {
                        barrier.await();
                        logLikeCrazy();
                    }
                    catch (BrokenBarrierException | InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

                void logLikeCrazy() {
                    for (int j = 0; j < numLogEntries; j++) {
                        try {
                            h.publish(record1);
                            h.publish(record2);
                        }
                        catch (Throwable t) {
                            fail(t.getMessage());
                        }
                    }
                }
            }

            for (int i = 0; i < numThreads; i++) {
                t[i] = new Thread(new LogRacer());
                t[i].start();
            }

            for (int i = 0; i < numThreads; i++) {
                t[i].join();
            }

            String[] lines = readFile("test4");
            assertTrue("num lines was " + lines.length
                       + " should be " +  (2 * numLogEntries * numThreads),
                       (lines.length == (2 * numLogEntries * numThreads)));

            h.cleanup();
            h.close();
        }
        catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        finally {
            new File("test4").delete();
        }

    }

    /**
     * Make sure unicode characters in log message works
     */
    @Test
    public void testUnicodeLog () throws IOException {
        try {
            VespaLogHandler h
                = new VespaLogHandler(new FileLogTarget(new File("test4")),
                    new VespaLevelControllerRepo("my-test-config-id.logcontrol", "all", "TST"), "my-test-config-id",
                                      "TST"
            );
            h.publish(record4);
            h.cleanup();
            h.close();

            // check that file exists
            assertTrue(new File("test4").exists());

            // read file
            String[] lines = readFile("test4");

            // make sure the file have the correct number of lines
            assertEquals(1, lines.length);

            // make sure the read lines match the correct string
            assertEquals(record4String, lines[0]);
        }
        finally {
            new File("test4").delete();
        }
    }

    /**
     * read a text file into a string array
     *
     */
    protected static String[] readFile (String fileName) {
        List<String> lines = new LinkedList<>();
        try (BufferedReader br = new BufferedReader(
                new InputStreamReader(new FileInputStream(new File(fileName)), StandardCharsets.UTF_8))) {
            for (String line = br.readLine();
                 line != null;
                 line = br.readLine()) {
                lines.add(line);
            }
            return lines.toArray(new String[0]);
        } catch (Throwable e) {
            return new String[0];
        }
    }

    private static class MockLevelControllerRepo implements LevelControllerRepo {
        private final LevelController levelController;
        MockLevelControllerRepo(LevelController controller) {
            this.levelController = controller;
        }

        @Override
        public LevelController getLevelController(String component) {
            return levelController;
        }

        @Override
        public void close() { }
    }

    private static class MockLevelController implements LevelController {

        private Level logLevel = Level.ALL;

        @Override
        public boolean shouldLog(Level level) {
            return (level.equals(logLevel));
        }

        void setShouldLog(Level level) {
            this.logLevel = level;
        }


        @Override
        public String getOnOffString() { return ""; }

        @Override
        public void checkBack() { }

        @Override
        public Level getLevelLimit() {
            return logLevel;
        }
    }

    private static class MockLogTarget implements LogTarget {
        private final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        String[] getLines() {
            return baos.toString().split("\n");
        }
        @Override
        public OutputStream open() {
            return baos;
        }

        @Override
        public void close() {
            try {
                baos.close();
            } catch (IOException e) {
                fail("Test failed: " + e.getMessage());
            }
        }
    }
}