|
| 1 | +/* Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | + * you may not use this file except in compliance with the License. |
| 3 | + * You may obtain a copy of the License at |
| 4 | + * |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | +package org.flowable.cmmn.test; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.assertj.core.api.Assertions.entry; |
| 17 | + |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import org.flowable.common.engine.impl.cfg.CommandExecutorImpl; |
| 21 | +import org.flowable.common.engine.impl.db.DbSqlSessionFactory; |
| 22 | +import org.flowable.common.engine.impl.interceptor.CommandExecutor; |
| 23 | +import org.flowable.common.engine.impl.interceptor.CommandInterceptor; |
| 24 | +import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl; |
| 25 | +import org.flowable.engine.test.profiler.FlowableProfiler; |
| 26 | +import org.flowable.engine.test.profiler.ProfilingDbSqlSessionFactory; |
| 27 | +import org.flowable.engine.test.profiler.TotalExecutionTimeCommandInterceptor; |
| 28 | +import org.junit.jupiter.api.AfterEach; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.condition.DisabledIfSystemProperty; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Filip Hrisafov |
| 35 | + */ |
| 36 | +@DisabledIfSystemProperty(named = "disableWhen", matches = "cockroachdb") |
| 37 | +public class VerifyDatabaseOperationsTest extends AbstractProcessEngineIntegrationTest { |
| 38 | + |
| 39 | + protected DbSqlSessionFactory oldDbSqlSessionFactory; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + protected void setUp() { |
| 43 | + // The process and the CMMN engine share the session factories |
| 44 | + ProcessEngineConfigurationImpl processEngineConfigurationImpl = (ProcessEngineConfigurationImpl) processEngineConfiguration; |
| 45 | + this.oldDbSqlSessionFactory = processEngineConfigurationImpl.getDbSqlSessionFactory(); |
| 46 | + DbSqlSessionFactory newDbSqlSessionFactory = new ProfilingDbSqlSessionFactory(processEngineConfigurationImpl.isUsePrefixId()); |
| 47 | + newDbSqlSessionFactory.setBulkInserteableEntityClasses(oldDbSqlSessionFactory.getBulkInserteableEntityClasses()); |
| 48 | + newDbSqlSessionFactory.setInsertionOrder(oldDbSqlSessionFactory.getInsertionOrder()); |
| 49 | + newDbSqlSessionFactory.setDeletionOrder(oldDbSqlSessionFactory.getDeletionOrder()); |
| 50 | + newDbSqlSessionFactory.setDatabaseType(oldDbSqlSessionFactory.getDatabaseType()); |
| 51 | + newDbSqlSessionFactory.setDatabaseTablePrefix(oldDbSqlSessionFactory.getDatabaseTablePrefix()); |
| 52 | + newDbSqlSessionFactory.setTablePrefixIsSchema(oldDbSqlSessionFactory.isTablePrefixIsSchema()); |
| 53 | + newDbSqlSessionFactory.setDatabaseCatalog(oldDbSqlSessionFactory.getDatabaseCatalog()); |
| 54 | + newDbSqlSessionFactory.setDatabaseSchema(oldDbSqlSessionFactory.getDatabaseSchema()); |
| 55 | + newDbSqlSessionFactory.setSqlSessionFactory(oldDbSqlSessionFactory.getSqlSessionFactory()); |
| 56 | + newDbSqlSessionFactory.setDbHistoryUsed(oldDbSqlSessionFactory.isDbHistoryUsed()); |
| 57 | + newDbSqlSessionFactory.setDatabaseSpecificStatements(oldDbSqlSessionFactory.getDatabaseSpecificStatements()); |
| 58 | + newDbSqlSessionFactory.setLogicalNameToClassMapping(oldDbSqlSessionFactory.getLogicalNameToClassMapping()); |
| 59 | + processEngineConfigurationImpl.addSessionFactory(newDbSqlSessionFactory); |
| 60 | + } |
| 61 | + |
| 62 | + @AfterEach |
| 63 | + protected void tearDown() { |
| 64 | + ((ProcessEngineConfigurationImpl) processEngineConfiguration).addSessionFactory(oldDbSqlSessionFactory); |
| 65 | + FlowableProfiler.getInstance().reset(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testCaseWithProcessTaskFromSameDeployment() { |
| 70 | + processEngineRepositoryService.createDeployment() |
| 71 | + .addClasspathResource("org/flowable/cmmn/test/processTaskSameDeploymentTrue.cmmn") |
| 72 | + .addClasspathResource("org/flowable/cmmn/test/oneTaskProcess.bpmn20.xml") |
| 73 | + .deploy(); |
| 74 | + |
| 75 | + Map<String, Long> selects = profileDatabaseSelects(cmmnEngineConfiguration.getCommandExecutor(), "org.flowable.cmmn.engine.impl.cmd.StartCaseInstanceCmd", |
| 76 | + () -> cmmnRuntimeService.createCaseInstanceBuilder() |
| 77 | + .caseDefinitionKey("oneProcessTaskCase") |
| 78 | + .start()); |
| 79 | + |
| 80 | + // The cached case definition has the parent deployment id, so the CMMN deployment is not selected |
| 81 | + assertThat(selects).containsOnly( |
| 82 | + entry("selectLatestCaseDefinitionByKey", 1L), |
| 83 | + entry("selectProcessDefinitionByParentDeploymentAndKey", 1L), |
| 84 | + entry("selectProcessDefinitionInfoByProcessDefinitionId", 1L), |
| 85 | + entry("selectPlanItemInstancesByCaseInstanceIdAndPlanItemId", 1L), |
| 86 | + entry("selectEntityLinksByQuery", 2L) |
| 87 | + ); |
| 88 | + assertThat(processEngineRuntimeService.createProcessInstanceQuery().count()).isOne(); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testCaseWithCaseTaskFromSameDeployment() { |
| 93 | + cmmnRepositoryService.createDeployment() |
| 94 | + .addClasspathResource("org/flowable/cmmn/test/caseTaskSameDeploymentTrue.cmmn") |
| 95 | + .addClasspathResource("org/flowable/cmmn/test/oneHumanTaskCase.cmmn") |
| 96 | + .deploy(); |
| 97 | + |
| 98 | + Map<String, Long> selects = profileDatabaseSelects(cmmnEngineConfiguration.getCommandExecutor(), "org.flowable.cmmn.engine.impl.cmd.StartCaseInstanceCmd", |
| 99 | + () -> cmmnRuntimeService.createCaseInstanceBuilder() |
| 100 | + .caseDefinitionKey("oneCaseTaskCase") |
| 101 | + .start()); |
| 102 | + |
| 103 | + // The cached case definition has the parent deployment id, so the CMMN deployment is not selected |
| 104 | + assertThat(selects).containsOnly( |
| 105 | + entry("selectLatestCaseDefinitionByKey", 1L), |
| 106 | + entry("selectCaseDefinitionByParentDeploymentAndKey", 1L), |
| 107 | + entry("selectPlanItemInstancesByCaseInstanceIdAndPlanItemId", 2L), |
| 108 | + entry("selectEntityLinksByQuery", 2L) |
| 109 | + ); |
| 110 | + assertThat(cmmnRuntimeService.createCaseInstanceQuery().caseDefinitionKey("oneHumanTaskCase").count()).isOne(); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testProcessWithCaseTaskFromSameDeployment() { |
| 115 | + processEngineRepositoryService.createDeployment() |
| 116 | + .addClasspathResource("org/flowable/cmmn/test/caseTaskProcessWithSameDeployment.bpmn20.xml") |
| 117 | + .addClasspathResource("org/flowable/cmmn/test/oneHumanTaskCase.cmmn") |
| 118 | + .deploy(); |
| 119 | + |
| 120 | + Map<String, Long> selects = profileDatabaseSelects(processEngineConfiguration.getCommandExecutor(), "org.flowable.engine.impl.cmd.StartProcessInstanceCmd", |
| 121 | + () -> processEngineRuntimeService.createProcessInstanceBuilder() |
| 122 | + .processDefinitionKey("caseTask") |
| 123 | + .start()); |
| 124 | + |
| 125 | + // The cached process definition has the parent deployment id, so the process deployment is not selected |
| 126 | + assertThat(selects).containsOnly( |
| 127 | + entry("selectLatestProcessDefinitionByKey", 1L), |
| 128 | + entry("selectCaseDefinitionByParentDeploymentAndKey", 1L), |
| 129 | + entry("selectProcessDefinitionInfoByProcessDefinitionId", 1L), |
| 130 | + entry("selectPlanItemInstancesByCaseInstanceIdAndPlanItemId", 1L), |
| 131 | + entry("selectEntityLinksByQuery", 2L) |
| 132 | + ); |
| 133 | + assertThat(cmmnRuntimeService.createCaseInstanceQuery().count()).isOne(); |
| 134 | + } |
| 135 | + |
| 136 | + protected Map<String, Long> profileDatabaseSelects(CommandExecutor commandExecutor, String commandClass, Runnable runnable) { |
| 137 | + // Only the outer command is profiled, the commands of the other engine reuse its command context |
| 138 | + CommandExecutorImpl commandExecutorImpl = (CommandExecutorImpl) commandExecutor; |
| 139 | + CommandInterceptor oldFirstCommandInterceptor = commandExecutorImpl.getFirst(); |
| 140 | + TotalExecutionTimeCommandInterceptor timeCommandInterceptor = new TotalExecutionTimeCommandInterceptor(); |
| 141 | + timeCommandInterceptor.setNext(oldFirstCommandInterceptor); |
| 142 | + commandExecutorImpl.setFirst(timeCommandInterceptor); |
| 143 | + |
| 144 | + FlowableProfiler profiler = FlowableProfiler.getInstance(); |
| 145 | + profiler.reset(); |
| 146 | + profiler.startProfileSession("Profiling session"); |
| 147 | + try { |
| 148 | + runnable.run(); |
| 149 | + } finally { |
| 150 | + profiler.stopCurrentProfileSession(); |
| 151 | + commandExecutorImpl.setFirst(oldFirstCommandInterceptor); |
| 152 | + } |
| 153 | + |
| 154 | + return profiler.getProfileSessions().get(0).calculateSummaryStatistics().get(commandClass).getDbSelects(); |
| 155 | + } |
| 156 | +} |
0 commit comments