简体   繁体   中英

Esper Engine : Same table created again when running JUNIT test cases

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 Z4D236D9A2D102C5FE6 AD1C50DA4BEC50Z.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 Z93F725A07423FE1C889F448B33D21F 46Z.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();

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

runtime.initialize();

Need to call runtime.destroy() after each test.

From Obtaining a Runtime From EPRuntimeProvider

// Obtain a runtime
EPRuntime runtime = EPRuntimeProvider.getDefaultRuntime(config);

// Optionally, use initialize if the same runtime 
// has been used before to start clean
runtime.initialize();

// Destroy the runtime when no longer needed, 
// frees up resources, releases the runtime URI
runtime.destroy();

An alternative approach is to use a context.

create context SingleDayContext start @now end EndOfDayEvent;

and send an EndOfDayEvent which clears out all the events between tests.

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

}

And then at the end of the test run, destroy the tests in @AfterAll

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM