Skip to content

Commit b8cfbc8

Browse files
authored
Merge pull request #127 from BenCodez/test/delayed-entity-contract
Test delayed entity dispatch through the current Spigot backend
2 parents e914bc0 + de7ae64 commit b8cfbc8

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.bencodez.simpleapi.tests.scheduler;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.mockito.ArgumentMatchers.*;
5+
import static org.mockito.Mockito.*;
6+
7+
import java.util.ArrayDeque;
8+
import java.util.Queue;
9+
import java.util.function.Consumer;
10+
11+
import org.bukkit.Server;
12+
import org.bukkit.entity.Entity;
13+
import org.bukkit.plugin.java.JavaPlugin;
14+
import org.bukkit.scheduler.BukkitTask;
15+
import org.junit.jupiter.api.Test;
16+
17+
import com.bencodez.simpleapi.scheduler.BukkitScheduler;
18+
import com.tcoded.folialib.FoliaLib;
19+
import com.tcoded.folialib.impl.SpigotImplementation;
20+
import com.tcoded.folialib.util.FoliaLibOptions;
21+
22+
class DelayedEntityDispatchContractTest {
23+
@Test
24+
@SuppressWarnings("unchecked")
25+
void validEntityQueuesOnceAndRunsAfterDelay() {
26+
JavaPlugin plugin = mock(JavaPlugin.class);
27+
Server server = mock(Server.class);
28+
org.bukkit.scheduler.BukkitScheduler nativeScheduler = mock(org.bukkit.scheduler.BukkitScheduler.class);
29+
when(plugin.getServer()).thenReturn(server);
30+
when(server.getScheduler()).thenReturn(nativeScheduler);
31+
32+
FoliaLib library = mock(FoliaLib.class);
33+
FoliaLibOptions options = mock(FoliaLibOptions.class);
34+
when(options.useIsValidOnNonFolia()).thenReturn(true);
35+
when(library.getOptions()).thenReturn(options);
36+
when(library.getPlugin()).thenReturn(plugin);
37+
// Exercise the actual current backend, including its overloads and seconds conversion.
38+
SpigotImplementation backend = new SpigotImplementation(library);
39+
when(library.getScheduler()).thenReturn(backend);
40+
BukkitScheduler facade = mock(BukkitScheduler.class, CALLS_REAL_METHODS);
41+
when(facade.getFoliaLib()).thenReturn(library);
42+
Entity entity = mock(Entity.class);
43+
when(entity.isValid()).thenReturn(true);
44+
Runnable action = mock(Runnable.class);
45+
46+
Queue<Consumer<BukkitTask>> callbacks = new ArrayDeque<>();
47+
doAnswer(invocation -> {
48+
callbacks.add((Consumer<BukkitTask>) invocation.getArgument(1));
49+
return null;
50+
}).when(nativeScheduler).runTaskLater(eq(plugin), any(Consumer.class), anyLong());
51+
52+
facade.runTaskLater(plugin, action, 2, entity);
53+
54+
verify(nativeScheduler, times(1)).runTaskLater(eq(plugin), any(Consumer.class), eq(40L));
55+
verifyNoInteractions(action);
56+
assertEquals(1, callbacks.size());
57+
callbacks.remove().accept(mock(BukkitTask.class));
58+
verify(action, times(1)).run();
59+
assertTrue(callbacks.isEmpty());
60+
verifyNoMoreInteractions(nativeScheduler, action);
61+
}
62+
}

0 commit comments

Comments
 (0)