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
|
// Copyright Yahoo. 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.TestUtil;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@SuppressWarnings("removal")
public class EventTestCase {
Count countEvent;
Count floatCountEvent;
Value valueEvent;
Value floatValueEvent;
Crash crashEvent;
Progress progressEvent1;
Progress progressEvent2;
Starting startingEvent;
Started startedEvent;
Stopping stoppingEvent;
Stopped stoppedEvent;
Unknown unknownEvent;
@Before
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);
startingEvent = new Starting("startingName");
startedEvent = new Started("startedName");
stoppingEvent = new Stopping("stoppingName", "because we want to");
stoppedEvent = new Stopped("stoppedName", 1234, 1);
unknownEvent = new Unknown();
}
// make sure we can make the test instances okay
@Test
public void testExists () {
assertNotNull(countEvent);
assertNotNull(floatCountEvent);
assertNotNull(valueEvent);
assertNotNull(floatValueEvent);
assertNotNull(crashEvent);
assertNotNull(progressEvent1);
assertNotNull(progressEvent2);
assertNotNull(startingEvent);
assertNotNull(startedEvent);
assertNotNull(stoppingEvent);
assertNotNull(stoppedEvent);
assertNotNull(unknownEvent);
}
@Test
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("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("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());
}
@Test
public void testParser () throws MalformedEventException {
parseEvent(countEvent);
parseEvent(floatCountEvent);
parseEvent(crashEvent);
parseEvent(progressEvent1);
parseEvent(progressEvent2);
parseEvent(startingEvent);
parseEvent(startedEvent);
parseEvent(stoppingEvent);
parseEvent(stoppedEvent);
}
@Test
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.
*/
@Test
public void testFindingCallingClassLogger() {
SingleHandler sh = new SingleHandler();
assertNull(sh.lastRecord());
Logger log = Logger.getLogger(EventTestCase.class.getName());
synchronized(log) {
log.setUseParentHandlers(false);
log.addHandler(sh);
Event.starting("mintest");
assertTrue(TestUtil.formatWithVespaFormatter(sh.lastRecord())
.contains("\t.com.yahoo.log.event.EventTestCase\tevent\tstarting/1 name=mintest"));
Event.starting("startingName");
Event.started("startedName");
Event.stopping("stoppingName", "whyParam");
Event.stopped("stoppedName", 1, 2);
Event.count("countName", 1);
Event.progress("progressName", 1, 2);
Event.crash("crashName", 1, 2);
}
}
@Test
public void testFunnyEvent () {
String funnyEvent = "value/1 collectionId=1111111111 name=\"syncrows\" params=\"column=0 badrow=1 goodrow=0\"";
try {
Event e = Event.parse(funnyEvent);
}
catch (MalformedEventException e) {
fail();
}
}
@Test
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");
}
}
}
|