aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/test/java/me/lucko/spark/common
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2024-07-29 22:54:12 +0100
committerLuck <git@lucko.me>2024-07-29 22:54:12 +0100
commit1fb00b191cb3efd5b44d18fd2730ac6683626d67 (patch)
tree04d02538d8afbf9e9867c81e3768ebe4bc66c5d0 /spark-common/src/test/java/me/lucko/spark/common
parent6e7cc883d2d716bfcfcf871956f2acb995474d2d (diff)
downloadspark-1fb00b191cb3efd5b44d18fd2730ac6683626d67.tar.gz
spark-1fb00b191cb3efd5b44d18fd2730ac6683626d67.tar.bz2
spark-1fb00b191cb3efd5b44d18fd2730ac6683626d67.zip
Add more automated tests
Diffstat (limited to 'spark-common/src/test/java/me/lucko/spark/common')
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java16
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java18
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/platform/PlatformStatisticsProviderTest.java30
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/sampler/SamplerTest.java105
4 files changed, 157 insertions, 12 deletions
diff --git a/spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java b/spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java
index ec3638f..10da849 100644
--- a/spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java
+++ b/spark-common/src/test/java/me/lucko/spark/common/SparkPlatformTest.java
@@ -20,27 +20,21 @@
package me.lucko.spark.common;
-import me.lucko.spark.test.plugin.TestCommandSender;
import me.lucko.spark.test.plugin.TestSparkPlugin;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
public class SparkPlatformTest {
@Test
public void testEnableDisable(@TempDir Path directory) {
- System.setProperty("spark.backgroundProfiler", "false");
-
- SparkPlatform platform = new SparkPlatform(new TestSparkPlugin(directory));
- platform.enable();
-
- platform.executeCommand(TestCommandSender.INSTANCE, new String[]{"help"}).join();
- platform.executeCommand(TestCommandSender.INSTANCE, new String[]{"profiler", "info"}).join();
- platform.executeCommand(TestCommandSender.INSTANCE, new String[]{"health"}).join();
-
- platform.disable();
+ try (TestSparkPlugin plugin = new TestSparkPlugin(directory)) {
+ assertTrue(plugin.platform().hasEnabled());
+ }
}
}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java
index 42492d1..b2bb384 100644
--- a/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java
+++ b/spark-common/src/test/java/me/lucko/spark/common/heapdump/HeapDumpSummaryTest.java
@@ -20,9 +20,14 @@
package me.lucko.spark.common.heapdump;
+import me.lucko.spark.proto.SparkHeapProtos;
import me.lucko.spark.test.TestClass;
+import me.lucko.spark.test.plugin.TestCommandSender;
+import me.lucko.spark.test.plugin.TestSparkPlugin;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import java.nio.file.Path;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -31,7 +36,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
public class HeapDumpSummaryTest {
@Test
- public void testHeapDumpSummary() throws Exception {
+ public void testHeapDumpSummary(@TempDir Path directory) throws Exception {
TestClass testClass1 = new TestClass();
TestClass testClass2 = new TestClass();
@@ -42,6 +47,17 @@ public class HeapDumpSummaryTest {
assertNotNull(thisClassEntry);
assertEquals(2, thisClassEntry.getInstances());
assertEquals(32, thisClassEntry.getBytes());
+
+ SparkHeapProtos.HeapData proto;
+ try (TestSparkPlugin plugin = new TestSparkPlugin(directory)) {
+ proto = dump.toProto(plugin.platform(), TestCommandSender.INSTANCE.toData());
+ }
+ assertNotNull(proto);
+
+ SparkHeapProtos.HeapEntry protoEntry = proto.getEntriesList().stream().filter(entry -> entry.getType().equals(TestClass.class.getName())).findAny().orElse(null);
+ assertNotNull(protoEntry);
+ assertEquals(2, protoEntry.getInstances());
+ assertEquals(32, protoEntry.getSize());
}
}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/platform/PlatformStatisticsProviderTest.java b/spark-common/src/test/java/me/lucko/spark/common/platform/PlatformStatisticsProviderTest.java
new file mode 100644
index 0000000..6726461
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/platform/PlatformStatisticsProviderTest.java
@@ -0,0 +1,30 @@
+package me.lucko.spark.common.platform;
+
+import me.lucko.spark.proto.SparkProtos;
+import me.lucko.spark.test.plugin.TestSparkPlugin;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.nio.file.Path;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class PlatformStatisticsProviderTest {
+
+ @Test
+ public void testSystemStatistics(@TempDir Path directory) {
+ try (TestSparkPlugin plugin = new TestSparkPlugin(directory)) {
+ SparkProtos.SystemStatistics systemStatistics = new PlatformStatisticsProvider(plugin.platform()).getSystemStatistics();
+ assertNotNull(systemStatistics);
+ }
+ }
+
+ @Test
+ public void testPlatformStatistics(@TempDir Path directory) {
+ try (TestSparkPlugin plugin = new TestSparkPlugin(directory)) {
+ SparkProtos.PlatformStatistics platformStatistics = new PlatformStatisticsProvider(plugin.platform()).getPlatformStatistics(null, true);
+ assertNotNull(platformStatistics);
+ }
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/sampler/SamplerTest.java b/spark-common/src/test/java/me/lucko/spark/common/sampler/SamplerTest.java
new file mode 100644
index 0000000..7db14a3
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/sampler/SamplerTest.java
@@ -0,0 +1,105 @@
+/*
+ * 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 me.lucko.spark.common.sampler.async.AsyncSampler;
+import me.lucko.spark.common.sampler.java.JavaSampler;
+import me.lucko.spark.common.sampler.node.MergeMode;
+import me.lucko.spark.common.sampler.source.ClassSourceLookup;
+import me.lucko.spark.common.util.MethodDisambiguator;
+import me.lucko.spark.proto.SparkSamplerProtos;
+import me.lucko.spark.test.TestClass2;
+import me.lucko.spark.test.plugin.TestCommandSender;
+import me.lucko.spark.test.plugin.TestSparkPlugin;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Locale;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+public class SamplerTest {
+
+ @ParameterizedTest
+ @EnumSource
+ public void testSampler(ProfilerType profilerType, @TempDir Path directory) {
+ if (profilerType == ProfilerType.ASYNC) {
+ String os = System.getProperty("os.name").toLowerCase(Locale.ROOT).replace(" ", "");
+ assumeTrue(os.equals("linux") || os.equals("macosx"), "async profiler is only supported on Linux and macOS");
+ }
+
+ Thread thread = new Thread(new TestClass2(), "Test Thread");
+ thread.start();
+
+ try (TestSparkPlugin plugin = new TestSparkPlugin(directory)) {
+ Sampler sampler = new SamplerBuilder()
+ .threadDumper(new ThreadDumper.Specific(thread))
+ .threadGrouper(ThreadGrouper.BY_POOL)
+ .samplingInterval(10)
+ .forceJavaSampler(profilerType == ProfilerType.JAVA)
+ .completeAfter(2, TimeUnit.SECONDS)
+ .start(plugin.platform());
+
+ assertInstanceOf(profilerType.expectedClass, sampler);
+
+ assertNotEquals(-1, sampler.getAutoEndTime());
+ sampler.getFuture().join();
+
+ Sampler.ExportProps exportProps = new Sampler.ExportProps()
+ .creator(TestCommandSender.INSTANCE.toData())
+ .mergeMode(() -> MergeMode.sameMethod(new MethodDisambiguator(plugin.platform().createClassFinder())))
+ .classSourceLookup(() -> ClassSourceLookup.create(plugin.platform()));
+
+ SparkSamplerProtos.SamplerData proto = sampler.toProto(plugin.platform(), exportProps);
+ assertNotNull(proto);
+
+ List<SparkSamplerProtos.ThreadNode> threads = proto.getThreadsList();
+ assertEquals(1, threads.size());
+
+ SparkSamplerProtos.ThreadNode protoThread = threads.get(0);
+ assertEquals("Test Thread", protoThread.getName());
+ assertTrue(protoThread.getChildrenList().stream().anyMatch(n -> n.getClassName().equals("me.lucko.spark.test.TestClass2") && n.getMethodName().equals("test")));
+ assertTrue(protoThread.getChildrenList().stream().anyMatch(n -> n.getClassName().equals("me.lucko.spark.test.TestClass2") && n.getMethodName().equals("testA")));
+ assertTrue(protoThread.getChildrenList().stream().anyMatch(n -> n.getClassName().equals("me.lucko.spark.test.TestClass2") && n.getMethodName().equals("testB")));
+ }
+ }
+
+ public enum ProfilerType {
+ JAVA(JavaSampler.class),
+ ASYNC(AsyncSampler.class);
+
+ private final Class<? extends Sampler> expectedClass;
+
+ ProfilerType(Class<? extends Sampler> expectedClass) {
+ this.expectedClass = expectedClass;
+ }
+ }
+
+}