ruanwenjun commented on code in PR #15993:
URL: 
https://github.com/apache/dolphinscheduler/pull/15993#discussion_r1599681595


##########
dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/queue/MasterPriorityQueue.java:
##########
@@ -80,15 +80,23 @@ public void clear() {
 
     private void refreshMasterList() {
         hostIndexMap.clear();
-        Iterator<Server> iterator = queue.iterator();
         int index = 0;
-        while (iterator.hasNext()) {
-            Server server = iterator.next();
+        for (Server server: getOrderedElements()) {
             String addr = NetUtils.getAddr(server.getHost(), server.getPort());
             hostIndexMap.put(addr, index);
             index += 1;
         }
+    }
 
+    /**
+     * get ordered collection of priority queue
+     *
+     * @return ordered collection
+     */
+    private Server[] getOrderedElements() {

Review Comment:
   ```suggestion
       Server[] getOrderedElements() {
   ```



##########
dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/queue/MasterPriorityQueueTest.java:
##########
@@ -0,0 +1,92 @@
+package org.apache.dolphinscheduler.service.queue;
+
+import org.apache.dolphinscheduler.common.model.Server;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
+import java.util.Date;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Assertions;
+
+public class MasterPriorityQueueTest {
+
+
+    @Test
+    public void getOrderedCollection () throws NoSuchMethodException, 
InvocationTargetException, IllegalAccessException {
+
+        MasterPriorityQueue queue = new MasterPriorityQueue();
+        Method getOrderedElementsMethod = 
MasterPriorityQueue.class.getDeclaredMethod("getOrderedElements");
+        getOrderedElementsMethod.setAccessible(true);
+
+        // Test empty queue
+        Server[] emptyElements = (Server[]) 
getOrderedElementsMethod.invoke(queue);
+        Assertions.assertArrayEquals(emptyElements, new Server[]{});
+
+        // Test queue with fabricated servers
+        queue.putAll(getServerList());
+        Server[] orderElements = (Server[]) 
getOrderedElementsMethod.invoke(queue);
+        Assertions.assertEquals(extractServerIds(orderElements), 
Arrays.asList(4, 2, 1, 3));
+
+    }

Review Comment:
   Don't use reflection to test private methods



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to