forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowFunctionExecutorTest.java
233 lines (198 loc) · 9.73 KB
/
WindowFunctionExecutorTest.java
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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.pulsar.functions.windowing;
import com.google.gson.Gson;
import org.apache.pulsar.client.api.TypedMessageBuilder;
import org.apache.pulsar.functions.api.Context;
import org.apache.pulsar.functions.api.Record;
import org.apache.pulsar.common.functions.WindowConfig;
import org.apache.pulsar.functions.api.WindowContext;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
/**
* Unit tests for {@link WindowFunctionExecutor}
*/
public class WindowFunctionExecutorTest {
private static class TestWindowFunctionExecutor extends WindowFunctionExecutor<Long, Long> {
List<Window<Record<Long>>> windows = new ArrayList<>();
@Override
public Long process(Window<Record<Long>> inputWindow, WindowContext context) throws Exception {
windows.add(inputWindow);
return null;
}
}
private static class TestFunction implements Function<Collection<Long>, Long> {
@Override
public Long apply(Collection<Long> longs) {
return null;
}
}
private static class TestWrongFunction implements Function<Long, Long> {
@Override
public Long apply(Long aLong) {
return null;
}
}
private static class TestTimestampExtractor implements TimestampExtractor<Long> {
@Override
public long extractTimestamp(Long input) {
return input;
}
}
private static class TestWrongTimestampExtractor implements TimestampExtractor<String> {
@Override
public long extractTimestamp(String input) {
return Long.parseLong(input);
}
}
private TestWindowFunctionExecutor testWindowedPulsarFunction;
private Context context;
private WindowConfig windowConfig;
@BeforeMethod
public void setUp() {
testWindowedPulsarFunction = new TestWindowFunctionExecutor();
context = mock(Context.class);
doReturn("test-function").when(context).getFunctionName();
doReturn("test-namespace").when(context).getNamespace();
doReturn("test-tenant").when(context).getTenant();
Record<?> record = mock(Record.class);
doReturn(Optional.of("test-topic")).when(record).getTopicName();
doReturn(record).when(context).getCurrentRecord();
windowConfig = new WindowConfig();
windowConfig.setTimestampExtractorClassName(TestTimestampExtractor.class.getName());
windowConfig.setWindowLengthDurationMs(20L);
windowConfig.setSlidingIntervalDurationMs(10L);
windowConfig.setMaxLagMs(5L);
// trigger manually to avoid timing issues
windowConfig.setWatermarkEmitIntervalMs(100000L);
windowConfig.setActualWindowFunctionClassName(TestFunction.class.getName());
doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class))).when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
doReturn(Collections.singleton("test-source-topic")).when(context).getInputTopics();
doReturn("test-sink-topic").when(context).getOutputTopic();
}
@AfterMethod(alwaysRun = true)
public void cleanUp() {
testWindowedPulsarFunction.shutdown();
}
@Test(expectedExceptions = RuntimeException.class)
public void testExecuteWithWrongWrongTimestampExtractorType() throws Exception {
WindowConfig windowConfig = new WindowConfig();
windowConfig.setTimestampExtractorClassName(TestWrongTimestampExtractor.class.getName());
doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class)))
.when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
testWindowedPulsarFunction.process(10L, context);
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testExecuteWithWrongJavaWindowFunctionType() throws Exception {
WindowConfig windowConfig = new WindowConfig();
windowConfig.setActualWindowFunctionClassName(TestWrongFunction.class.getName());
doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class)))
.when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
testWindowedPulsarFunction.process(10L, context);
}
@Test
public void testExecuteWithTs() throws Exception {
long[] timestamps = {603, 605, 607, 618, 626, 636};
for (long ts : timestamps) {
Record<?> record = mock(Record.class);
doReturn(Optional.of("test-topic")).when(record).getTopicName();
doReturn(record).when(context).getCurrentRecord();
doReturn(ts).when(record).getValue();
testWindowedPulsarFunction.process(ts, context);
}
testWindowedPulsarFunction.waterMarkEventGenerator.run();
assertEquals(3, testWindowedPulsarFunction.windows.size());
Window<Record<Long>> first = testWindowedPulsarFunction.windows.get(0);
assertArrayEquals(
new long[]{603, 605, 607},
new long[]{first.get().get(0).getValue(), first.get().get(1).getValue(), first.get().get(2).getValue()});
Window<Record<Long>> second = testWindowedPulsarFunction.windows.get(1);
assertArrayEquals(
new long[]{603, 605, 607, 618},
new long[]{second.get().get(0).getValue(), second.get().get(1).getValue(), second.get().get(2).getValue(), second.get().get(3).getValue()});
Window<Record<Long>> third = testWindowedPulsarFunction.windows.get(2);
assertArrayEquals(new long[]{618, 626}, new long[]{third.get().get(0).getValue(), third.get().get(1).getValue()});
}
@Test
public void testPrepareLateTupleStreamWithoutTs() throws Exception {
context = mock(Context.class);
doReturn("test-function").when(context).getFunctionName();
doReturn("test-namespace").when(context).getNamespace();
doReturn("test-tenant").when(context).getTenant();
doReturn(Collections.singleton("test-source-topic")).when(context).getInputTopics();
doReturn("test-sink-topic").when(context).getOutputTopic();
WindowConfig windowConfig = new WindowConfig();
windowConfig.setWindowLengthDurationMs(20L);
windowConfig.setSlidingIntervalDurationMs(10L);
windowConfig.setLateDataTopic("$late");
windowConfig.setMaxLagMs(5L);
windowConfig.setWatermarkEmitIntervalMs(10L);
windowConfig.setActualWindowFunctionClassName(TestFunction.class.getName());
doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class)))
.when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
try {
testWindowedPulsarFunction.process(10L, context);
fail();
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "Late data topic can be defined only when specifying a "
+ "timestamp extractor class");
}
}
@Test
public void testExecuteWithLateTupleStream() throws Exception {
windowConfig.setLateDataTopic("$late");
doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class)))
.when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
TypedMessageBuilder typedMessageBuilder = mock(TypedMessageBuilder.class);
when(typedMessageBuilder.value(any())).thenReturn(typedMessageBuilder);
when(typedMessageBuilder.sendAsync()).thenReturn(CompletableFuture.anyOf());
when(context.newOutputMessage(anyString(), any())).thenReturn(typedMessageBuilder);
long[] timestamps = {603, 605, 607, 618, 626, 636, 600};
List<Long> events = new ArrayList<>(timestamps.length);
for (long ts : timestamps) {
events.add(ts);
Record<?> record = mock(Record.class);
doReturn(Optional.of("test-topic")).when(record).getTopicName();
doReturn(record).when(context).getCurrentRecord();
doReturn(ts).when(record).getValue();
testWindowedPulsarFunction.process(ts, context);
//Update the watermark to this timestamp
testWindowedPulsarFunction.waterMarkEventGenerator.run();
}
System.out.println(testWindowedPulsarFunction.windows);
long event = events.get(events.size() - 1);
}
}