-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathUtilsTest.java
266 lines (243 loc) · 9.74 KB
/
UtilsTest.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
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
// Copyright (c) 2020-2021 VMware, Inc. or its affiliates. All rights reserved.
//
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
package com.rabbitmq.stream.perf;
import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.params.provider.Arguments.of;
import com.rabbitmq.stream.OffsetSpecification;
import com.rabbitmq.stream.compression.Compression;
import com.rabbitmq.stream.perf.Utils.CompressionTypeConverter;
import com.rabbitmq.stream.perf.Utils.NameStrategyConverter;
import com.rabbitmq.stream.perf.Utils.PatternNameStrategy;
import com.rabbitmq.stream.perf.Utils.RangeTypeConverter;
import com.rabbitmq.stream.perf.Utils.SniServerNamesConverter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import javax.net.ssl.SNIHostName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import picocli.CommandLine;
import picocli.CommandLine.TypeConversionException;
public class UtilsTest {
CommandLine.ITypeConverter<OffsetSpecification> offsetSpecificationConverter =
new Utils.OffsetSpecificationTypeConverter();
CompressionTypeConverter compressionTypeConverter = new CompressionTypeConverter();
static Stream<Arguments> offsetSpecificationTypeConverterOkArguments() {
return Stream.of(
of("", OffsetSpecification.first()),
of("first", OffsetSpecification.first()),
of("FIRST", OffsetSpecification.first()),
of("last", OffsetSpecification.last()),
of("LAST", OffsetSpecification.last()),
of("next", OffsetSpecification.next()),
of("NEXT", OffsetSpecification.next()),
of("0", OffsetSpecification.offset(0)),
of("1000", OffsetSpecification.offset(1000)),
of("9223372036854775817", OffsetSpecification.offset(Long.MAX_VALUE + 10)),
of("2020-06-03T08:54:57Z", OffsetSpecification.timestamp(1591174497000L)),
of("2020-06-03T10:54:57+02:00", OffsetSpecification.timestamp(1591174497000L)));
}
static Stream<Arguments> streams() {
Stream<Arguments> arguments =
Stream.of(
of("1", "stream", Arrays.asList("stream")),
of("5", "stream", IntStream.range(1, 6).mapToObj(i -> "stream-" + i).collect(toList())),
of(
"10",
"stream",
IntStream.range(1, 11).mapToObj(i -> format("stream-%02d", i)).collect(toList())),
of(
"1-10",
"stream",
IntStream.range(1, 11).mapToObj(i -> format("stream-%02d", i)).collect(toList())),
of(
"50-500",
"stream",
IntStream.range(50, 501).mapToObj(i -> format("stream-%03d", i)).collect(toList())),
of(
"1-10",
"stream-%d",
IntStream.range(1, 11).mapToObj(i -> format("stream-%d", i)).collect(toList())),
of(
"50-500",
"stream-%d",
IntStream.range(50, 501).mapToObj(i -> format("stream-%d", i)).collect(toList())));
return arguments.flatMap(
arg -> {
String range = arg.get()[0].toString();
return range.contains("-")
? Stream.of(
of(range, arg.get()[1], arg.get()[2]),
of(range.replace("-", ".."), arg.get()[1], arg.get()[2]))
: Stream.of(arg);
});
}
@ParameterizedTest
@MethodSource("offsetSpecificationTypeConverterOkArguments")
void offsetSpecificationTypeConverterOk(String value, OffsetSpecification expected)
throws Exception {
assertThat(offsetSpecificationConverter.convert(value)).isEqualTo(expected);
}
@ParameterizedTest
@ValueSource(strings = {"foo", "-1", "2020-06-03"})
void offsetSpecificationTypeConverterKo(String value) {
assertThatThrownBy(() -> offsetSpecificationConverter.convert(value))
.isInstanceOf(CommandLine.TypeConversionException.class);
}
@ParameterizedTest
@ValueSource(
strings = {
"none", "gzip", "snappy", "lz4", "zstd",
"NONE", "GZIP", "SNAPPY", "LZ4", "ZSTD"
})
void compressionTypeConverterOk(String value) {
assertThat(compressionTypeConverter.convert(value))
.isEqualTo(Compression.valueOf(value.toUpperCase(Locale.ENGLISH)));
}
@ParameterizedTest
@ValueSource(strings = {"", "foo", "bar"})
void compressionTypeConverterKo(String value) {
assertThatThrownBy(() -> compressionTypeConverter.convert(value))
.isInstanceOf(TypeConversionException.class)
.hasMessageContaining("Accepted values are none, gzip, snappy, lz4, zstd");
}
@ParameterizedTest
@CsvSource({
"%s-%d,s1-2",
"stream-%s-consumer-%d,stream-s1-consumer-2",
"consumer-%2$d-on-stream-%1$s,consumer-2-on-stream-s1"
})
void consumerNameStrategy(String pattern, String expected) {
BiFunction<String, Integer, String> strategy = new PatternNameStrategy(pattern);
assertThat(strategy.apply("s1", 2)).isEqualTo(expected);
}
@Test
void producerConsumerNameStrategyConverterShouldReturnUuidWhenAskedForUuid() {
NameStrategyConverter nameStrategyConverter = new NameStrategyConverter();
BiFunction<String, Integer, String> nameStrategy = nameStrategyConverter.convert("uuid");
String name = nameStrategy.apply("stream", 1);
UUID.fromString(name);
assertThat(nameStrategy.apply("stream", 1)).isNotEqualTo(name);
}
@Test
void producerConsumerNameStrategyConverterShouldReturnEmptyStringWhenPatternIsEmptyString() {
NameStrategyConverter nameStrategyConverter = new NameStrategyConverter();
BiFunction<String, Integer, String> nameStrategy = nameStrategyConverter.convert("");
assertThat(nameStrategy.apply("stream", 1)).isEmpty();
assertThat(nameStrategy.apply("stream", 2)).isEmpty();
}
@Test
void producerConsumerNameStrategyConverterShouldReturnPatternStrategyWhenAsked() {
NameStrategyConverter nameStrategyConverter = new NameStrategyConverter();
BiFunction<String, Integer, String> nameStrategy =
nameStrategyConverter.convert("stream-%s-consumer-%d");
assertThat(nameStrategy).isInstanceOf(PatternNameStrategy.class);
assertThat(nameStrategy.apply("s1", 2)).isEqualTo("stream-s1-consumer-2");
}
@Test
void sniServerNamesConverter() throws Exception {
SniServerNamesConverter converter = new SniServerNamesConverter();
assertThat(converter.convert("")).isEmpty();
assertThat(converter.convert("localhost,dummy"))
.hasSize(2)
.contains(new SNIHostName("localhost"))
.contains(new SNIHostName("dummy"));
}
@Test
void writeReadLongInByteArray() {
byte[] array = new byte[8];
LongStream.of(
Long.MIN_VALUE,
Long.MAX_VALUE,
1,
128,
256,
33_000,
66_000,
1_000_000,
new Random().nextLong())
.forEach(
value -> {
Utils.writeLong(array, value);
assertThat(Utils.readLong(array)).isEqualTo(value);
});
}
@Test
void rotateList() {
List<String> hosts = Arrays.asList("host1", "host2", "host3");
Collections.rotate(hosts, -1);
assertThat(hosts).isEqualTo(Arrays.asList("host2", "host3", "host1"));
Collections.rotate(hosts, -1);
assertThat(hosts).isEqualTo(Arrays.asList("host3", "host1", "host2"));
}
@ParameterizedTest
@ValueSource(strings = {"1..10", "1-10", "1", "10"})
void rangeConverterOk(String value) {
assertThat(new RangeTypeConverter().convert(value)).isEqualTo(value);
}
@ParameterizedTest
@ValueSource(
strings = {
"0",
"dummy",
"-1",
"-1..2",
"-1-2",
"10..1",
"10-1",
"1..bb",
"1-bb",
"bb..1",
"bb-1",
"1..10..15",
"1-10-15"
})
void rangeConverterKo(String value) {
assertThatThrownBy(() -> new RangeTypeConverter().convert(value))
.isInstanceOf(CommandLine.TypeConversionException.class)
.hasMessageContaining("not valid");
}
@ParameterizedTest
@MethodSource
void streams(String range, String input, List<String> expected) {
assertThat(Utils.streams(range, Collections.singletonList(input)))
.hasSameSizeAs(expected)
.isEqualTo(expected);
}
@Test
void streamsStreamListShouldBeUsedWhenStreamCountIsOne() {
assertThat(Utils.streams("1", Arrays.asList("stream1", "stream2", "stream3")))
.hasSize(3)
.isEqualTo(Arrays.asList("stream1", "stream2", "stream3"));
}
@Test
void streamsSpecifyOnlyOneStreamWhenStreamCountIsSpecified() {
assertThatThrownBy(() -> Utils.streams("2", Arrays.asList("stream1", "stream2")))
.isInstanceOf(IllegalArgumentException.class);
}
}