Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run async scripts in a sync way in integration tests #876

Merged
merged 2 commits into from
Feb 25, 2020
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ compileJava {
options.errorprone.disable('UnusedVariable')
options.errorprone.disable('MixedMutabilityReturnType')
options.errorprone.disable('MissingOverride')
options.errorprone.disable('JavaTimeDefaultTimeZone') // a little bit too noisy
}

compileJava.dependsOn(processResources)
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/alfio/config/DataSourceConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

@Configuration
@EnableTransactionManagement
Expand Down Expand Up @@ -138,6 +141,12 @@ public List<ParameterConverter> getAdditionalParameterConverters() {
return Arrays.asList(new JSONColumnMapper.Converter(), new ArrayColumnMapper.Converter());
}

@Bean
@Profile("!"+Initializer.PROFILE_INTEGRATION_TEST)
public Supplier<Executor> getNewSingleThreadExecutorSupplier() {
return () -> Executors.newSingleThreadExecutor();
}

@Bean
public Flyway migrator(DataSource dataSource) {
var configuration = Flyway.configure();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executors;

import static alfio.util.Wrappers.optionally;

Expand All @@ -66,7 +67,7 @@ public void migrate(Context context) throws Exception {
ExtensionRepository extensionRepository = QueryFactory.from(ExtensionRepository.class, "PGSQL", dataSource);
ExtensionLogRepository extensionLogRepository = QueryFactory.from(ExtensionLogRepository.class, "PGSQL", dataSource);
PluginRepository pluginRepository = QueryFactory.from(PluginRepository.class, "PGSQL", dataSource);
ExtensionService extensionService = new ExtensionService(new ScriptingExecutionService(HttpClient.newHttpClient()), extensionRepository, extensionLogRepository, new DataSourceTransactionManager(dataSource), new ExternalConfiguration());
ExtensionService extensionService = new ExtensionService(new ScriptingExecutionService(HttpClient.newHttpClient(), () -> Executors.newSingleThreadExecutor()), extensionRepository, extensionLogRepository, new DataSourceTransactionManager(dataSource), new ExternalConfiguration());

extensionService.createOrUpdate(null, null, new Extension("-", "mailchimp", getMailChimpScript(), true));

Expand Down
17 changes: 10 additions & 7 deletions src/main/java/alfio/extension/ScriptingExecutionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand All @@ -47,20 +48,22 @@
public class ScriptingExecutionService {

private final SimpleHttpClient simpleHttpClient;
private final Supplier<Executor> executorSupplier;

public ScriptingExecutionService(HttpClient httpClient) {
public ScriptingExecutionService(HttpClient httpClient, Supplier<Executor> executorSupplier) {
this.simpleHttpClient = new SimpleHttpClient(httpClient);
this.executorSupplier = executorSupplier;
}

private static final Compilable engine = (Compilable) new ScriptEngineManager().getEngineByName("nashorn");
private final Cache<String, CompiledScript> compiledScriptCache = Caffeine.newBuilder()
.expireAfterAccess(12, TimeUnit.HOURS)
.build();
private final Cache<String, ExecutorService> asyncExecutors = Caffeine.newBuilder()
private final Cache<String, Executor> asyncExecutors = Caffeine.newBuilder()
.expireAfterAccess(12, TimeUnit.HOURS)
.removalListener((String key, ExecutorService value, RemovalCause cause) -> {
if (value != null) {
value.shutdown();
.removalListener((String key, Executor value, RemovalCause cause) -> {
if (value != null && value instanceof ExecutorService) {
((ExecutorService) value).shutdown();
}
})
.build();
Expand All @@ -79,8 +82,8 @@ public <T> T executeScript(String name, String hash, Supplier<String> scriptFetc
}

public void executeScriptAsync(String path, String name, String hash, Supplier<String> scriptFetcher, Map<String, Object> params, ExtensionLogger extensionLogger) {
Optional.ofNullable(asyncExecutors.get(path, key -> Executors.newSingleThreadExecutor()))
.ifPresent(it -> it.submit(() -> executeScript(name, hash, scriptFetcher, params, Object.class, extensionLogger)));
Optional.ofNullable(asyncExecutors.get(path, key -> executorSupplier.get()))
.ifPresent(it -> it.execute(() -> executeScript(name, hash, scriptFetcher, params, Object.class, extensionLogger)));
}


Expand Down
11 changes: 9 additions & 2 deletions src/test/java/alfio/TestConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
package alfio;

import alfio.config.Initializer;
import alfio.config.support.PlatformProvider;
import alfio.manager.FileDownloadManager;
import alfio.test.util.IntegrationTestUtil;
import alfio.util.BaseIntegrationTest;
import com.opentable.db.postgres.embedded.EmbeddedPostgres;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
Expand All @@ -42,7 +42,8 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Properties;

import java.util.concurrent.Executor;
import java.util.function.Supplier;


@Configuration
Expand Down Expand Up @@ -115,4 +116,10 @@ public DownloadedFile downloadFile(String url) {
}
};
}

@Bean
@Profile(Initializer.PROFILE_INTEGRATION_TEST)
public Supplier<Executor> getCurrentThreadExecutorSupplier() {
return () -> (job) -> job.run();
}
}