summaryrefslogtreecommitdiffstats
path: root/logserver/src/test/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolderTestCase.java
blob: a0e97c28f767ec98af3890bc15ba2f5dad7ea4e2 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.logserver.handlers.lasterrorsholder;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yahoo.log.InvalidLogFormatException;
import com.yahoo.log.LogLevel;
import com.yahoo.log.LogMessage;
import com.yahoo.logserver.Server;
import com.yahoo.text.Utf8;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.StringWriter;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.time.Duration;
import java.time.Instant;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class LastErrorsHolderTestCase {

    private static final int serverPort = 18324;
    private static final int lastErrorsHolderPort = 18326;
    Server server;
    Thread serverThread;
    LastErrorsHolder lastErrorsHolder;

    @Before
    public void setUp() throws InterruptedException, IOException {
        server = Server.getInstance();
        server.initialize(serverPort);
        serverThread = new Thread(server);
        serverThread.start();
        lastErrorsHolder = new LastErrorsHolder(lastErrorsHolderPort);
    }

    @After
    public void tearDown() throws InterruptedException {
        if (serverThread!=null) {
            serverThread.interrupt();
            serverThread.join();
        }
        if (lastErrorsHolder !=null) lastErrorsHolder.close();
    }

    public String connectAndGetLogMessages() throws InterruptedException, IOException {
        SocketChannel socket = null;
        Instant start = Instant.now();
        while (Instant.now().isBefore(start.plus(Duration.ofMinutes(1)))) {
            try {
                socket = SocketChannel.open(new InetSocketAddress(lastErrorsHolderPort));
                break;
            } catch (Exception e) {
                Thread.sleep(100);
            }
        }
        if (socket == null) {
            throw new RuntimeException("Could not connect to server");
        }

        ByteBuffer buf = ByteBuffer.allocateDirect(10000);
        int bytesRead = socket.read(buf);
        byte[] bytes = new byte[bytesRead];
        buf.position(0);
        buf.get(bytes);
        socket.close();

        return Utf8.toString(bytes);
    }


    @Test
    public void testLastErrorsHolder() throws IOException, InvalidLogFormatException, InterruptedException {
        LastErrorsHolder.Message logMessage1 = new LastErrorsHolder.Message(1433996283, "host1.yahoo.com", "container", LogLevel.ERROR.getName(), "foo");
        LastErrorsHolder.Message logMessage2 = new LastErrorsHolder.Message(1433996284, "host2.yahoo.com", "container", LogLevel.ERROR.getName(), "bar");
        LastErrorsHolder.Message logMessage3 = new LastErrorsHolder.Message(1433996285, "host2.yahoo.com", "container", LogLevel.INFO.getName(), "bar");

        LastErrorsHolder.Messages messages = new LastErrorsHolder.Messages();

        // No log messages yet
        String logs = connectAndGetLogMessages();
        final ObjectMapper mapper = new ObjectMapper();
        StringWriter stringWriter = new StringWriter();
        mapper.writeValue(stringWriter, messages);
        assertThat(logs, is(stringWriter.toString()));

        // Three messages, one is at level INFO
        lastErrorsHolder.doHandle(createLogMessage(logMessage1));
        lastErrorsHolder.doHandle(createLogMessage(logMessage2));
        lastErrorsHolder.doHandle(createLogMessage(logMessage3));
        messages = new LastErrorsHolder.Messages();
        messages.addMessage(logMessage1);
        messages.addMessage(logMessage2);
        messages.setNumberOfErrors(2);
        // Not adding logMessage3, since it is at level INFO

        logs = connectAndGetLogMessages();
        stringWriter = new StringWriter();
        mapper.writeValue(stringWriter, messages);
        assertThat(logs, is(stringWriter.toString()));
    }

    private LogMessage createLogMessage(LastErrorsHolder.Message message) throws InvalidLogFormatException {
        return createLogMessage(message.getTime(), message.getHostname(), message.getService(), message.getLogLevel(), message.getMessage());
    }

    private LogMessage createLogMessage(long time, String hostname, String service, String logLevel, String message) throws InvalidLogFormatException {
        return LogMessage.parseNativeFormat(String.format("%d\t%s\t1/1\t%s\tcomponent\t%s\t%s", time, hostname, service, logLevel.toLowerCase(), message));
    }

}