forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix flaky MessageIdTest and introduce some testing improvements (apac…
…he#9286) * Refactor PulsarClient initialization and lifecycle management in tests * Add getter and setter to access remoteEndpointProtocolVersion field - it makes it easier to override for tests * Add hooks for overriding the producer implementation in PulsarClientImpl - useful for tests. Instead of relying on Mockito, there's a pure Java way to inject behavior to producer implementations for testing purposes * Introduce PulsarTestClient that contains ways to prevent race conditions and test flakiness - provides features for simulating failure conditions, for example the case of the broker connection disconnecting * Add solution for using Enums classes as source for TestNG DataProvider * Fix flaky MessageIdTest and move checksum related tests to new class * Fix NPE in PartitionedProducerImplTest
- Loading branch information
Showing
28 changed files
with
845 additions
and
539 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
buildtools/src/main/java/org/apache/pulsar/tests/EnumValuesDataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* 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.tests; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.testng.annotations.DataProvider; | ||
|
||
/** | ||
* TestNG DataProvider for passing all Enum values as parameters to a test method. | ||
* | ||
* Supports currently a single Enum parameter for a test method. | ||
*/ | ||
public abstract class EnumValuesDataProvider { | ||
@DataProvider | ||
public static final Object[][] values(Method testMethod) { | ||
Class<?> enumClass = Arrays.stream(testMethod.getParameterTypes()) | ||
.findFirst() | ||
.filter(Class::isEnum) | ||
.orElseThrow(() -> new IllegalArgumentException("The test method should have an enum parameter.")); | ||
return toDataProviderArray((Class<? extends Enum<?>>) enumClass); | ||
} | ||
|
||
/* | ||
* Converts all values of an Enum class to a TestNG DataProvider object array | ||
*/ | ||
public static Object[][] toDataProviderArray(Class<? extends Enum<?>> enumClass) { | ||
Enum<?>[] enumValues = enumClass.getEnumConstants(); | ||
return Stream.of(enumValues) | ||
.map(enumValue -> new Object[]{enumValue}) | ||
.collect(Collectors.toList()) | ||
.toArray(new Object[0][]); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
buildtools/src/test/java/org/apache/pulsar/tests/EnumValuesDataProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* 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.tests; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.testng.annotations.Test; | ||
|
||
public class EnumValuesDataProviderTest { | ||
enum Sample { | ||
A, B, C | ||
} | ||
|
||
@Test(dataProviderClass = EnumValuesDataProvider.class, dataProvider = "values") | ||
void testEnumValuesProvider(Sample sample) { | ||
System.out.println(sample); | ||
} | ||
|
||
@Test | ||
void shouldContainAllEnumValues() { | ||
verifyTestParameters(EnumValuesDataProvider.toDataProviderArray(Sample.class)); | ||
} | ||
|
||
@Test | ||
void shouldDetermineEnumValuesFromMethod() { | ||
Method testMethod = Arrays.stream(getClass().getDeclaredMethods()) | ||
.filter(method -> method.getName().equals("testEnumValuesProvider")) | ||
.findFirst() | ||
.get(); | ||
verifyTestParameters(EnumValuesDataProvider.values(testMethod)); | ||
} | ||
|
||
private void verifyTestParameters(Object[][] testParameters) { | ||
Set<Sample> enumValuesFromDataProvider = Arrays.stream(testParameters) | ||
.map(element -> element[0]) | ||
.map(Sample.class::cast) | ||
.collect(Collectors.toSet()); | ||
assertEquals(enumValuesFromDataProvider, new HashSet<>(Arrays.asList(Sample.values()))); | ||
} | ||
|
||
@Test(expectedExceptions = IllegalArgumentException.class) | ||
void shouldFailIfEnumParameterIsMissing() { | ||
Method testMethod = Arrays.stream(getClass().getDeclaredMethods()) | ||
.filter(method -> method.getName().equals("shouldFailIfEnumParameterIsMissing")) | ||
.findFirst() | ||
.get(); | ||
EnumValuesDataProvider.values(testMethod); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.