summaryrefslogtreecommitdiffstats
path: root/vespalog/src/test/java/com/yahoo/log/event/EventTestCase.java
blob: de116bc2e7485566177db72425f8ee089a0e256e (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.log.event;

import java.util.logging.Logger;

import com.yahoo.log.VespaFormatter;
import com.yahoo.log.event.Collection;
import com.yahoo.log.event.Count;
import com.yahoo.log.event.Crash;
import com.yahoo.log.event.Event;
import com.yahoo.log.event.MalformedEventException;
import com.yahoo.log.event.Progress;
import com.yahoo.log.event.Reloaded;
import com.yahoo.log.event.Reloading;
import com.yahoo.log.event.Started;
import com.yahoo.log.event.Starting;
import com.yahoo.log.event.Stopped;
import com.yahoo.log.event.Stopping;
import com.yahoo.log.event.Unknown;
import com.yahoo.log.event.Value;

public class EventTestCase extends junit.framework.TestCase {
    Count     countEvent;
    Count     floatCountEvent;
    Value     valueEvent;
    Value     floatValueEvent;
    Crash     crashEvent;
    Progress  progressEvent1;
    Progress  progressEvent2;
    Reloading reloadingEvent;
    Reloaded  reloadedEvent;
    Starting  startingEvent;
    Started   startedEvent;
    Stopping  stoppingEvent;
    Stopped   stoppedEvent;
    Collection collectionEvent;
    Unknown   unknownEvent;

    public EventTestCase (String name) {
        super(name);
    }

    public void setUp() {
        countEvent     = new Count("thecounter", 1234);
        floatCountEvent= new Count("thecounter", 1234.23);
        valueEvent     = new Value("thevalue", 4566);
        floatValueEvent= new Value("thevalue", 4566.23);
        crashEvent     = new Crash("appname", 1234, 11);
        progressEvent1 = new Progress("thename", 1);
        progressEvent2 = new Progress("thename", 1.0, 2.0);
        reloadingEvent = new Reloading("thefilewereloading");
        reloadedEvent  = new Reloaded("thefilewereloaded");
        startingEvent  = new Starting("startingName");
        startedEvent   = new Started("startedName");
        stoppingEvent  = new Stopping("stoppingName", "because we want to");
        stoppedEvent   = new Stopped("stoppedName", 1234, 1);
        collectionEvent=new Collection(123456, "thename");
        unknownEvent   = new Unknown();
    }

    // make sure we can make the test instances okay
    public void testExists () {
        assertNotNull(countEvent);
        assertNotNull(floatCountEvent);
        assertNotNull(valueEvent);
        assertNotNull(floatValueEvent);
        assertNotNull(crashEvent);
        assertNotNull(progressEvent1);
        assertNotNull(progressEvent2);
        assertNotNull(reloadingEvent);
        assertNotNull(reloadedEvent);
        assertNotNull(startingEvent);
        assertNotNull(startedEvent);
        assertNotNull(stoppingEvent);
        assertNotNull(stoppedEvent);
        assertNotNull(collectionEvent);
        assertNotNull(unknownEvent);
    }

    public void testEvents () {
        assertEquals("count/1 name=thecounter value=1234",
                     countEvent.toString());

        assertEquals("count/1 name=thecounter value=1234",
                     floatCountEvent.toString());

        assertEquals("crash/1 name=appname pid=1234 signal=11",
                     crashEvent.toString());

        assertEquals("progress/1 name=thename value=1.0 total=",
                     progressEvent1.toString());

        assertEquals("progress/1 name=thename value=1.0 total=2.0",
                     progressEvent2.toString());

        assertEquals("reloaded/1 name=thefilewereloaded",
                     reloadedEvent.toString());

        assertEquals("reloading/1 name=thefilewereloading",
                     reloadingEvent.toString());

        assertEquals("started/1 name=startedName",
                     startedEvent.toString());

        assertEquals("starting/1 name=startingName",
                     startingEvent.toString());

        assertEquals("stopping/1 name=stoppingName why=\"because we want to\"",
                     stoppingEvent.toString());

        assertEquals("collection/1 collectionId=123456 name=thename",
                     collectionEvent.toString());

        assertEquals("stopped/1 name=stoppedName pid=1234 exitcode=1",
                     stoppedEvent.toString());
    }

    /**
     * do the dirty work for testParser
     */
    private void parseEvent (Event e) throws MalformedEventException {
        assertEquals(e.toString(), Event.parse(e.toString()).toString());
    }

    public void testParser () throws MalformedEventException {
        parseEvent(countEvent);
        parseEvent(floatCountEvent);
        parseEvent(crashEvent);
        parseEvent(progressEvent1);
        parseEvent(progressEvent2);
        parseEvent(reloadingEvent);
        parseEvent(reloadedEvent);
        parseEvent(startingEvent);
        parseEvent(startedEvent);
        parseEvent(stoppingEvent);
        parseEvent(stoppedEvent);
    }

    public void testUnknownEvents () throws MalformedEventException {
        String s = "crappyevent/2 first=\"value one\" second=more third=4";
        Event event = Event.parse(s);
        assertEquals(s, event.toString());

        String s2 = "notinventedhere/999";
        assertEquals(s2, Event.parse(s2).toString());
    }

    /**
     * This test makes sure that the static event logging methods
     * successfully manage to find the right name on the calling
     * stack -- it should find the name of the calling class.
     */
    public void testFindingCallingClassLogger() {
        SingleHandler sh = new SingleHandler();
        assertNull(sh.lastRecord());

        VespaFormatter formatter = new VespaFormatter();
        Logger log = Logger.getLogger(EventTestCase.class.getName());
        synchronized(log) {
            log.setUseParentHandlers(false);
            log.addHandler(sh);
            Event.starting("mintest");

            assertTrue(formatter.format(sh.lastRecord()).
                       indexOf("\t.com.yahoo.log.event.EventTestCase\tevent\tstarting/1 name=mintest") > -1);

            Event.starting("startingName");
            Event.started("startedName");
            Event.stopping("stoppingName", "whyParam");
            Event.stopped("stoppedName", 1, 2);
            Event.reloading("reloadingName");
            Event.reloaded("reloadedName");
            Event.count("countName", 1);
            Event.progress("progressName", 1, 2);
            Event.crash("crashName", 1, 2);
        }
    }

    public void testFunnyEvent () {
        String funnyEvent = "collection/1 collectionId=1111111111 name=\"syncrows\" params=\"column=0 badrow=1 goodrow=0\"";
        try {
			Event e = Event.parse(funnyEvent);
        }
        catch (MalformedEventException e) {
            fail();
        }
    }

    public void testFullParse () {
        try {
        Event event = Event.parse("count/1 name=\"data_searched_mb\" value=15115168.3940149993");
        assertTrue(event instanceof Count);
        assertEquals("data_searched_mb", event.getValue("name"));
        assertEquals("15115168", event.getValue("value"));
        } catch (MalformedEventException e) {
            fail("Malformed Event Exception on parsing");
        }
    }
}