Skip to content

Commit

Permalink
run async scripts in a sync way in integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Feb 25, 2020
1 parent d97d04e commit 35f3a98
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
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
9 changes: 7 additions & 2 deletions src/test/java/alfio/TestConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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 +41,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 +115,9 @@ public DownloadedFile downloadFile(String url) {
}
};
}

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

0 comments on commit 35f3a98

Please sign in to comment.