Skip to content

Commit

Permalink
fix: fix flaky test assertions (apache#2640)
Browse files Browse the repository at this point in the history
in test TestResourceAssignmentOptimizerAccessor
#testComputePartitionAssignment
  • Loading branch information
simonh5 committed Oct 5, 2023
1 parent 6241bcb commit ed8daee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
6 changes: 6 additions & 0 deletions helix-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@
<artifactId>swagger-models</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.apache.helix.model.IdealState;
import org.apache.helix.model.InstanceConfig;
import org.apache.helix.rest.server.resources.helix.ResourceAssignmentOptimizerAccessor;
import org.testng.Assert;
import org.json.JSONException;import org.skyscreamer.jsonassert.JSONAssert;import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -99,7 +99,7 @@ public void afterClass() {
}

@Test
public void testComputePartitionAssignment() throws IOException {
public void testComputePartitionAssignment() throws IOException, JSONException {
System.out.println("Start test :" + TestHelper.getTestMethodName());

// Test AddInstances, RemoveInstances and SwapInstances
Expand All @@ -121,8 +121,7 @@ public void testComputePartitionAssignment() throws IOException {
Assert.assertTrue(headers.containsKey(ResourceAssignmentOptimizerAccessor.RESPONSE_HEADER_KEY));
Assert.assertFalse(
headers.get(ResourceAssignmentOptimizerAccessor.RESPONSE_HEADER_KEY).isEmpty());
Assert.assertEquals(headers.get(ResourceAssignmentOptimizerAccessor.RESPONSE_HEADER_KEY).get(0),
"{instanceFilter=[], resourceFilter=[], returnFormat=IdealStateFormat}");
JSONAssert.assertEquals((String) headers.get(ResourceAssignmentOptimizerAccessor.RESPONSE_HEADER_KEY).get(0), "{instanceFilter=[], resourceFilter=[], returnFormat=IdealStateFormat}", false);

// Test partitionAssignment InstanceFilter
String payload2 = "{\"Options\" : { \"InstanceFilter\" : [\"" + liveInstances.get(0) + "\" , \""
Expand All @@ -142,18 +141,13 @@ public void testComputePartitionAssignment() throws IOException {
MultivaluedMap<String, Object> headers2 = response2.getHeaders();
Assert
.assertTrue(headers2.containsKey(ResourceAssignmentOptimizerAccessor.RESPONSE_HEADER_KEY));
List partitionAssignmentMetadata2 =
headers2.get(ResourceAssignmentOptimizerAccessor.RESPONSE_HEADER_KEY);
List partitionAssignmentMetadata2 = headers2.get(ResourceAssignmentOptimizerAccessor.RESPONSE_HEADER_KEY);
Assert.assertFalse(
headers2.get(ResourceAssignmentOptimizerAccessor.RESPONSE_HEADER_KEY).isEmpty());
Assert.assertTrue(
partitionAssignmentMetadata2.get(0).equals(
"{instanceFilter=[" + liveInstances.get(0) + ", " + liveInstances.get(1)
+ "], resourceFilter=[], returnFormat=IdealStateFormat}") ||
partitionAssignmentMetadata2.get(0).equals(
"{instanceFilter=[" + liveInstances.get(1) + ", " + liveInstances.get(0)
+ "], resourceFilter=[], returnFormat=IdealStateFormat}"),
partitionAssignmentMetadata2.get(0).toString());
JSONAssert.assertEquals("{instanceFilter=[" + liveInstances.get(0) + ", " + liveInstances.get(1) + "], "
+ "resourceFilter=[], " + "returnFormat=IdealStateFormat}", partitionAssignmentMetadata2.get(0).toString(), false);
JSONAssert.assertEquals("{instanceFilter=[" + liveInstances.get(0) + ", " + liveInstances.get(1) + "], "
+ "resourceFilter=[], " + "returnFormat=IdealStateFormat}", ((String) partitionAssignmentMetadata2.get(0)), false);

// Test partitionAssignment ResourceFilter
String payload3 =
Expand All @@ -177,14 +171,8 @@ public void testComputePartitionAssignment() throws IOException {
headers3.get(ResourceAssignmentOptimizerAccessor.RESPONSE_HEADER_KEY);
Assert.assertFalse(
headers3.get(ResourceAssignmentOptimizerAccessor.RESPONSE_HEADER_KEY).isEmpty());
Assert.assertTrue(
partitionAssignmentMetadata3.get(0).equals(
"{instanceFilter=[], resourceFilter=[" + resources.get(0) + ", " + resources.get(1)
+ "], returnFormat=IdealStateFormat}") ||
partitionAssignmentMetadata3.get(0).equals(
"{instanceFilter=[], resourceFilter=[" + resources.get(1) + ", " + resources.get(0)
+ "], returnFormat=IdealStateFormat}"),
partitionAssignmentMetadata3.get(0).toString());
JSONAssert.assertEquals("{instanceFilter=[], resourceFilter=[" + resources.get(0) + ", " + resources.get(1)
+ "], returnFormat=IdealStateFormat}", partitionAssignmentMetadata3.get(0).toString(), false);

// Test Option CurrentState format with AddInstances, RemoveInstances and SwapInstances
String payload4 = "{\"InstanceChange\" : { \"ActivateInstances\" : [\"" + toEnabledInstance
Expand All @@ -211,14 +199,9 @@ public void testComputePartitionAssignment() throws IOException {
headers4.get(ResourceAssignmentOptimizerAccessor.RESPONSE_HEADER_KEY);
Assert.assertFalse(
headers4.get(ResourceAssignmentOptimizerAccessor.RESPONSE_HEADER_KEY).isEmpty());
Assert.assertTrue(
partitionAssignmentMetadata4.get(0).equals(
"{instanceFilter=[], resourceFilter=[" + resources.get(0) + ", " + resources.get(1)
+ "], returnFormat=CurrentStateFormat}") ||
partitionAssignmentMetadata4.get(0).equals(
"{instanceFilter=[], resourceFilter=[" + resources.get(1) + ", " + resources.get(0)
+ "], returnFormat=CurrentStateFormat}"),
partitionAssignmentMetadata4.get(0).toString());
JSONAssert.assertEquals(partitionAssignmentMetadata4.get(0).toString(),
"{instanceFilter=[], resourceFilter=[" + resources.get(0) + ", " + resources.get(1)+ "], returnFormat" +
"=CurrentStateFormat}",false);

System.out.println("End test :" + TestHelper.getTestMethodName());
}
Expand Down

0 comments on commit ed8daee

Please sign in to comment.