Skip to content

Commit

Permalink
[DROOLS-7496] fixing concurrency problems in TruthMaintenanceSystemIm…
Browse files Browse the repository at this point in the history
…pl (#5374)
  • Loading branch information
holzig committed Jul 6, 2023
1 parent f8b6125 commit 6d453fc
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.drools.testcoverage.regression;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.drools.kiesession.rulebase.InternalKnowledgeBase;
import org.drools.kiesession.rulebase.KnowledgeBaseFactory;
import org.drools.kiesession.session.StatefulKnowledgeSessionImpl;
import org.drools.mvel.compiler.Cheese;
import org.junit.Test;
import org.kie.api.definition.KiePackage;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.ClassObjectFilter;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;

import static org.assertj.core.api.Assertions.assertThat;


public class TruthMaintenanceSystemConcurrencyTest {

@Test
public void testUsingMultipleSessionsConcurrently() throws InterruptedException {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(
ResourceFactory.newClassPathResource(
"test_concurrency.drl",
getClass()
),
ResourceType.DRL
);
Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();

InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kpkgs);

final ExecutorService executorService = Executors.newFixedThreadPool(20);

final Collection<Throwable> errors =
Collections.synchronizedCollection(new LinkedList<>());
for (int i = 0; i < 2000; i++) {
executorService.submit(() -> {
try {
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kbase.newKieSession();

ksession.fireAllRules();
assertThat(ksession.getObjects(new ClassObjectFilter(Cheese.class)))
.hasSize(2);
} catch (Throwable e) {
errors.add(e);
}
});
}
executorService.shutdown();
assertThat(executorService.awaitTermination(2, TimeUnit.SECONDS))
.isTrue();

assertThat(errors)
.isEmpty();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.testcoverage.regression;

import org.drools.mvel.compiler.Cheese;

rule "default"
then
insert("cheeseType");
insert(new Cheese("cheeseType"));
end


rule "insert new Cheese"
when
$type : String(this == "cheeseType")
Cheese( type == $type )
then
insertLogical( new Cheese("success") );
end
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,14 @@ public TruthMaintenanceSystemImpl(InternalWorkingMemoryEntryPoint ep) {
}

private static class EqualityKeyPlaceholder {
private static final EqualityKeyPlaceholder INSTANCE = new EqualityKeyPlaceholder();
private final Object object;

private Object object;

public EqualityKeyPlaceholder withObject(Object object) {
private EqualityKeyPlaceholder(final Object object) {
this.object = object;
return this;
}

private static Object transformEqualityKey(Object o) {
return o instanceof EqualityKey ? o : EqualityKeyPlaceholder.INSTANCE.withObject(o);
return o instanceof EqualityKey ? o : new EqualityKeyPlaceholder(o);
}

@Override
Expand Down

0 comments on commit 6d453fc

Please sign in to comment.