Skip to content

Commit

Permalink
Cover the async part with Thread.sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
gicig committed Sep 10, 2024
1 parent 8927f9b commit f5dc3ca
Showing 1 changed file with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ class AcToolStartupHookServiceImplTest {
@Spy
AcInstallationService installationService;

@ParameterizedTest
@CsvSource({
"false, false, true",
"false, true, false",
"true, false, true",
"true, true, false"
})
void testActivationSync(boolean runAsync, boolean canReadApps, boolean pathsForInstallationEmpty) throws RepositoryException, InterruptedException {
setup(canReadApps);
try (MockedStatic<FrameworkUtil> mockedFrameworkUtil = mockStatic(FrameworkUtil.class)) {
createAndActivateStartupHookService(mockedFrameworkUtil, runAsync);
// wait for the thread in AcToolStartupHookServiceImpl#runAcToolAsync to get started
if (runAsync && canReadApps) {
Thread.sleep(1000L);
}
verify(installationService, times(1)).apply(null, pathsForInstallationEmpty ? new String[]{} : new String[]{ "^/$", "^$" }, true);
}
}

void setup(boolean canReadApps) throws RepositoryException {
FrameworkStartLevel startLevel = Mockito.mock(FrameworkStartLevel.class);
when(startLevel.getStartLevel()).thenReturn(0);
Expand All @@ -88,27 +107,6 @@ void setup(boolean canReadApps) throws RepositoryException {
when(bundleContext.getBundle(anyLong())).thenReturn(bundle);
}

@ParameterizedTest
@CsvSource({
"false, false, true",
"false, true, false",
"true, false, true"
// "true, true, false"
/*
last case is excluded, because it starts a thread in AcToolStartupHookServiceImpl#runAcToolAsync
so AcToolStartupHookServiceImpl#apply is invoked asynchronously and can not be checked easily in
unit test
*/

})
void testActivationSync(boolean runAsync, boolean canReadApps, boolean pathsForInstallationEmpty) throws RepositoryException {
setup(canReadApps);
try (MockedStatic<FrameworkUtil> mockedFrameworkUtil = mockStatic(FrameworkUtil.class)) {
createAndActivateStartupHookService(mockedFrameworkUtil, runAsync);
verify(installationService, times(1)).apply(null, pathsForInstallationEmpty ? new String[]{} : new String[]{ "^/$", "^$" }, true);
}
}

private void createAndActivateStartupHookService(MockedStatic<FrameworkUtil> mockedFrameworkUtil, boolean runAsyncForMutableContent) {
mockedFrameworkUtil.when(() -> FrameworkUtil.getBundle(RuntimeHelper.class)).thenReturn(bundle);
AcToolStartupHookServiceImpl startupHookService = new AcToolStartupHookServiceImpl();
Expand Down

0 comments on commit f5dc3ca

Please sign in to comment.