You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2019/02/12 13:41:53 UTC

[incubator-skywalking] branch top-sql updated: Fix wrong order logic, and add a test case to verify, to fix https://github.com/apache/incubator-skywalking/pull/2239#discussion_r255948249

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch top-sql
in repository https://gitbox.apache.org/repos/asf/incubator-skywalking.git


The following commit(s) were added to refs/heads/top-sql by this push:
     new b844b36  Fix wrong order logic, and add a test case to verify, to fix https://github.com/apache/incubator-skywalking/pull/2239#discussion_r255948249
b844b36 is described below

commit b844b366f3d9c1856c6d583ea231c48c335a4935
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Tue Feb 12 21:41:44 2019 +0800

    Fix wrong order logic, and add a test case to verify, to fix https://github.com/apache/incubator-skywalking/pull/2239#discussion_r255948249
---
 .../analysis/data/LimitedSizeDataCollection.java   |  2 +-
 .../data/LimitedSizeDataCollectionTest.java        | 72 ++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/data/LimitedSizeDataCollection.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/data/LimitedSizeDataCollection.java
index 70b7966..12cd96c 100644
--- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/data/LimitedSizeDataCollection.java
+++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/data/LimitedSizeDataCollection.java
@@ -95,8 +95,8 @@ public class LimitedSizeDataCollection<STORAGE_DATA extends ComparableStorageDat
                 } else {
                     // Remove the smallest in top N list
                     // add the current value into the right position
-                    storageDataList.removeFirst();
                     storageDataList.add(i, value);
+                    storageDataList.removeFirst();
                 }
                 return;
             }
diff --git a/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/analysis/data/LimitedSizeDataCollectionTest.java b/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/analysis/data/LimitedSizeDataCollectionTest.java
new file mode 100644
index 0000000..852c884
--- /dev/null
+++ b/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/analysis/data/LimitedSizeDataCollectionTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.skywalking.oap.server.core.analysis.data;
+
+import java.util.Objects;
+import org.apache.skywalking.oap.server.core.storage.ComparableStorageData;
+import org.junit.*;
+
+/**
+ * @author wusheng
+ */
+public class LimitedSizeDataCollectionTest {
+    @Test
+    public void testPut() {
+        LimitedSizeDataCollection<MockStorageData> collection = new LimitedSizeDataCollection<>(5);
+        collection.put(new MockStorageData(1));
+        collection.put(new MockStorageData(3));
+        collection.put(new MockStorageData(5));
+        collection.put(new MockStorageData(7));
+        collection.put(new MockStorageData(9));
+
+        MockStorageData income = new MockStorageData(4);
+        collection.put(income);
+
+        int[] expected = new int[] {3, 4, 5, 7, 9};
+        int i = 0;
+        for (MockStorageData data : collection.collection()) {
+            Assert.assertEquals(expected[i++], data.latency);
+        }
+    }
+
+    private class MockStorageData implements ComparableStorageData {
+        private long latency;
+
+        public MockStorageData(long latency) {
+            this.latency = latency;
+        }
+
+        @Override public int compareTo(Object o) {
+            MockStorageData target = (MockStorageData)o;
+            return (int)(latency - target.latency);
+        }
+
+        @Override public String id() {
+            return null;
+        }
+
+        @Override public boolean equals(Object o) {
+            return true;
+        }
+
+        @Override public int hashCode() {
+            return Objects.hash(1);
+        }
+    }
+}