1

I am using Esper 8.8. The following code is used to initialize SQL and register all events -

    EPCompiler compiler = EPCompilerProvider.getCompiler();
    Configuration configuration = new Configuration();
    configuration.getCommon().addEventType(Employee.class);
    configuration.getCompiler().getByteCode().setAllowSubscriber(true);

    CompilerArguments arguments = new CompilerArguments(configuration);
    EPCompiled epCompiled = compiler.compile("@public create table EmployeeTable(empId string primary key, age integer);", arguments);


    EPRuntime runtime = EPRuntimeProvider.getDefaultRuntime(configuration);
    arguments.getPath().add(runtime.getRuntimePath());
    arguments.getPath().add(epCompiled);

    EPDeploymentService deploymentService = runtime.getDeploymentService();
    EPDeployment deployment = deploymentService.deploy(epCompiled);
    EPEventService eventService = runtime.getEventService();

When test cases run, each test executes the above code and reports the following error. The above code is present in a Spring Bean and initialized once in the application. However, test cases are initializing bean with each test and Engine should not remember any table creation -

Caused by: com.espertech.esper.runtime.client.EPDeployPreconditionException: A precondition is not satisfied: A table by name 'EmployeeTable' has already been created for module '(unnamed)' at com.espertech.esper.runtime.internal.kernel.service.DeployerHelperUpdatePath.updatePath(DeployerHelperUpdatePath.java:67) at com.espertech.esper.runtime.internal.kernel.service.Deployer.deploySafe(Deployer.java:97) at com.espertech.esper.runtime.internal.kernel.service.Deployer.deploy(Deployer.java:60) at com.espertech.esper.runtime.internal.kernel.service.Deployer.deployFresh(Deployer.java:48) at com.espertech.esper.runtime.internal.kernel.service.EPDeploymentServiceImpl.deploy(EPDeploymentServiceImpl.java:119) at com.espertech.esper.runtime.internal.kernel.service.EPDeploymentServiceImpl.deploy(EPDeploymentServiceImpl.java:94) at com.rbccm.gdm.aps.algo.esper.EsperEngine.init(EsperEngine.java:91) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566)

Is there a way to clear Esper Engine in version 8.8.0. I found in Esper version 0.6.1 however these classes no more available in version 8.8.0 http://esper.espertech.com/release-6.0.1/esper-reference/html/api.html#api-engine-instances -

// Obtain an engine instance
EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider(config);

// Optionally, use initialize if the same engine instance has been used before to start clean
epService.initialize();
Sushil
  • 327
  • 1
  • 3
  • 20

2 Answers2

1

https://www.espertech.com/2018/10/23/esper-8-migrating-api-uses-from-older-versions/ provide the solution. Need to call this -

runtime.initialize();

Sushil
  • 327
  • 1
  • 3
  • 20
  • Thanks for the question & answer! I was inspired to try this approach (instead of using the context approach posted below) and found that a context is *substantially* faster then creating and initializing, compiling, and deploying each test. – Dakotah North Sep 21 '22 at 23:07
  • Documentation for `initialize` is also captured in [Obtaining a Runtime From EPRuntimeProvider] http://esper.espertech.com/release-8.8.0/reference-esper/html/apiruntime.html#apiruntime-provider – Dakotah North Sep 25 '22 at 15:53
1

A substantially faster alternative to run unit test suite with Esper is to use a context.

Set up the Esper engine statically within the test class or in the initAll and use a context. For example

create context SingleDayContext start @now end EndOfDayEvent;

and then send EndOfDayEvent events after each test to clear out all the events between each test

@AfterEach
void tearDown() {
    epEventService.sendEvent(new EndOfDayEvent());

}

Destroy the runtime in @AfterAll

@AfterAll
static void tearDownAll() {
    runtime.destroy();

}

to properly isolate the runtime between each test suite defined as part of each class.

Dakotah North
  • 1,324
  • 12
  • 29
  • 1
    Thanks for your answer. Unfortunately, I can not initialize EsperEngine in my test case. However, I tried and the solution is good. – Sushil Sep 26 '22 at 20:39