diff --git a/executor/src/main/java/midorum/melbone/executor/internal/processor/LaunchAccountAction.java b/executor/src/main/java/midorum/melbone/executor/internal/processor/LaunchAccountAction.java index 21d8e38..41d94d4 100644 --- a/executor/src/main/java/midorum/melbone/executor/internal/processor/LaunchAccountAction.java +++ b/executor/src/main/java/midorum/melbone/executor/internal/processor/LaunchAccountAction.java @@ -94,6 +94,7 @@ private void closeAllUnnecessaryWindowsAndCheckHealthForRest() { throw new ControlledInterruptedException(e); } }); + if(!settings.application().checkHealthBeforeLaunch()) return; windowsMap.get(false).forEach(w -> { try { final String characterName = w.getCharacterName().orElse("unbound"); diff --git a/executor/src/test/java/midorum/melbone/executor/internal/processor/LaunchAccountActionTest.java b/executor/src/test/java/midorum/melbone/executor/internal/processor/LaunchAccountActionTest.java index 6f410af..a322679 100644 --- a/executor/src/test/java/midorum/melbone/executor/internal/processor/LaunchAccountActionTest.java +++ b/executor/src/test/java/midorum/melbone/executor/internal/processor/LaunchAccountActionTest.java @@ -19,6 +19,10 @@ import java.util.*; import java.util.stream.Stream; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.when; + class LaunchAccountActionTest { public static final String TEST_ACCOUNT_FOR_CLOSE = "test_account_for_close"; @@ -42,12 +46,12 @@ public static void beforeAll() { @BeforeEach void beforeEach() { - Mockito.when(settings.application()).thenReturn(applicationSettings); - Mockito.when(settings.targetCountControl()).thenReturn(targetCountControlSettings); - Mockito.when(settings.targetBaseAppSettings()).thenReturn(targetBaseAppSettings); - Mockito.when(targetBaseAppSettings.windowAppearingLatency()).thenReturn(100); - Mockito.when(targetBaseAppSettings.windowAppearingDelay()).thenReturn(100); - Mockito.when(targetBaseAppSettings.windowAppearingTimeout()).thenReturn(1_000); + when(settings.application()).thenReturn(applicationSettings); + when(settings.targetCountControl()).thenReturn(targetCountControlSettings); + when(settings.targetBaseAppSettings()).thenReturn(targetBaseAppSettings); + when(targetBaseAppSettings.windowAppearingLatency()).thenReturn(100); + when(targetBaseAppSettings.windowAppearingDelay()).thenReturn(100); + when(targetBaseAppSettings.windowAppearingTimeout()).thenReturn(1_000); } @AfterEach @@ -57,8 +61,9 @@ public void afterEach() { @SuppressWarnings("unchecked") @Test - void allAccountsAlreadyLaunched() throws InterruptedException { - System.out.println("allAccountsAlreadyLaunched"); + void allAccountsAlreadyLaunched_checkHealthIsOn() throws InterruptedException { + System.out.println("allAccountsAlreadyLaunched_checkHealthIsOn"); + when(applicationSettings.checkHealthBeforeLaunch()).thenReturn(true); final Account[] testAccountsToLaunch = getTestAccountsToLaunch(); final List testWindowsForCheck = mockTestBaseAppWindows(getTestBoundWindowsForCheck(TEST_ACCOUNT_TO_LAUNCH_1, TEST_ACCOUNT_TO_LAUNCH_2)); final LaunchAccountAction instance = new LaunchAccountAction(testAccountsToLaunch, windowFactory, settings); @@ -73,6 +78,25 @@ void allAccountsAlreadyLaunched() throws InterruptedException { Mockito.verify(windowFactory, Mockito.never()).findOrTryStartLauncherWindow(); } + @SuppressWarnings("unchecked") + @Test + void allAccountsAlreadyLaunched_checkHealthIsOff() throws InterruptedException { + System.out.println("allAccountsAlreadyLaunched_checkHealthIsOn"); + when(applicationSettings.checkHealthBeforeLaunch()).thenReturn(false); + final Account[] testAccountsToLaunch = getTestAccountsToLaunch(); + final List testWindowsForCheck = mockTestBaseAppWindows(getTestBoundWindowsForCheck(TEST_ACCOUNT_TO_LAUNCH_1, TEST_ACCOUNT_TO_LAUNCH_2)); + final LaunchAccountAction instance = new LaunchAccountAction(testAccountsToLaunch, windowFactory, settings); + instance.perform(); + testWindowsForCheck.forEach(baseAppWindow -> { + try { + Mockito.verify(baseAppWindow, Mockito.never()).doInGameWindow(Mockito.any(ConsumerThrowing.class)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + Mockito.verify(windowFactory, Mockito.never()).findOrTryStartLauncherWindow(); + } + @Test @SuppressWarnings("unchecked") void closeUnboundWindowsAndLaunchNewAccounts_launcherNotFound() throws InterruptedException { @@ -80,11 +104,11 @@ void closeUnboundWindowsAndLaunchNewAccounts_launcherNotFound() throws Interrupt final List testBoundWindowsForCheck = getTestBoundWindowsForCheck(TEST_ACCOUNT_TO_LAUNCH_1); final List testUnboundWindowsForClose = getTestUnboundWindowsForClose(TEST_ACCOUNT_FOR_CLOSE, TEST_ACCOUNT_NOT_BOUND_BEFORE); final List existWindows = mergeLists(testBoundWindowsForCheck, testUnboundWindowsForClose); - Mockito.when(windowFactory.getAllBaseAppWindows()).thenReturn(existWindows).thenReturn(testBoundWindowsForCheck); + when(windowFactory.getAllBaseAppWindows()).thenReturn(existWindows).thenReturn(testBoundWindowsForCheck); mockLauncherWindowForNotFoundCase(); mockTargetProcessesLimitNotReached(); final LaunchAccountAction instance = new LaunchAccountAction(getTestAccountsToLaunch(), windowFactory, settings); - Assertions.assertThrows(NeedRetryException.class, instance::perform); + assertThrows(NeedRetryException.class, instance::perform); existWindows.forEach(baseAppWindow -> { try { if (baseAppWindow.getCharacterName().isEmpty()) { @@ -122,7 +146,7 @@ void closeUnnecessaryWindowsAndLaunchNewAccounts() throws InterruptedException { System.out.println("closeUnnecessaryWindowsAndLaunchNewAccounts"); final Account[] testAccountsToLaunch = getTestAccountsToLaunch(); final List testUnboundWindowsForClose = getTestUnboundWindowsForClose(TEST_ACCOUNT_TO_LAUNCH_3, TEST_ACCOUNT_TO_LAUNCH_4); - Mockito.when(windowFactory.getAllBaseAppWindows()).thenReturn(testUnboundWindowsForClose).thenReturn(List.of()); + when(windowFactory.getAllBaseAppWindows()).thenReturn(testUnboundWindowsForClose).thenReturn(List.of()); mockLauncherWindowForLoginAccounts(testAccountsToLaunch); mockTargetProcessesLimitNotReached(); mockNewUnboundBaseAppWindow(); @@ -175,14 +199,14 @@ private List getEmptyTestWindows() { private BaseAppWindow getBaseAppWindow(final String characterName) throws InterruptedException { final BaseAppWindow mock = Mockito.mock(BaseAppWindow.class); - Mockito.when(mock.getCharacterName()).thenReturn(Optional.ofNullable(characterName)); + when(mock.getCharacterName()).thenReturn(Optional.ofNullable(characterName)); return mock; } @SuppressWarnings("unchecked") private BaseAppWindow getBaseAppWindowThatMustBeClosed(final String characterName) throws InterruptedException { final BaseAppWindow mock = getBaseAppWindow(characterName); - Mockito.doAnswer(invocation -> { + doAnswer(invocation -> { final Object[] args = invocation.getArguments(); Assertions.assertEquals(1, args.length); final ConsumerThrowing consumer = (ConsumerThrowing) args[0]; @@ -195,15 +219,15 @@ private BaseAppWindow getBaseAppWindowThatMustBeClosed(final String characterNam } private List mockTestBaseAppWindows(final List testWindows) { - Mockito.when(windowFactory.getAllBaseAppWindows()).thenReturn(testWindows); + when(windowFactory.getAllBaseAppWindows()).thenReturn(testWindows); return testWindows; } @SuppressWarnings("unchecked") private void mockNewUnboundBaseAppWindow() throws InterruptedException { final BaseAppWindow mock = getBaseAppWindow(null); - Mockito.when(windowFactory.findUnboundBaseAppWindowAndBindWithAccount(Mockito.anyString())).thenReturn(Optional.of(mock)); - Mockito.doAnswer(invocation -> { + when(windowFactory.findUnboundBaseAppWindowAndBindWithAccount(Mockito.anyString())).thenReturn(Optional.of(mock)); + doAnswer(invocation -> { Assertions.assertEquals(1, invocation.getArguments().length); final ConsumerThrowing consumer = invocation.getArgument(0); final RestoredBaseAppWindow restoredBaseAppWindowMock = Mockito.mock(RestoredBaseAppWindow.class); @@ -215,7 +239,7 @@ private void mockNewUnboundBaseAppWindow() throws InterruptedException { } private void mockLauncherWindowForNotFoundCase() throws InterruptedException { - Mockito.when(windowFactory.findOrTryStartLauncherWindow()).thenReturn(Optional.empty()); + when(windowFactory.findOrTryStartLauncherWindow()).thenReturn(Optional.empty()); } private void mockLauncherWindowForLoginAccounts(final Account[] testAccountsToLaunch) throws InterruptedException { @@ -229,7 +253,7 @@ private void mockLauncherWindowForLoginAccounts(final Account[] testAccountsToLa @SuppressWarnings("unchecked") private LauncherWindow getLauncherMockForLoginAccount(final Account account) throws InterruptedException { final LauncherWindow mock = Mockito.mock(LauncherWindow.class); - Mockito.doAnswer(invocation -> { + doAnswer(invocation -> { Assertions.assertEquals(1, invocation.getArguments().length); final RestoredLauncherWindow restoredLauncherWindowMock = Mockito.mock(RestoredLauncherWindow.class); invocation.>getArgument(0).accept(restoredLauncherWindowMock); @@ -241,13 +265,13 @@ private LauncherWindow getLauncherMockForLoginAccount(final Account account) thr } private void mockTargetProcessesLimitNotReached() { - Mockito.when(applicationSettings.maxAccountsSimultaneously()).thenReturn(2); - Mockito.when(windowFactory.countAllTargetProcesses()).thenReturn(1); + when(applicationSettings.maxAccountsSimultaneously()).thenReturn(2); + when(windowFactory.countAllTargetProcesses()).thenReturn(1); } private void mockTargetProcessesLimitReached() { - Mockito.when(applicationSettings.maxAccountsSimultaneously()).thenReturn(2); - Mockito.when(windowFactory.countAllTargetProcesses()).thenReturn(2); + when(applicationSettings.maxAccountsSimultaneously()).thenReturn(2); + when(windowFactory.countAllTargetProcesses()).thenReturn(2); } private Account buildAccount(final String name) { diff --git a/model/src/main/java/midorum/melbone/model/settings/setting/ApplicationSettings.java b/model/src/main/java/midorum/melbone/model/settings/setting/ApplicationSettings.java index c6c4efe..8d73e71 100644 --- a/model/src/main/java/midorum/melbone/model/settings/setting/ApplicationSettings.java +++ b/model/src/main/java/midorum/melbone/model/settings/setting/ApplicationSettings.java @@ -40,4 +40,6 @@ public interface ApplicationSettings { int stampDeviation(); int actionsCount(); + + boolean checkHealthBeforeLaunch(); } diff --git a/pom.xml b/pom.xml index 317b2ba..fd08428 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ - 2.0.1-rc.0.2.0 + 2.0.1-check-health-before-launch.0.1.0 app-build.properties UTF-8 16 diff --git a/settings/src/main/java/midorum/melbone/settings/SettingKeys.java b/settings/src/main/java/midorum/melbone/settings/SettingKeys.java index c2be16f..5a95efb 100644 --- a/settings/src/main/java/midorum/melbone/settings/SettingKeys.java +++ b/settings/src/main/java/midorum/melbone/settings/SettingKeys.java @@ -69,7 +69,10 @@ enum Application implements SettingKey { randomRoutineDelayMax(Long.class, "0 - for disable, >0 - maximal random delay on every routine iteration in minutes", 5L, - LONG_POSITIVE_PREDICATE.predicate()); + LONG_POSITIVE_PREDICATE.predicate()), + checkHealthBeforeLaunch(Boolean.class, + "Check existing base windows health before launch new one. Use when have often disconnect problems. Disable to speed up launch.", + false); private final SettingData settingData; diff --git a/settings/src/main/java/midorum/melbone/settings/internal/obtaining/setting/ApplicationSettingsImpl.java b/settings/src/main/java/midorum/melbone/settings/internal/obtaining/setting/ApplicationSettingsImpl.java index 9b35ae8..49543fc 100644 --- a/settings/src/main/java/midorum/melbone/settings/internal/obtaining/setting/ApplicationSettingsImpl.java +++ b/settings/src/main/java/midorum/melbone/settings/internal/obtaining/setting/ApplicationSettingsImpl.java @@ -44,4 +44,9 @@ public int stampDeviation() { public int actionsCount() { return (int) getValue(SettingKeys.Application.actionsCount); } + + @Override + public boolean checkHealthBeforeLaunch() { + return (boolean) getValue(SettingKeys.Application.checkHealthBeforeLaunch); + } }