forked from linkedin/parseq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestTaskLogging.java
More file actions
273 lines (233 loc) · 8.73 KB
/
TestTaskLogging.java
File metadata and controls
273 lines (233 loc) · 8.73 KB
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
/*
* Copyright 2012 LinkedIn, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.linkedin.parseq;
import com.linkedin.parseq.trace.ResultType;
import org.testng.annotations.Test;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.fail;
/**
* @author Chris Pettitt (cpettitt@linkedin.com)
*/
public class TestTaskLogging extends BaseEngineTest
{
private static final String ALL_LOGGER = Engine.class.getName() + ":all";
private static final String ROOT_LOGGER = Engine.class.getName() + ":root";
private static final String PLAN_CLASS_LOGGER = Engine.class.getName() + ":planClass=";
@Test
public void testSingleTaskWithDefaultLogging() throws InterruptedException
{
final Task<?> task = TestUtil.noop();
getEngine().run(task);
assertTrue(task.await(5, TimeUnit.SECONDS));
assertEquals(0, getLogEntries(ALL_LOGGER).size());
assertEquals(0, getLogEntries(ROOT_LOGGER).size());
assertEquals(0, getLogEntries(planClassLogger(task)).size());
}
@Test
public void testSingleTaskCombinations() throws InterruptedException
{
final String taskValue = "value";
final String[] loggers = new String[] {
ALL_LOGGER, ROOT_LOGGER, planClassLogger(TestUtil.value("dummy", "dummy"))
};
final int[] levels = new int[] { ListLogger.LEVEL_DEBUG, ListLogger.LEVEL_TRACE };
for(String logger : loggers)
{
for (int level : levels)
{
resetLoggers();
final Task<?> task = TestUtil.value("t1", taskValue);
setLogLevel(logger, level);
getEngine().run(task);
assertTrue(task.await(5, TimeUnit.SECONDS));
for (String checkLogger : loggers)
{
if (logger.equals(checkLogger))
{
assertTaskLogged(task, taskValue, checkLogger, level);
}
else
{
assertEquals(Collections.emptyList(), getLogEntries(checkLogger));
}
}
}
}
}
@Test
public void testSingleTaskWithErrorCombinations() throws InterruptedException
{
final String errorMsg = "this task has failed";
final Exception exception = new Exception(errorMsg);
final String[] loggers = new String[] {
ALL_LOGGER, ROOT_LOGGER, planClassLogger(TestUtil.errorTask("dummy", exception))
};
final int[] levels = new int[] { ListLogger.LEVEL_DEBUG, ListLogger.LEVEL_TRACE };
for(String logger : loggers)
{
for (int level : levels)
{
resetLoggers();
final Task<?> task = TestUtil.errorTask("t1", exception);
setLogLevel(logger, level);
getEngine().run(task);
assertTrue(task.await(5, TimeUnit.SECONDS));
for (String checkLogger : loggers)
{
if (logger.equals(checkLogger))
{
assertTaskLogged(task, errorMsg, checkLogger, level);
}
else
{
assertEquals(Collections.emptyList(), getLogEntries(checkLogger));
}
}
}
}
}
@Test
public void testCompositeTaskWithAllLoggerTrace() throws InterruptedException
{
final Task<?> child1 = TestUtil.value("t1", "value");
final Task<?> child2 = TestUtil.noop();
final Task<?> parent = Tasks.seq(child1, child2);
setLogLevel(ALL_LOGGER, ListLogger.LEVEL_TRACE);
getEngine().run(parent);
assertTrue(parent.await(5, TimeUnit.SECONDS));
assertTaskLogged(parent, "null", ALL_LOGGER, ListLogger.LEVEL_TRACE);
assertTaskLogged(child1, "value", ALL_LOGGER, ListLogger.LEVEL_TRACE);
assertTaskLogged(child2, "null", ALL_LOGGER, ListLogger.LEVEL_TRACE);
}
@Test
public void testCompositeTaskWithRootLoggerTrace() throws InterruptedException
{
final Task<?> child1 = TestUtil.value("t1", "value");
final Task<?> child2 = TestUtil.noop();
final Task<?> parent = Tasks.seq(child1, child2);
setLogLevel(ROOT_LOGGER, ListLogger.LEVEL_TRACE);
getEngine().run(parent);
assertTrue(parent.await(5, TimeUnit.SECONDS));
assertTaskLogged(parent, "null", ROOT_LOGGER, ListLogger.LEVEL_TRACE);
final List<ListLogger.Entry> entries = getLogEntries(ROOT_LOGGER);
assertEquals("Should only log start and stop for the root trace: " + entries, 2, entries.size());
}
@Test
public void testCompositeTaskWithPlanClassLoggerTrace() throws InterruptedException
{
final Task<?> child1 = TestUtil.value("t1", "value");
final Task<?> child2 = TestUtil.noop();
final Task<?> parent = Tasks.seq(child1, child2);
final String planClassLogger = planClassLogger(parent);
setLogLevel(planClassLogger, ListLogger.LEVEL_TRACE);
getEngine().run(parent);
assertTrue(parent.await(5, TimeUnit.SECONDS));
assertTaskLogged(parent, "null", planClassLogger, ListLogger.LEVEL_TRACE);
assertTaskLogged(child1, "value", planClassLogger, ListLogger.LEVEL_TRACE);
assertTaskLogged(child2, "null", planClassLogger, ListLogger.LEVEL_TRACE);
}
private void assertTaskLogged(final Task<?> task,
final String taskValue,
final String loggerName,
final int level) throws InterruptedException
{
if (level != ListLogger.LEVEL_TRACE && level != ListLogger.LEVEL_DEBUG)
{
throw new IllegalArgumentException("Log level is not trace or debug");
}
retryAssertions(5, 20, new Runnable()
{
@Override
public void run()
{
final List<ListLogger.Entry> entries = getLogEntries(loggerName);
final int startIdx = assertHasStartLog(loggerName, task, entries);
final int endIdx = level == ListLogger.LEVEL_TRACE
? assertHasTraceEndLog(loggerName, task, taskValue, entries)
: assertHasDebugEndLog(loggerName, task, entries);
assertTrue("Start should happen before end. Start: " + startIdx + " End: " + endIdx,
startIdx < endIdx);
}
});
}
private int assertHasStartLog(String loggerName, Task<?> task, List<ListLogger.Entry> entries)
{
for (int i = 0; i < entries.size(); i++)
{
final String message = entries.get(i).getMessage();
if (message.contains("Starting task") && message.contains(task.getName()))
{
return i;
}
}
fail(loggerName + ": " + entries + " does not contain a start log entry for " + task.getName());
throw new RuntimeException("Execution never gets here - see preceding fail");
}
private int assertHasDebugEndLog(String loggerName, Task<?> task, List<ListLogger.Entry> entries)
{
for (int i = 0; i < entries.size(); i++)
{
final String message = entries.get(i).getMessage();
if (message.contains("Ending task") && message.contains(task.getName()) &&
message.contains(ResultType.fromTask(task).toString()))
{
return i;
}
}
fail(loggerName + ": " + entries + " does not contain an end log entry for " + task.getName());
throw new RuntimeException("Execution never gets here - see preceding fail");
}
private int assertHasTraceEndLog(String loggerName, Task<?> task, String taskValue, List<ListLogger.Entry> entries)
{
final int i = assertHasDebugEndLog(loggerName, task, entries);
final String message = entries.get(i).getMessage();
assertContains(taskValue, message);
return i;
}
private static void assertContains(String substring, String actual)
{
assertTrue("Actual string '" + actual + "' does not contain substring '" + substring + "'",
actual.contains(substring));
}
// Retries a runnable with asserts until all the asserts pass or
// retries * delayMillis has passed.
private static void retryAssertions(int retries, int delayMillis,
Runnable runnable) throws InterruptedException
{
for (int i = 0; i < retries; i++)
{
try
{
runnable.run();
return;
}
catch (AssertionError t)
{
// Swallow and retry after sleep
Thread.sleep(delayMillis);
}
}
runnable.run();
}
private static String planClassLogger(Task<?> plan)
{
return PLAN_CLASS_LOGGER + plan.getClass().getName();
}
}