mcvsubbu commented on a change in pull request #4649: Use timed wait around 
get() calls in PinotInstanceRestletResourceTest
URL: https://github.com/apache/incubator-pinot/pull/4649#discussion_r328709772
 
 

 ##########
 File path: 
pinot-controller/src/test/java/org/apache/pinot/controller/api/PinotInstanceRestletResourceTest.java
 ##########
 @@ -99,60 +111,69 @@ public void testInstanceListingAndCreation()
     }
 
     // Check that there are still 5 instances
-    
assertEquals(JsonUtils.stringToJsonNode(sendGetRequest(listInstancesUrl)).get("instances").size(),
 5);
+    checkNumInstances(listInstancesUrl, 5);
 
     // Check that the instances are properly created
-    JsonNode instance = JsonUtils
-        
.stringToJsonNode(sendGetRequest(_controllerRequestURLBuilder.forInstanceInformation("Broker_1.2.3.4_1234")));
-    assertEquals(instance.get("instanceName").asText(), "Broker_1.2.3.4_1234");
-    assertEquals(instance.get("hostName").asText(), "Broker_1.2.3.4");
-    assertEquals(instance.get("port").asText(), "1234");
-    assertTrue(instance.get("enabled").asBoolean());
-    assertEquals(instance.get("tags").size(), 0);
-    assertTrue(instance.get("pools").isNull());
-
-    instance = JsonUtils
-        
.stringToJsonNode(sendGetRequest(_controllerRequestURLBuilder.forInstanceInformation("Server_1.2.3.4_2345")));
-    assertEquals(instance.get("instanceName").asText(), "Server_1.2.3.4_2345");
-    assertEquals(instance.get("hostName").asText(), "Server_1.2.3.4");
-    assertEquals(instance.get("port").asText(), "2345");
-    assertTrue(instance.get("enabled").asBoolean());
-    assertEquals(instance.get("tags").size(), 0);
-    assertTrue(instance.get("pools").isNull());
-
-    instance = JsonUtils
-        
.stringToJsonNode(sendGetRequest(_controllerRequestURLBuilder.forInstanceInformation("Broker_2.3.4.5_1234")));
-    assertEquals(instance.get("instanceName").asText(), "Broker_2.3.4.5_1234");
-    assertEquals(instance.get("hostName").asText(), "Broker_2.3.4.5");
-    assertEquals(instance.get("port").asText(), "1234");
-    assertTrue(instance.get("enabled").asBoolean());
-    JsonNode tags = instance.get("tags");
-    assertEquals(tags.size(), 1);
-    assertEquals(tags.get(0).asText(), "tag_BROKER");
-    assertTrue(instance.get("pools").isNull());
-
-    instance = JsonUtils
-        
.stringToJsonNode(sendGetRequest(_controllerRequestURLBuilder.forInstanceInformation("Server_2.3.4.5_2345")));
-    assertEquals(instance.get("instanceName").asText(), "Server_2.3.4.5_2345");
-    assertEquals(instance.get("hostName").asText(), "Server_2.3.4.5");
-    assertEquals(instance.get("port").asText(), "2345");
-    assertTrue(instance.get("enabled").asBoolean());
-    tags = instance.get("tags");
-    assertEquals(tags.size(), 2);
-    assertEquals(tags.get(0).asText(), "tag_OFFLINE");
-    assertEquals(tags.get(1).asText(), "tag_REALTIME");
-    JsonNode pools = instance.get("pools");
-    assertEquals(pools.size(), 2);
-    assertEquals(pools.get("tag_OFFLINE").asText(), "0");
-    assertEquals(pools.get("tag_REALTIME").asText(), "1");
-
-    // Check that an error is given for an instance that does not exist
-    try {
-      
sendGetRequest(_controllerRequestURLBuilder.forInstanceInformation("Server_potato_8126"));
-      fail("Request to get instance information for an instance that does not 
exist did not fail");
-    } catch (IOException e) {
-      // Expected
-    }
+    checkInstanceInfo("Broker_1.2.3.4_1234", "Broker_1.2.3.4", 1234, new 
String[0], null, null);
+    checkInstanceInfo("Server_1.2.3.4_2345", "Server_1.2.3.4", 2345, new 
String[0], null, null);
+    checkInstanceInfo("Broker_2.3.4.5_1234", "Broker_2.3.4.5", 1234, new 
String[]{"tag_BROKER"}, null, null);
+    checkInstanceInfo("Server_2.3.4.5_2345", "Server_2.3.4.5", 2345, new 
String[]{"tag_OFFLINE", "tag_REALTIME"},
+        new String[]{"tag_OFFLINE", "tag_REALTIME"}, new int[]{0, 1});
+  }
+
+  private void checkInstanceInfo(String instanceName, String hostName, int 
port, String[] tags, String[] pools,
+      int[] poolValues) {
+    TestUtils.waitForCondition(new Function<Void, Boolean>() {
+      @Nullable
+      @Override
+      public Boolean apply(@Nullable Void aVoid) {
+        try {
+          String getResponse =
+              
sendGetRequest(_controllerRequestURLBuilder.forInstanceInformation(instanceName));
+          JsonNode instance = JsonUtils.stringToJsonNode(getResponse);
+          boolean result =
+              (instance.get("instanceName") != null) && 
(instance.get("instanceName").asText().equals(instanceName))
+                  && (instance.get("hostName") != null) && 
(instance.get("hostName").asText().equals(hostName)) && (
+                  instance.get("port") != null) && 
(instance.get("port").asText().equals(String.valueOf(port)))
+                  && (instance.get("enabled").asBoolean()) && 
(instance.get("tags") != null) && (
+                  instance.get("tags").size() == tags.length);
+
+          for (int i = 0; i < tags.length; i++) {
+            result = result && 
instance.get("tags").get(i).asText().equals(tags[i]);
+          }
+
+          if (!result) {
+            return false;
+          }
+
+          if (pools != null) {
+            result = result && (instance.get("pools") != null) && 
(instance.get("pools").size() == pools.length);
+            for (int i = 0; i < pools.length; i++) {
+              result = result && 
instance.get("pools").get(pools[i]).asText().equals((String.valueOf(poolValues[i])));
+            }
+          } else {
+            result = result && instance.get("pools").isNull();
+          }
+
+          return result;
+        } catch (Exception e) {
+          throw new RuntimeException(e);
+        }
+      }
+    }, 500L, 10000L, "Failed to retrieve correct information for instance: " + 
instanceName);
 
 Review comment:
   same. Use the other signature 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to