aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/test/java/me/lucko/spark/common/sampler
diff options
context:
space:
mode:
Diffstat (limited to 'spark-common/src/test/java/me/lucko/spark/common/sampler')
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadDumperTest.java64
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadGrouperTest.java80
2 files changed, 144 insertions, 0 deletions
diff --git a/spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadDumperTest.java b/spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadDumperTest.java
new file mode 100644
index 0000000..b96f149
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadDumperTest.java
@@ -0,0 +1,64 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.sampler;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class ThreadDumperTest {
+
+ @Test
+ public void testAll() {
+ assertTrue(ThreadDumper.ALL.isThreadIncluded(1, "test"));
+ assertTrue(ThreadDumper.ALL.isThreadIncluded(2, "test2"));
+ }
+
+ @Test
+ public void testSpecific() {
+ Thread thread = new Thread(() -> {
+ try {
+ Thread.sleep(100_000);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ }, "test-thread-1");
+ thread.start();
+
+ ThreadDumper.Specific specific = new ThreadDumper.Specific(thread);
+
+ assertTrue(specific.isThreadIncluded(thread.getId(), "test-thread-1"));
+
+ Set<Thread> threads = specific.getThreads();
+ assertEquals(1, threads.size());
+ assertTrue(threads.contains(thread));
+
+ Set<String> threadNames = specific.getThreadNames();
+ assertEquals(1, threadNames.size());
+ assertTrue(threadNames.contains("test-thread-1"));
+
+ thread.interrupt();
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadGrouperTest.java b/spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadGrouperTest.java
new file mode 100644
index 0000000..5f4e5ae
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/sampler/ThreadGrouperTest.java
@@ -0,0 +1,80 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.sampler;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class ThreadGrouperTest {
+
+ @Test
+ public void testByName() {
+ ThreadGrouper threadGrouper = ThreadGrouper.BY_NAME.get();
+
+ String group = threadGrouper.getGroup(1, "main");
+ assertEquals("main", group);
+
+ String label = threadGrouper.getLabel("main");
+ assertEquals("main", label);
+ }
+
+ @Test
+ public void testAsOne() {
+ ThreadGrouper threadGrouper = ThreadGrouper.AS_ONE.get();
+
+ String group = threadGrouper.getGroup(1, "main");
+ assertEquals("root", group);
+
+ String label = threadGrouper.getLabel("root");
+ assertEquals("All (x1)", label);
+
+ group = threadGrouper.getGroup(2, "main2");
+ assertEquals("root", group);
+
+ label = threadGrouper.getLabel("root");
+ assertEquals("All (x2)", label);
+ }
+
+ @Test
+ public void testByPool() {
+ ThreadGrouper threadGrouper = ThreadGrouper.BY_POOL.get();
+
+ String group = threadGrouper.getGroup(1, "main");
+ assertEquals("main", group);
+
+ String label = threadGrouper.getLabel("main");
+ assertEquals("main", label);
+
+ group = threadGrouper.getGroup(2, "Test Pool - #1");
+ assertEquals("Test Pool", group);
+
+ label = threadGrouper.getLabel("Test Pool");
+ assertEquals("Test Pool (x1)", label);
+
+ group = threadGrouper.getGroup(3, "Test Pool - #2");
+ assertEquals("Test Pool", group);
+
+ label = threadGrouper.getLabel("Test Pool");
+ assertEquals("Test Pool (x2)", label);
+ }
+
+}