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

Point value cache changes #1431

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
34 changes: 33 additions & 1 deletion Core/src-test/com/serotonin/m2m2/H2InMemoryDatabaseProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package com.serotonin.m2m2;

import static org.junit.Assert.fail;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -27,6 +29,7 @@
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.PlatformTransactionManager;

import com.infiniteautomation.mango.monitor.IntegerMonitor;
import com.serotonin.ShouldNeverHappenException;
import com.serotonin.db.DaoUtils;
import com.serotonin.db.spring.ConnectionCallbackVoid;
Expand All @@ -47,6 +50,7 @@
import com.serotonin.m2m2.vo.User;
import com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory;
import com.serotonin.provider.Providers;
import com.serotonin.timer.SimulationTimer;

/**
* Using an H2 in memory database we can easily mock the database proxy.
Expand Down Expand Up @@ -363,9 +367,16 @@ public void doInConnection(ConnectionCallbackVoid callback) {
public <T> List<T> doLimitQuery(DaoUtils dao, String sql, Object[] args, RowMapper<T> rowMapper,
int limit) {
if (limit > 0)
sql += " LIMIT " + limit;
sql = getLimitQuerySql(sql, limit);
return dao.query(sql, args, rowMapper);
}

@Override
public String getLimitQuerySql(String sql, int limit) {
if (limit > 0)
return sql + " LIMIT " + limit;
return sql;
}

@Override
public long doLimitDelete(ExtendedJdbcTemplate ejt, String sql, Object[] args, int chunkSize,
Expand Down Expand Up @@ -439,10 +450,31 @@ public NoSQLProxy getNoSQLProxy() {
*/
public void clean() throws Exception {

//If the SQL Batch writer is executing we need to allow it to finish
if(initialized) {
SimulationTimer timer = Common.timer instanceof SimulationTimer ? (SimulationTimer)Common.timer : null;
IntegerMonitor m = (IntegerMonitor)Common.MONITORED_VALUES.getValueMonitor(PointValueDaoSQL.INSTANCES_MONITOR_ID);
if(m != null) {
int retries = 100;
while(retries > 0) {
if(m.getValue() == 0)
break;
retries--;
if(timer != null)
timer.fastForwardTo(timer.currentTimeMillis() + 1000);
Thread.sleep(100);
}
if(retries == 0)
fail("SQL Batch write behind still running.");
}
}

ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate();
ejt.setDataSource(getDataSource());

//Drop everything
runScript(new String[] {"DROP ALL OBJECTS;"}, null);
//Create database
runScript(this.getClass().getResourceAsStream("/createTables-" + getType().name() + ".sql"), null);

for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class))
Expand Down
4 changes: 2 additions & 2 deletions Core/src-test/com/serotonin/m2m2/MangoTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ public void before() {

@After
public void after() {
SimulationTimerProvider provider = (SimulationTimerProvider) Providers.get(TimerProvider.class);
provider.reset();
Common.runtimeManager.terminate();
Common.runtimeManager.joinTermination();
H2InMemoryDatabaseProxy proxy = (H2InMemoryDatabaseProxy) Common.databaseProxy;
Expand All @@ -132,6 +130,8 @@ public void after() {
} catch (Exception e) {
throw new ShouldNeverHappenException(e);
}
SimulationTimerProvider provider = (SimulationTimerProvider) Providers.get(TimerProvider.class);
provider.reset();
}

@AfterClass
Expand Down
23 changes: 8 additions & 15 deletions Core/src-test/com/serotonin/m2m2/MockMangoLifecycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void initialize() throws InterruptedException, ExecutionException {
Common.MA_HOME = maHome;

Common.setModuleClassLoader(Thread.currentThread().getContextClassLoader());

//Add in modules
for(Module module : modules)
ModuleRegistry.addModule(module);
Expand Down Expand Up @@ -141,7 +141,7 @@ public void initialize() throws InterruptedException, ExecutionException {
}

freemarkerInitialize();

//TODO This must be done only once because we have a static
// final referece to the PointValueDao in the PointValueCache class
// and so if you try to restart the database it doesn't get the new connection
Expand Down Expand Up @@ -216,17 +216,17 @@ protected CompletableFuture<ApplicationContext> springRuntimeContextInitialize(

private void freemarkerInitialize() {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
File baseTemplateDir = new File(Common.MA_HOME, "ftl");
File baseTemplateDir = Common.MA_HOME_PATH.resolve("ftl").toFile();
if(!baseTemplateDir.exists()) {
LOG.info("Not initializing Freemarker, this test is not running in Core source tree. Requires ./ftl directory to initialize.");
return;
}

try {
List<TemplateLoader> loaders = new ArrayList<>();

// Add the overrides directory.
File override = new File(Common.MA_HOME, "overrides/ftl");
File override = Common.MA_HOME_PATH.resolve("overrides/ftl").toFile();
if (override.exists())
loaders.add(new FileTemplateLoader(override));

Expand Down Expand Up @@ -259,16 +259,10 @@ public boolean isTerminated() {

@Override
public void terminate() {
// H2InMemoryDatabaseProxy proxy = (H2InMemoryDatabaseProxy) Common.databaseProxy;
// try {
// proxy.clean();
// } catch (Exception e) {
// throw new ShouldNeverHappenException(e);
// }
if(Common.databaseProxy != null)
Common.databaseProxy.terminate(true);

if(Common.serialPortManager != null) {
if (Common.serialPortManager != null) {
try {
Common.serialPortManager.terminate();
} catch (LifecycleException e) {
Expand Down Expand Up @@ -311,15 +305,14 @@ public void loadLic() {
// TODO Auto-generated method stub

}

@Override
public Integer dataPointLimit() {
return Integer.MAX_VALUE;
}

@Override
public Thread scheduleShutdown(Long timeout, boolean b, User user) {

return null;
}

Expand Down
Loading