Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand All @@ -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<BaseAppWindow> testWindowsForCheck = mockTestBaseAppWindows(getTestBoundWindowsForCheck(TEST_ACCOUNT_TO_LAUNCH_1, TEST_ACCOUNT_TO_LAUNCH_2));
final LaunchAccountAction instance = new LaunchAccountAction(testAccountsToLaunch, windowFactory, settings);
Expand All @@ -73,18 +78,37 @@ 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<BaseAppWindow> 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 {
System.out.println("closeUnboundWindowsAndLaunchNewAccounts_launcherNotFound");
final List<BaseAppWindow> testBoundWindowsForCheck = getTestBoundWindowsForCheck(TEST_ACCOUNT_TO_LAUNCH_1);
final List<BaseAppWindow> testUnboundWindowsForClose = getTestUnboundWindowsForClose(TEST_ACCOUNT_FOR_CLOSE, TEST_ACCOUNT_NOT_BOUND_BEFORE);
final List<BaseAppWindow> 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()) {
Expand Down Expand Up @@ -122,7 +146,7 @@ void closeUnnecessaryWindowsAndLaunchNewAccounts() throws InterruptedException {
System.out.println("closeUnnecessaryWindowsAndLaunchNewAccounts");
final Account[] testAccountsToLaunch = getTestAccountsToLaunch();
final List<BaseAppWindow> 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();
Expand Down Expand Up @@ -175,14 +199,14 @@ private List<BaseAppWindow> 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<RestoredBaseAppWindow, InterruptedException> consumer = (ConsumerThrowing<RestoredBaseAppWindow, InterruptedException>) args[0];
Expand All @@ -195,15 +219,15 @@ private BaseAppWindow getBaseAppWindowThatMustBeClosed(final String characterNam
}

private List<BaseAppWindow> mockTestBaseAppWindows(final List<BaseAppWindow> 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<RestoredBaseAppWindow, InterruptedException> consumer = invocation.getArgument(0);
final RestoredBaseAppWindow restoredBaseAppWindowMock = Mockito.mock(RestoredBaseAppWindow.class);
Expand All @@ -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 {
Expand All @@ -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.<ConsumerThrowing<RestoredLauncherWindow, InterruptedException>>getArgument(0).accept(restoredLauncherWindowMock);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ public interface ApplicationSettings {
int stampDeviation();

int actionsCount();

boolean checkHealthBeforeLaunch();
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</modules>

<properties>
<revision>2.0.1-rc.0.2.0</revision>
<revision>2.0.1-check-health-before-launch.0.1.0</revision>
<app.build.properties.filename>app-build.properties</app.build.properties.filename>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>16</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}