Fix where automation status got set to OK instead of running; switch to do an automation polling thread per-table/status

This commit is contained in:
2022-10-20 10:47:02 -05:00
parent 8b3b300eb1
commit 20c42deae5
6 changed files with 162 additions and 106 deletions

View File

@ -58,9 +58,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/*******************************************************************************
** Unit test for PollingAutomationRunner
** Unit test for PollingAutomationPerTableRunner
*******************************************************************************/
class PollingAutomationRunnerTest
class PollingAutomationPerTableRunnerTest
{
/*******************************************************************************
@ -81,8 +81,7 @@ class PollingAutomationRunnerTest
@Test
void testInsertAndUpdate() throws QException
{
QInstance qInstance = TestUtils.defineInstance();
PollingAutomationRunner pollingAutomationRunner = new PollingAutomationRunner(qInstance, TestUtils.POLLING_AUTOMATION, null);
QInstance qInstance = TestUtils.defineInstance();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// insert 2 person records, both updated by the insert action, and 1 logged by logger-on-update automation //
@ -101,7 +100,7 @@ class PollingAutomationRunnerTest
// assert that the update-automation won't run - as no UPDATE has happened on the table //
// even though the insert action does update the records!! //
//////////////////////////////////////////////////////////////////////////////////////////
pollingAutomationRunner.run();
runAllTableActions(qInstance);
assertThat(TestUtils.LogPersonUpdate.updatedIds).isNullOrEmpty();
assertAllRecordsAutomationStatus(AutomationStatus.OK);
@ -114,7 +113,7 @@ class PollingAutomationRunnerTest
/////////////////////////////////////////////////////////////////////////////////////////
// run automations again - make sure that there haven't been any updates triggered yet //
/////////////////////////////////////////////////////////////////////////////////////////
pollingAutomationRunner.run();
runAllTableActions(qInstance);
assertThat(TestUtils.LogPersonUpdate.updatedIds).isNullOrEmpty();
assertAllRecordsAutomationStatus(AutomationStatus.OK);
@ -135,7 +134,7 @@ class PollingAutomationRunnerTest
// assert that the update-automation DOES run now - and that it only runs once //
// note that it will only run on a sub-set of the records //
/////////////////////////////////////////////////////////////////////////////////
pollingAutomationRunner.run();
runAllTableActions(qInstance);
assertThat(TestUtils.LogPersonUpdate.updatedIds)
.contains(2)
.hasSize(1);
@ -145,13 +144,27 @@ class PollingAutomationRunnerTest
// re-run and assert no further automations happen //
/////////////////////////////////////////////////////
TestUtils.LogPersonUpdate.updatedIds.clear();
pollingAutomationRunner.run();
runAllTableActions(qInstance);
assertThat(TestUtils.LogPersonUpdate.updatedIds).isNullOrEmpty();
assertAllRecordsAutomationStatus(AutomationStatus.OK);
}
/*******************************************************************************
**
*******************************************************************************/
private void runAllTableActions(QInstance qInstance)
{
List<PollingAutomationPerTableRunner.TableActions> tableActions = PollingAutomationPerTableRunner.getTableActions(qInstance, TestUtils.POLLING_AUTOMATION);
for(PollingAutomationPerTableRunner.TableActions tableAction : tableActions)
{
new PollingAutomationPerTableRunner(qInstance, TestUtils.POLLING_AUTOMATION, null, tableAction).run();
}
}
/*******************************************************************************
** Test a large-ish number - to demonstrate paging working.
**
@ -162,8 +175,7 @@ class PollingAutomationRunnerTest
@Test
void testMultiPages() throws QException
{
QInstance qInstance = TestUtils.defineInstance();
PollingAutomationRunner pollingAutomationRunner = new PollingAutomationRunner(qInstance, TestUtils.POLLING_AUTOMATION, null);
QInstance qInstance = TestUtils.defineInstance();
//////////////////////////////////////////////////////////////////////////////////
// insert many people - half who should be updated by the AgeChecker automation //
@ -186,7 +198,7 @@ class PollingAutomationRunnerTest
/////////////////////////
// run the automations //
/////////////////////////
pollingAutomationRunner.run();
runAllTableActions(qInstance);
assertAllRecordsAutomationStatus(AutomationStatus.OK);
///////////////////////////////////////////////////////////////////////////
@ -213,8 +225,7 @@ class PollingAutomationRunnerTest
@Test
void testRunningProcess() throws QException
{
QInstance qInstance = TestUtils.defineInstance();
PollingAutomationRunner pollingAutomationRunner = new PollingAutomationRunner(qInstance, TestUtils.POLLING_AUTOMATION, null);
QInstance qInstance = TestUtils.defineInstance();
////////////////////////////////////////////////////////////////////
// insert 2 person records, 1 to trigger the "increaseAge" action //
@ -228,7 +239,7 @@ class PollingAutomationRunnerTest
));
new InsertAction().execute(insertInput);
pollingAutomationRunner.run();
runAllTableActions(qInstance);
/////////////////////////////////////////////////////////////////////////////////////////////
// make sure the process ran - which means, it would have updated Tim's birth year to 1900 //
@ -276,19 +287,18 @@ class PollingAutomationRunnerTest
instance.getTable(TestUtils.TABLE_NAME_SHAPE)
.withField(new QFieldMetaData("automationStatus", QFieldType.INTEGER))
.setAutomationDetails(new QTableAutomationDetails()
.withProviderName(TestUtils.POLLING_AUTOMATION)
.withStatusTracking(new AutomationStatusTracking().withType(AutomationStatusTrackingType.FIELD_IN_TABLE).withFieldName("automationStatus"))
.withAction(new TableAutomationAction()
.withName("shapeToPerson")
.withTriggerEvent(TriggerEvent.POST_INSERT)
.withProcessName("shapeToPersonETLProcess")
)
);
.withProviderName(TestUtils.POLLING_AUTOMATION)
.withStatusTracking(new AutomationStatusTracking().withType(AutomationStatusTrackingType.FIELD_IN_TABLE).withFieldName("automationStatus"))
.withAction(new TableAutomationAction()
.withName("shapeToPerson")
.withTriggerEvent(TriggerEvent.POST_INSERT)
.withProcessName("shapeToPersonETLProcess")
)
);
TestUtils.insertDefaultShapes(instance);
PollingAutomationRunner pollingAutomationRunner = new PollingAutomationRunner(instance, TestUtils.POLLING_AUTOMATION, null);
pollingAutomationRunner.run();
runAllTableActions(instance);
List<QRecord> postList = TestUtils.queryTable(instance, TestUtils.TABLE_NAME_PERSON);
assertThat(postList)

View File

@ -24,6 +24,7 @@ package com.kingsrook.qqq.backend.core.actions.automation.polling;
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@ -127,7 +128,8 @@ class StandardScheduledExecutorTest
//////////////////////////////////////////////////////////////////////
qInstance.getTable(TestUtils.TABLE_NAME_PERSON_MEMORY)
.getAutomationDetails().getActions().get(0)
.setCodeReference(new QCodeReference(CaptureSessionIdAutomationHandler.class));
.withCodeReference(new QCodeReference(CaptureSessionIdAutomationHandler.class))
.withName("captureSessionId");
////////////////////////////////////////////////////////////
// insert a person that will trigger the on-insert action //
@ -184,17 +186,24 @@ class StandardScheduledExecutorTest
*******************************************************************************/
private void runPollingAutomationExecutorForAwhile(QInstance qInstance, Supplier<QSession> sessionSupplier)
{
PollingAutomationRunner pollingAutomationRunner = new PollingAutomationRunner(qInstance, TestUtils.POLLING_AUTOMATION, sessionSupplier);
List<PollingAutomationPerTableRunner.TableActions> tableActions = PollingAutomationPerTableRunner.getTableActions(qInstance, TestUtils.POLLING_AUTOMATION);
List<StandardScheduledExecutor> executors = new ArrayList<>();
for(PollingAutomationPerTableRunner.TableActions tableAction : tableActions)
{
PollingAutomationPerTableRunner pollingAutomationPerTableRunner = new PollingAutomationPerTableRunner(qInstance, TestUtils.POLLING_AUTOMATION, sessionSupplier, tableAction);
StandardScheduledExecutor pollingAutomationExecutor = new StandardScheduledExecutor(pollingAutomationPerTableRunner);
pollingAutomationExecutor.setInitialDelayMillis(0);
pollingAutomationExecutor.setDelayMillis(100);
pollingAutomationExecutor.setQInstance(qInstance);
pollingAutomationExecutor.setName(TestUtils.POLLING_AUTOMATION);
pollingAutomationExecutor.setSessionSupplier(sessionSupplier);
pollingAutomationExecutor.start();
executors.add(pollingAutomationExecutor);
}
StandardScheduledExecutor pollingAutomationExecutor = new StandardScheduledExecutor(pollingAutomationRunner);
pollingAutomationExecutor.setInitialDelayMillis(0);
pollingAutomationExecutor.setDelayMillis(100);
pollingAutomationExecutor.setQInstance(qInstance);
pollingAutomationExecutor.setName(TestUtils.POLLING_AUTOMATION);
pollingAutomationExecutor.setSessionSupplier(sessionSupplier);
pollingAutomationExecutor.start();
SleepUtils.sleep(1, TimeUnit.SECONDS);
pollingAutomationExecutor.stop();
executors.forEach(StandardScheduledExecutor::stop);
}
}