#937 Add option for AuthMe to run in sync

- Create BukkitService#runTaskOptionallyAsync and BukkitService#scheduleSyncTaskFromOptionallyAsyncTask whose behavior depends on a new setting
- Use the new methods where applicable
- Declare events async or sync depending on the new setting
This commit is contained in:
ljacqu
2016-09-18 21:58:14 +02:00
parent ff9f50f63f
commit 4eab258993
20 changed files with 169 additions and 172 deletions
@@ -87,6 +87,20 @@ public final class TestHelper {
runnable.run();
}
/**
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#runTaskOptionallyAsync} method.
* Note that calling this method expects that there be a runnable sent to the method and will fail
* otherwise.
*
* @param service The mock service
*/
public static void runOptionallyAsyncTask(BukkitService service) {
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
verify(service).runTaskOptionallyAsync(captor.capture());
Runnable runnable = captor.getValue();
runnable.run();
}
/**
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#scheduleSyncDelayedTask(Runnable)}
* method. Note that calling this method expects that there be a runnable sent to the method and will fail
@@ -115,6 +129,20 @@ public final class TestHelper {
runnable.run();
}
/**
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#scheduleSyncTaskFromOptionallyAsyncTask}
* method. Note that calling this method expects that there be a runnable sent to the method and will fail
* otherwise.
*
* @param service The mock service
*/
public static void runSyncTaskFromOptionallyAsyncTask(BukkitService service) {
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
verify(service).scheduleSyncTaskFromOptionallyAsyncTask(captor.capture());
Runnable runnable = captor.getValue();
runnable.run();
}
/**
* Assign the necessary fields on ConsoleLogger with mocks.
*
@@ -21,7 +21,7 @@ import org.mockito.runners.MockitoJUnitRunner;
import java.util.Arrays;
import static fr.xephi.authme.TestHelper.runInnerRunnable;
import static fr.xephi.authme.TestHelper.runOptionallyAsyncTask;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
@@ -89,7 +89,7 @@ public class ChangePasswordAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(player, password));
runInnerRunnable(bukkitService);
runOptionallyAsyncTask(bukkitService);
// then
verify(service).send(sender, MessageKey.UNKNOWN_USER);
@@ -114,7 +114,7 @@ public class ChangePasswordAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(player, password));
runInnerRunnable(bukkitService);
runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validatePassword(password, player);
@@ -142,7 +142,7 @@ public class ChangePasswordAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(player, password));
runInnerRunnable(bukkitService);
runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validatePassword(password, player);
@@ -169,7 +169,7 @@ public class ChangePasswordAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(player, password));
runInnerRunnable(bukkitService);
runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validatePassword(password, player);
@@ -23,6 +23,7 @@ import org.mockito.runners.MockitoJUnitRunner;
import java.util.Arrays;
import static fr.xephi.authme.TestHelper.runSyncTaskFromOptionallyAsyncTask;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
@@ -92,7 +93,7 @@ public class RegisterAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(user, password));
TestHelper.runInnerRunnable(bukkitService);
TestHelper.runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validatePassword(password, user);
@@ -114,7 +115,7 @@ public class RegisterAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(user, password));
TestHelper.runInnerRunnable(bukkitService);
TestHelper.runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validatePassword(password, user);
@@ -139,7 +140,7 @@ public class RegisterAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(user, password));
TestHelper.runInnerRunnable(bukkitService);
TestHelper.runOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validatePassword(password, user);
@@ -168,8 +169,8 @@ public class RegisterAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(user, password));
TestHelper.runInnerRunnable(bukkitService);
runSyncDelayedTask(bukkitService);
TestHelper.runOptionallyAsyncTask(bukkitService);
runSyncTaskFromOptionallyAsyncTask(bukkitService);
// then
verify(validationService).validatePassword(password, user);
@@ -186,11 +187,4 @@ public class RegisterAdminCommandTest {
assertThat(auth.getNickname(), equalTo(name.toLowerCase()));
assertThat(auth.getPassword(), equalTo(hashedPassword));
}
private static void runSyncDelayedTask(BukkitService bukkitService) {
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
verify(bukkitService).scheduleSyncDelayedTask(captor.capture());
Runnable runnable = captor.getValue();
runnable.run();
}
}
@@ -15,7 +15,7 @@ import org.mockito.runners.MockitoJUnitRunner;
import java.util.Arrays;
import static fr.xephi.authme.TestHelper.runInnerRunnable;
import static fr.xephi.authme.TestHelper.runOptionallyAsyncTask;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
@@ -73,7 +73,7 @@ public class SetEmailCommandTest {
// when
command.executeCommand(sender, Arrays.asList(user, email));
runInnerRunnable(bukkitService);
runOptionallyAsyncTask(bukkitService);
// then
verify(commandService).validateEmail(email);
@@ -95,7 +95,7 @@ public class SetEmailCommandTest {
// when
command.executeCommand(sender, Arrays.asList(user, email));
runInnerRunnable(bukkitService);
runOptionallyAsyncTask(bukkitService);
// then
verify(commandService).validateEmail(email);
@@ -120,7 +120,7 @@ public class SetEmailCommandTest {
// when
command.executeCommand(sender, Arrays.asList(user, email));
runInnerRunnable(bukkitService);
runOptionallyAsyncTask(bukkitService);
// then
verify(commandService).validateEmail(email);
@@ -146,7 +146,7 @@ public class SetEmailCommandTest {
// when
command.executeCommand(sender, Arrays.asList(user, email));
runInnerRunnable(bukkitService);
runOptionallyAsyncTask(bukkitService);
// then
verify(commandService).validateEmail(email);
@@ -173,7 +173,7 @@ public class SetEmailCommandTest {
// when
command.executeCommand(sender, Arrays.asList(user, email));
runInnerRunnable(bukkitService);
runOptionallyAsyncTask(bukkitService);
// then
verify(commandService).validateEmail(email);
@@ -2,6 +2,8 @@ package fr.xephi.authme.util;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -13,6 +15,7 @@ import java.util.Collection;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
@@ -23,6 +26,8 @@ public class BukkitServiceTest {
@Mock
private AuthMe authMe;
@Mock
private Settings settings;
/**
* Checks that {@link BukkitService#getOnlinePlayersIsCollection} is initialized to {@code true} on startup;
@@ -31,7 +36,8 @@ public class BukkitServiceTest {
@Test
public void shouldHavePlayerListAsCollectionMethod() {
// given
BukkitService bukkitService = new BukkitService(authMe);
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
BukkitService bukkitService = new BukkitService(authMe, settings);
// when
boolean doesMethodReturnCollection = ReflectionTestUtils
@@ -44,7 +50,8 @@ public class BukkitServiceTest {
@Test
public void shouldRetrieveListOfOnlinePlayersFromReflectedMethod() {
// given
BukkitService bukkitService = new BukkitService(authMe);
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
BukkitService bukkitService = new BukkitService(authMe, settings);
ReflectionTestUtils.setField(BukkitService.class, bukkitService, "getOnlinePlayersIsCollection", false);
ReflectionTestUtils.setField(BukkitService.class, bukkitService, "getOnlinePlayers",
ReflectionTestUtils.getMethod(BukkitServiceTest.class, "onlinePlayersImpl"));