use of io.aklivity.zilla.runtime.engine.Engine in project zilla by aklivity.
the class ZillaStartCommand method run.
@Override
public void run() {
Runtime runtime = getRuntime();
Properties props = new Properties();
props.setProperty(ENGINE_DIRECTORY.name(), ".zilla/engine");
Path path = Paths.get(properties != null ? properties : OPTION_PROPERTIES_DEFAULT);
if (Files.exists(path) || properties != null) {
try {
props.load(Files.newInputStream(path));
} catch (IOException ex) {
System.out.println("Failed to load properties: " + path);
rethrowUnchecked(ex);
}
}
if (workers >= 0) {
props.setProperty(ENGINE_WORKERS.name(), Integer.toString(workers));
}
if (verbose) {
props.setProperty(ENGINE_VERBOSE.name(), Boolean.toString(verbose));
}
EngineConfiguration config = new EngineConfiguration(props);
Consumer<Throwable> report = exceptions ? e -> e.printStackTrace(System.err) : e -> System.err.println(e.getMessage());
try (Engine engine = Engine.builder().config(config).configURL(configURL.toURL()).errorHandler(this::onError).build()) {
engine.start().get();
System.out.println("started");
runtime.addShutdownHook(new Thread(this::onShutdown));
stop.await();
errors.forEach(report);
System.out.println("stopped");
stopped.countDown();
} catch (Throwable ex) {
System.out.println("error");
rethrowUnchecked(ex);
}
}
use of io.aklivity.zilla.runtime.engine.Engine in project zilla by aklivity.
the class EngineRule method apply.
@Override
public Statement apply(Statement base, Description description) {
Class<?> testClass = description.getTestClass();
final String testMethod = description.getMethodName().replaceAll("\\[.*\\]", "");
try {
Configure[] configures = testClass.getDeclaredMethod(testMethod).getAnnotationsByType(Configure.class);
Arrays.stream(configures).forEach(p -> properties.setProperty(p.name(), p.value()));
Configuration config = description.getAnnotation(Configuration.class);
if (config != null) {
if (configurationRoot != null) {
String resourceName = String.format("%s/%s", configurationRoot, config.value());
configURL = testClass.getClassLoader().getResource(resourceName);
} else {
String resourceName = String.format("%s-%s", testClass.getSimpleName(), config.value());
configURL = testClass.getResource(resourceName);
}
}
cleanup();
} catch (Exception e) {
LangUtil.rethrowUnchecked(e);
}
return new Statement() {
@Override
public void evaluate() throws Throwable {
EngineConfiguration config = configuration();
final Thread baseThread = Thread.currentThread();
final List<Throwable> errors = new ArrayList<>();
final ErrorHandler errorHandler = ex -> {
errors.add(ex);
baseThread.interrupt();
};
engine = builder.config(config).configURL(configURL).errorHandler(errorHandler).build();
try {
engine.start().get();
base.evaluate();
} catch (Throwable t) {
errors.add(t);
} finally {
try {
engine.close();
} catch (Throwable t) {
errors.add(t);
} finally {
assertEmpty(errors);
}
}
}
};
}
use of io.aklivity.zilla.runtime.engine.Engine in project zilla by aklivity.
the class EngineTest method shouldConfigure.
@Test
public void shouldConfigure() throws Exception {
String resource = String.format("%s-%s.json", getClass().getSimpleName(), "configure");
URL configURL = getClass().getResource(resource);
List<Throwable> errors = new LinkedList<>();
try (Engine engine = Engine.builder().config(config).configURL(configURL).errorHandler(errors::add).build()) {
engine.start().get();
EngineStats stats = engine.stats("default", "test0");
assertEquals(0L, stats.initialOpens());
assertEquals(0L, stats.initialCloses());
assertEquals(0L, stats.initialErrors());
assertEquals(0L, stats.initialBytes());
assertEquals(0L, stats.replyOpens());
assertEquals(0L, stats.replyCloses());
assertEquals(0L, stats.replyErrors());
assertEquals(0L, stats.replyBytes());
} catch (Throwable ex) {
errors.add(ex);
} finally {
assertThat(errors, empty());
}
}
use of io.aklivity.zilla.runtime.engine.Engine in project zilla by aklivity.
the class EngineTest method shouldNotConfigureDuplicateKey.
@Test
public void shouldNotConfigureDuplicateKey() throws Exception {
String resource = String.format("%s-%s.json.broken", getClass().getSimpleName(), "duplicate-key");
URL configURL = getClass().getResource(resource);
List<Throwable> errors = new LinkedList<>();
try (Engine engine = Engine.builder().config(config).configURL(configURL).errorHandler(errors::add).build()) {
engine.start().get();
} catch (Throwable ex) {
errors.add(ex);
} finally {
assertThat(errors, not(empty()));
}
}
Aggregations