aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/test/java/me/lucko/spark/common/tick
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2024-09-03 00:11:24 +0100
committerLuck <git@lucko.me>2024-09-03 00:11:24 +0100
commit35b557af4fd1bc0ce0a5745716e898cd1300a08c (patch)
tree5927e55839d5dd1a0759db3a2fd54f9f8df5d032 /spark-common/src/test/java/me/lucko/spark/common/tick
parent55b38397296813b66082ad935f773357c8ad5282 (diff)
downloadspark-35b557af4fd1bc0ce0a5745716e898cd1300a08c.tar.gz
spark-35b557af4fd1bc0ce0a5745716e898cd1300a08c.tar.bz2
spark-35b557af4fd1bc0ce0a5745716e898cd1300a08c.zip
Improve unit tests
Diffstat (limited to 'spark-common/src/test/java/me/lucko/spark/common/tick')
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/tick/TickHookTest.java69
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/tick/TickReporterTest.java94
2 files changed, 163 insertions, 0 deletions
diff --git a/spark-common/src/test/java/me/lucko/spark/common/tick/TickHookTest.java b/spark-common/src/test/java/me/lucko/spark/common/tick/TickHookTest.java
new file mode 100644
index 0000000..f920262
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/tick/TickHookTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.tick;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class TickHookTest {
+
+ @Test
+ public void testAbstractHook() {
+ AbstractTickHook hook = new AbstractTickHook() {
+ @Override
+ public void start() {
+
+ }
+
+ @Override
+ public void close() {
+
+ }
+ };
+
+ assertEquals(0, hook.getCurrentTick());
+
+ List<Integer> ticks = new ArrayList<>();
+ TickHook.Callback callback = ticks::add;
+
+ hook.addCallback(callback);
+
+ hook.onTick();
+ assertEquals(1, hook.getCurrentTick());
+ assertEquals(ImmutableList.of(0), ticks);
+
+ hook.onTick();
+ assertEquals(2, hook.getCurrentTick());
+ assertEquals(ImmutableList.of(0, 1), ticks);
+
+ hook.removeCallback(callback);
+
+ hook.onTick();
+ assertEquals(3, hook.getCurrentTick());
+ assertEquals(ImmutableList.of(0, 1), ticks);
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/tick/TickReporterTest.java b/spark-common/src/test/java/me/lucko/spark/common/tick/TickReporterTest.java
new file mode 100644
index 0000000..f7402c5
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/tick/TickReporterTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.tick;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class TickReporterTest {
+
+ @Test
+ public void testAbstractReporter() {
+ AbstractTickReporter reporter = new AbstractTickReporter() {
+ @Override
+ public void start() {
+
+ }
+
+ @Override
+ public void close() {
+
+ }
+ };
+
+ List<Double> durations = new ArrayList<>();
+ TickReporter.Callback callback = durations::add;
+
+ reporter.addCallback(callback);
+
+ reporter.onTick(1.0);
+ assertEquals(ImmutableList.of(1.0), durations);
+
+ reporter.onTick(2.0);
+ assertEquals(ImmutableList.of(1.0, 2.0), durations);
+
+ reporter.removeCallback(callback);
+
+ reporter.onTick(3.0);
+ assertEquals(ImmutableList.of(1.0, 2.0), durations);
+ }
+
+ @Test
+ public void testSimpleReporter() {
+ SimpleTickReporter reporter = new SimpleTickReporter() {
+ @Override
+ public void start() {
+
+ }
+ };
+
+ List<Double> durations = new ArrayList<>();
+ TickReporter.Callback callback = durations::add;
+
+ reporter.addCallback(callback);
+
+ reporter.onStart();
+ assertEquals(0, durations.size());
+
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+
+ reporter.onEnd();
+
+ assertEquals(1, durations.size());
+ assertTrue(durations.get(0) > 0);
+ }
+
+}