Best MockBukkit code snippet using be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock.waitAsyncEventsFinished
Source:BukkitSchedulerMock.java
...68 waitAsyncTasksFinished();69 shutdownPool(pool);70 if (asyncException.get() != null)71 throw new AsyncTaskException(asyncException.get());72 waitAsyncEventsFinished();73 shutdownPool(asyncEventExecutor);74 }75 /**76 * Shuts down the given executor service, waiting up to the shutdown timeout for all tasks to finish.77 *78 * @param pool The pool to shut down.79 * @see #setShutdownTimeout(long)80 */81 private void shutdownPool(@NotNull ExecutorService pool)82 {83 pool.shutdown();84 try85 {86 if (!pool.awaitTermination(this.executorTimeout, TimeUnit.MILLISECONDS))87 {88 pool.shutdownNow();89 }90 }91 catch (InterruptedException e)92 {93 pool.shutdownNow();94 Thread.currentThread().interrupt();95 }96 }97 public @NotNull Future<?> executeAsyncEvent(@NotNull Event event)98 {99 return executeAsyncEvent(event, null);100 }101 public <T extends Event> @NotNull Future<?> executeAsyncEvent(@NotNull T event, @Nullable Consumer<T> func)102 {103 MockBukkit.ensureMocking();104 Preconditions.checkNotNull(event, "Cannot call a null event!");105 Future<?> future = asyncEventExecutor.submit(() ->106 {107 MockBukkit.getMock().getPluginManager().callEvent(event);108 if (func != null)109 {110 func.accept(event);111 }112 });113 queuedAsyncEvents.add(future);114 return future;115 }116 /**117 * Get the current tick of the server.118 *119 * @return The current tick of the server.120 */121 public long getCurrentTick()122 {123 return currentTick;124 }125 /**126 * Perform one tick on the server.127 */128 public void performOneTick()129 {130 currentTick++;131 List<ScheduledTask> oldTasks = scheduledTasks.getCurrentTaskList();132 for (ScheduledTask task : oldTasks)133 {134 if (task.getScheduledTick() == currentTick && !task.isCancelled())135 {136 if (task.isSync())137 {138 wrapTask(task).run();139 }140 else141 {142 pool.submit(wrapTask(task));143 }144 if (task instanceof RepeatingTask)145 {146 if (!task.isCancelled())147 {148 ((RepeatingTask) task).updateScheduledTick();149 scheduledTasks.addTask(task);150 }151 }152 else153 {154 task.cancel();155 }156 }157 }158 }159 /**160 * Perform a number of ticks on the server.161 *162 * @param ticks The number of ticks to executed.163 */164 public void performTicks(long ticks)165 {166 for (long i = 0; i < ticks; i++)167 {168 performOneTick();169 }170 }171 /**172 * Gets the number of async tasks which are awaiting execution.173 *174 * @return The number of async tasks which are pending execution.175 */176 public int getNumberOfQueuedAsyncTasks()177 {178 int queuedAsync = 0;179 for (ScheduledTask task : scheduledTasks.getCurrentTaskList())180 {181 if (task.isSync() || task.isCancelled() || task.isRunning())182 {183 continue;184 }185 queuedAsync++;186 }187 return queuedAsync;188 }189 /**190 * Waits until all asynchronous tasks have finished executing. If you have an asynchronous task that runs191 * indefinitely, this function will never return. Note that this will not wait for async events to finish.192 */193 public void waitAsyncTasksFinished()194 {195 // Cancel repeating tasks so they don't run forever.196 scheduledTasks.tasks.entrySet().stream()197 .filter(entry -> entry.getValue() instanceof RepeatingTask)198 .forEach(entry -> scheduledTasks.cancelTask(entry.getKey()));199 // Make sure all tasks get to execute. (except for repeating asynchronous tasks, they only will fire once)200 while (scheduledTasks.getScheduledTaskCount() > 0)201 {202 performOneTick();203 }204 // Wait for all tasks to finish executing.205 long systemTime = System.currentTimeMillis();206 while (pool.getActiveCount() > 0)207 {208 try209 {210 Thread.sleep(10L);211 }212 catch (InterruptedException e)213 {214 Thread.currentThread().interrupt();215 return;216 }217 if (System.currentTimeMillis() <= (systemTime + executorTimeout))218 continue;219 // If a plugin has left a runnable going and not cancelled it we could call this bad practice.220 // We should force interrupt all these runnables, forcing them to throw Interrupted Exceptions221 // if they handle that.222 for (ScheduledTask task : scheduledTasks.getCurrentTaskList())223 {224 if (!task.isRunning())225 continue;226 task.cancel();227 cancelTask(task.getTaskId());228 throw new RuntimeException("Forced Cancellation of task owned by " + task.getOwner().getName());229 }230 pool.shutdownNow();231 }232 }233 public void waitAsyncEventsFinished()234 {235 for (Future<?> futureEvent : List.copyOf(queuedAsyncEvents))236 {237 if (futureEvent.isDone())238 {239 queuedAsyncEvents.remove(futureEvent);240 }241 else242 {243 try244 {245 queuedAsyncEvents.remove(futureEvent);246 futureEvent.get();247 }...
Source:BukkitSchedulerMockTest.java
...284 scheduler.setShutdownTimeout(10);285 assertThrows(RuntimeException.class, () -> scheduler.shutdown());286 }287 @Test288 void waitAsyncEventsFinished()289 {290 MockBukkit.mock();291 AtomicBoolean done = new AtomicBoolean(false);292 Bukkit.getPluginManager().registerEvents(new Listener()293 {294 @EventHandler295 public void onChat(AsyncChatEvent event) throws Exception296 {297 Thread.sleep(50);298 done.set(true);299 }300 }, MockBukkit.createMockPlugin());301 AsyncChatEvent event = new AsyncChatEvent(true, null, null, null, null, null);302 scheduler.executeAsyncEvent(event);303 assertFalse(done.get());304 scheduler.waitAsyncEventsFinished();305 assertTrue(done.get());306 MockBukkit.unmock();307 }308 @Test309 void shutdown_waitsForAsyncEvents()310 {311 MockBukkit.mock();312 AtomicBoolean done = new AtomicBoolean(false);313 Bukkit.getPluginManager().registerEvents(new Listener()314 {315 @EventHandler316 public void onChat(AsyncChatEvent event) throws Exception317 {318 Thread.sleep(SLEEP_TIME);...
waitAsyncEventsFinished
Using AI Code Generation
1BukkitSchedulerMock scheduler = (BukkitSchedulerMock)Bukkit.getScheduler();2scheduler.waitAsyncEventsFinished();3BukkitSchedulerMock scheduler = (BukkitSchedulerMock)Bukkit.getScheduler();4scheduler.waitAsyncEventsFinished(1000);5BukkitSchedulerMock scheduler = (BukkitSchedulerMock)Bukkit.getScheduler();6scheduler.waitAsyncEventsFinished(1000, 100);7BukkitSchedulerMock scheduler = (BukkitSchedulerMock)Bukkit.getScheduler();8scheduler.waitAsyncEventsFinished(1000, 100, 10);9BukkitSchedulerMock scheduler = (BukkitSchedulerMock)Bukkit.getScheduler();10scheduler.waitAsyncEventsFinished(1000, 100, 10, 1000);11BukkitSchedulerMock scheduler = (BukkitSchedulerMock)Bukkit.getScheduler();12scheduler.waitAsyncEventsFinished(1000, 100, 10, 1000, 1000);13BukkitSchedulerMock scheduler = (BukkitSchedulerMock)Bukkit.getScheduler();14scheduler.waitAsyncEventsFinished(1000, 100, 10, 1000, 1000, 1000);15BukkitSchedulerMock scheduler = (BukkitSchedulerMock)Bukkit.getScheduler();16scheduler.waitAsyncEventsFinished(1000, 100, 10, 1000,
waitAsyncEventsFinished
Using AI Code Generation
1import org.junit.jupiter.api.Test;2import org.junit.jupiter.api.AfterEach;3import org.junit.jupiter.api.BeforeEach;4import org.junit.jupiter.api.DisplayName;5import org.junit.jupiter.api.RepeatedTest;6import org.junit.jupiter.api.RepetitionInfo;7import org.junit.jupiter.api.extension.ExtendWith;8import org.junit.jupiter.api.Assertions;9import org.junit.jupiter.api.function.Executable;10import be.seeseemelk.mockbukkit.MockBukkit;11import be.seeseemelk.mockbukkit.ServerMock;12import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock;13import be.seeseemelk.mockbukkit.scheduler.BukkitTaskMock;14import java.util.ArrayList;15import java.util.List;16import org.bukkit.Bukkit;17import org.bukkit.scheduler.BukkitTask;18public class TestAsyncTasks {19 private ServerMock server;20 private BukkitSchedulerMock scheduler;21 private List<BukkitTaskMock> tasks;22 public void setUp() {23 server = MockBukkit.mock();24 scheduler = server.getScheduler();25 tasks = new ArrayList<BukkitTaskMock>();26 }27 public void tearDown() {28 MockBukkit.unmock();29 }30 @DisplayName("Test waitAsyncEventsFinished")31 public void testWaitAsyncEventsFinished() {32 int numTasks = 10;33 for(int i = 0; i < numTasks; i++) {34 tasks.add(scheduler.runTaskAsynchronously(() -> {35 try {36 Thread.sleep(500);37 } catch (InterruptedException e) {38 e.printStackTrace();39 }40 }));41 }42 scheduler.waitAsyncEventsFinished();43 Assertions.assertEquals(numTasks, scheduler.getPendingTasks().size());44 for(BukkitTask task : scheduler.getPendingTasks()) {45 task.cancel();46 }47 scheduler.waitAsyncEventsFinished();48 Assertions.assertEquals(0, scheduler.getPendingTasks().size());49 }50}
waitAsyncEventsFinished
Using AI Code Generation
1import org.junit.Test;2import org.junit.Before;3import org.junit.After;4import static org.junit.Assert.assertEquals;5import static org.junit.Assert.assertFalse;6import static org.junit.Assert.assertTrue;7import static org.junit.Assert.assertNotNull;8import static org.junit.Assert.assertNull;9import be.seeseemelk.mockbukkit.MockBukkit;10import be.seeseemelk.mockbukkit.ServerMock;11import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock;12import be.seeseemelk.mockbukkit.scheduler.BukkitTaskMock;13{14 private ServerMock server;15 private BukkitSchedulerMock scheduler;16 public void setup()17 {18 server = MockBukkit.mock();19 scheduler = server.getScheduler();20 }21 public void tearDown()22 {23 MockBukkit.unmock();24 }25 public void testWaitAsyncEventsFinished()26 {27 BukkitTaskMock task = scheduler.runTaskAsynchronously(null, () -> {28 try {29 Thread.sleep(1000);30 } catch (InterruptedException e) {31 e.printStackTrace();32 }33 });34 scheduler.waitAsyncEventsFinished();35 assertFalse(task.isSync());36 assertTrue(task.isCancelled());37 assertTrue(task.isDone());38 }39}
waitAsyncEventsFinished
Using AI Code Generation
1import org.junit.Test;2import org.junit.runner.RunWith;3import org.bukkit.event.player.PlayerJoinEvent;4import be.seeseemelk.mockbukkit.MockBukkit;5import be.seeseemelk.mockbukkit.ServerMock;6import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock;7import be.seeseemelk.mockbukkit.scheduler.BukkitTaskMock;8import static org.junit.Assert.*;9{10 public void test()11 {12 ServerMock server = MockBukkit.mock();13 BukkitSchedulerMock scheduler = server.getScheduler();14 scheduler.runTaskAsynchronously(null, () -> {15 {16 Thread.sleep(5000);17 }18 catch (InterruptedException e)19 {20 e.printStackTrace();21 }22 });23 scheduler.runTaskAsynchronously(null, () -> {24 {25 Thread.sleep(1000);26 }27 catch (InterruptedException e)28 {29 e.printStackTrace();30 }31 });32 scheduler.runTaskAsynchronously(null, () -> {33 {34 Thread.sleep(2000);35 }36 catch (InterruptedException e)37 {38 e.printStackTrace();39 }40 });41 scheduler.runTaskAsynchronously(null, () -> {42 {43 Thread.sleep(3000);44 }45 catch (InterruptedException e)46 {47 e.printStackTrace();48 }49 });50 scheduler.runTaskAsynchronously(null, () -> {51 {52 Thread.sleep(4000);53 }54 catch (InterruptedException e)55 {56 e.printStackTrace();57 }58 });59 scheduler.runTaskAsynchronously(null, () -> {60 {61 Thread.sleep(5000);62 }63 catch (InterruptedException e)64 {65 e.printStackTrace();66 }67 });68 scheduler.runTaskAsynchronously(null, () -> {69 {70 Thread.sleep(6000);71 }72 catch (InterruptedException e)73 {74 e.printStackTrace();75 }76 });77 scheduler.runTaskAsynchronously(null, () -> {78 {79 Thread.sleep(7000);80 }81 catch (InterruptedException e)82 {83 e.printStackTrace();84 }85 });
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!