From 9de8d0b54b364bda1b1d76d81923c8bfc060c2f8 Mon Sep 17 00:00:00 2001 From: Mario Fusco Date: Mon, 19 Jun 2023 18:24:17 +0200 Subject: [PATCH] rework drools-reliability tests to make them work seamlessly with one or more sessions (#5330) --- .../infinispan/CachePersistenceTest.java | 17 +- .../infinispan/ReliabilityCepTest.java | 30 ++-- .../infinispan/ReliabilityTest.java | 113 +++++++------ .../infinispan/ReliabilityTestBasics.java | 149 ++++++++++++------ .../ReliabilityUpdateInDrlTest.java | 12 +- .../infinispan/smoke/BaseSmokeTest.java | 4 +- 6 files changed, 189 insertions(+), 136 deletions(-) diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/CachePersistenceTest.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/CachePersistenceTest.java index b14d993f304..e84ca1b99e5 100644 --- a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/CachePersistenceTest.java +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/CachePersistenceTest.java @@ -15,8 +15,6 @@ package org.drools.reliability.infinispan; -import java.util.Optional; - import org.drools.reliability.core.StorageManagerFactory; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; @@ -24,6 +22,8 @@ import org.kie.api.runtime.conf.PersistedSessionOption; import org.test.domain.Person; +import java.util.Optional; + import static org.assertj.core.api.Assertions.assertThat; import static org.drools.reliability.core.StorageManagerFactory.SESSION_STORAGE_PREFIX; @@ -54,7 +54,7 @@ void removeAllSessionCaches_shouldRemoveAllSessionCachesEvenAfterFailover(Persis restoreSession(EMPTY_RULE, strategy); // restored but no objects in the cache - Optional toshiya = getPersonByName(session, "Toshiya"); + Optional toshiya = getPersonByName("Toshiya"); assertThat(toshiya).isEmpty(); // So cannot recover the fact } @@ -63,12 +63,13 @@ void removeAllSessionCaches_shouldRemoveAllSessionCachesEvenAfterFailover(Persis void ksessionDispose_shouldRemoveCache(PersistedSessionOption.PersistenceStrategy strategy){ createSession(EMPTY_RULE, strategy); // sessionId = 0. This creates session_0_epDEFAULT and session_0_globals + long sessionId = getSessionIdentifier(); insertNonMatchingPerson("Toshiya", 10); disposeSession(); // This should clean up session's cache - assertThat(StorageManagerFactory.get().getStorageManager().getStorageNames()).allMatch(name -> !name.startsWith(SESSION_STORAGE_PREFIX + savedSessionId)); + assertThat(StorageManagerFactory.get().getStorageManager().getStorageNames()).allMatch(name -> !name.startsWith(SESSION_STORAGE_PREFIX + sessionId)); } @ParameterizedTest @@ -82,14 +83,14 @@ void missingDispose_shouldNotReuseOrphanedCache(PersistedSessionOption.Persisten createSession(EMPTY_RULE, strategy); // new session. If sessionId = 0, it will potentially reuse the orphaned cache - Optional toshiya = getPersonByName(session, "Toshiya"); + Optional toshiya = getPersonByName("Toshiya"); assertThat(toshiya).isEmpty(); // new session doesn't trigger re-propagation failover(); restoreSession(EMPTY_RULE, strategy); // restoreSession triggers re-propagation - toshiya = getPersonByName(session, "Toshiya"); + toshiya = getPersonByName("Toshiya"); assertThat(toshiya).isEmpty(); // should not reuse the orphaned cache } @@ -97,12 +98,12 @@ void missingDispose_shouldNotReuseOrphanedCache(PersistedSessionOption.Persisten @MethodSource("strategyProviderStoresOnly") void reliableSessionCounter_shouldNotHaveTheSameIdAsPreviousKsession(PersistedSessionOption.PersistenceStrategy strategy) { createSession(EMPTY_RULE, strategy); // new session. sessionId = 0 - long firstSessionId = session.getIdentifier(); + long firstSessionId = getSessionIdentifier(); failover(); createSession(EMPTY_RULE, strategy); // new session. sessionId = 1 - long secondSessionId = session.getIdentifier(); + long secondSessionId = getSessionIdentifier(); assertThat(secondSessionId).isNotEqualTo(firstSessionId); // sessionId should not be the same } diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityCepTest.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityCepTest.java index 0a0f2ad4afb..cc3d9e7bb12 100644 --- a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityCepTest.java +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityCepTest.java @@ -49,33 +49,33 @@ void insertAdvanceInsertFailoverFire_shouldRecoverFromFailover(PersistedSessionO createSession(CEP_RULE, persistenceStrategy, safepointStrategy, EventProcessingOption.STREAM, ClockTypeOption.PSEUDO); - SessionPseudoClock clock = session.getSessionClock(); + SessionPseudoClock clock = getSessionClock(); - session.insert( new StockTick( "DROO" ) ); + insert( new StockTick( "DROO" ) ); clock.advanceTime( 6, TimeUnit.SECONDS ); - session.insert( new StockTick( "ACME" ) ); + insert( new StockTick( "ACME" ) ); //-- Assume JVM down here. Fail-over to other JVM or rebooted JVM //-- ksession and kbase are lost. CacheManager is recreated. Client knows only "id" failover(); restoreSession(CEP_RULE, persistenceStrategy, safepointStrategy, EventProcessingOption.STREAM, ClockTypeOption.PSEUDO); - clock = session.getSessionClock(); + clock = getSessionClock(); - assertThat(session.fireAllRules()).isEqualTo(1); + assertThat(fireAllRules()).isEqualTo(1); assertThat(getResults()).containsExactlyInAnyOrder("fired"); clearResults(); clock.advanceTime( 1, TimeUnit.SECONDS ); - session.insert( new StockTick( "ACME" ) ); + insert( new StockTick( "ACME" ) ); - assertThat(session.fireAllRules()).isEqualTo(1); + assertThat(fireAllRules()).isEqualTo(1); assertThat(getResults()).containsExactlyInAnyOrder("fired"); clearResults(); clock.advanceTime( 3, TimeUnit.SECONDS ); - session.insert( new StockTick( "ACME" ) ); + insert( new StockTick( "ACME" ) ); - assertThat(session.fireAllRules()).isZero(); + assertThat(fireAllRules()).isZero(); assertThat(getResults()).isEmpty(); } @@ -84,20 +84,20 @@ void insertAdvanceInsertFailoverFire_shouldRecoverFromFailover(PersistedSessionO void insertAdvanceFailoverExpireFire_shouldExpireAfterFailover(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy) { createSession(CEP_RULE, persistenceStrategy, safepointStrategy, EventProcessingOption.STREAM, ClockTypeOption.PSEUDO); - SessionPseudoClock clock = session.getSessionClock(); + SessionPseudoClock clock = getSessionClock(); - session.insert( new StockTick( "DROO" ) ); + insert( new StockTick( "DROO" ) ); clock.advanceTime( 6, TimeUnit.SECONDS ); - session.insert( new StockTick( "ACME" ) ); + insert( new StockTick( "ACME" ) ); failover(); restoreSession(CEP_RULE, persistenceStrategy, safepointStrategy, EventProcessingOption.STREAM, ClockTypeOption.PSEUDO); - clock = session.getSessionClock(); + clock = getSessionClock(); clock.advanceTime(58, TimeUnit.SECONDS); - assertThat(session.fireAllRules()).as("DROO is expired, but a match is available.") + assertThat(fireAllRules()).as("DROO is expired, but a match is available.") .isEqualTo(1); - assertThat(session.getFactHandles()).as("DROO should have expired because @Expires = 60s") + assertThat(getFactHandles()).as("DROO should have expired because @Expires = 60s") .hasSize(1); } } diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTest.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTest.java index 26aab60dd87..611a408534f 100644 --- a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTest.java +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTest.java @@ -15,7 +15,6 @@ package org.drools.reliability.infinispan; -import org.drools.reliability.core.ReliableKieSession; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; @@ -25,8 +24,6 @@ import org.kie.api.runtime.rule.FactHandle; import org.test.domain.Person; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; @ExtendWith(BeforeAllMethodExtension.class) @@ -46,13 +43,13 @@ class ReliabilityTest extends ReliabilityTestBasics { void createAndUseOfNonReliableSession_shouldWorkNormally() { createSession(BASIC_RULE, null); - insertString("M"); + insert("M"); insertMatchingPerson("Matching Person One", 37); insertNonMatchingPerson("Toshiya", 35); insertMatchingPerson("Matching Person Two", 40); - session.fireAllRules(); + fireAllRules(); assertThat(getResults()).containsExactlyInAnyOrder("Matching Person One", "Matching Person Two"); } @@ -62,7 +59,7 @@ void createAndUseOfNonReliableSession_shouldWorkNormally() { void insertFailoverInsertFire_shouldRecoverFromFailover(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy) { createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); - insertString("M"); + insert("M"); insertMatchingPerson("Matching Person One", 37); //-- Assume JVM down here. Fail-over to other JVM or rebooted JVM @@ -74,7 +71,7 @@ void insertFailoverInsertFire_shouldRecoverFromFailover(PersistedSessionOption.P insertNonMatchingPerson("Toshiya", 35); insertMatchingPerson("Matching Person Two", 40); - session.fireAllRules(); + fireAllRules(); assertThat(getResults()).containsExactlyInAnyOrder("Matching Person One", "Matching Person Two"); } @@ -86,18 +83,18 @@ void noFailover(PersistedSessionOption.PersistenceStrategy persistenceStrategy, createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); - insertString("M"); + insert("M"); insertMatchingPerson("Matching Person One", 37); if (safepointStrategy == PersistedSessionOption.SafepointStrategy.EXPLICIT) { - ((ReliableKieSession) session).safepoint(); + safepoint(); } restoreSession(BASIC_RULE, persistenceStrategy, safepointStrategy); insertNonMatchingPerson("Toshiya", 41); insertMatchingPerson("Matching Person Two", 40); - session.fireAllRules(); + fireAllRules(); assertThat(getResults()).containsExactlyInAnyOrder("Matching Person One", "Matching Person Two"); } @@ -109,10 +106,10 @@ void insertFireInsertFailoverInsertFire_shouldMatchFactInsertedBeforeFailover(Pe createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); - insertString("M"); + insert("M"); insertMatchingPerson("Matching Person One", 37); - session.fireAllRules(); + fireAllRules(); insertMatchingPerson("Matching Person Two", 40); @@ -124,7 +121,7 @@ void insertFireInsertFailoverInsertFire_shouldMatchFactInsertedBeforeFailover(Pe insertNonMatchingPerson("Toshiya", 35); insertMatchingPerson("Matching Person Three", 41); - session.fireAllRules(); + fireAllRules(); assertThat(getResults()).containsExactlyInAnyOrder("Matching Person Two", "Matching Person Three"); } @@ -134,10 +131,10 @@ void insertFireInsertFailoverInsertFire_shouldMatchFactInsertedBeforeFailover(Pe void insertFireFailoverInsertFire_shouldNotRepeatFiredMatch(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy) { createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); - insertString("M"); + insert("M"); insertMatchingPerson("Matching Person One", 37); - session.fireAllRules(); + fireAllRules(); failover(); @@ -146,7 +143,7 @@ void insertFireFailoverInsertFire_shouldNotRepeatFiredMatch(PersistedSessionOpti insertNonMatchingPerson("Toshiya", 35); insertMatchingPerson("Matching Person Two", 40); - session.fireAllRules(); + fireAllRules(); assertThat(getResults()).containsExactlyInAnyOrder("Matching Person One", "Matching Person Two"); } @@ -157,31 +154,31 @@ void updateBeforeFailover_shouldRecoverFromFailover(PersistedSessionOption.Persi createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); - insertString("M"); + insert("M"); Person p1 = new Person("Mario", 49); - FactHandle fh1 = session.insert(p1); + FactHandle fh1 = insert(p1); Person p2 = new Person("Toshiya", 45); - FactHandle fh2 = session.insert(p2); + FactHandle fh2 = insert(p2); - assertThat(session.fireAllRules()).isEqualTo(1); + assertThat(fireAllRules()).isEqualTo(1); assertThat(getResults()).containsExactlyInAnyOrder("Mario"); p1.setName("SuperMario"); - session.update(fh1, p1); + update(fh1, p1); p2.setName("MegaToshiya"); - session.update(fh2, p2); + update(fh2, p2); failover(); restoreSession(BASIC_RULE, persistenceStrategy, safepointStrategy); - assertThat(session.fireAllRules()).isEqualTo(1); + assertThat(fireAllRules()).isEqualTo(1); assertThat(getResults()).containsExactlyInAnyOrder("Mario", "MegaToshiya"); failover(); restoreSession(BASIC_RULE, persistenceStrategy, safepointStrategy); clearResults(); - assertThat(session.fireAllRules()).isZero(); + assertThat(fireAllRules()).isZero(); assertThat(getResults()).isEmpty(); } @@ -191,14 +188,14 @@ void deleteBeforeFailover_shouldRecoverFromFailover(PersistedSessionOption.Persi createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); - FactHandle fhString = insertString("M"); + FactHandle fhString = insert("M"); insertMatchingPerson("Matching Person One",37); insertNonMatchingPerson("Toshiya",35); - assertThat(session.fireAllRules()).isEqualTo(1); + assertThat(fireAllRules()).isEqualTo(1); assertThat(getResults()).containsExactlyInAnyOrder("Matching Person One"); - session.delete(fhString); + delete(fhString); failover(); restoreSession(BASIC_RULE, persistenceStrategy, safepointStrategy); @@ -206,15 +203,15 @@ void deleteBeforeFailover_shouldRecoverFromFailover(PersistedSessionOption.Persi insertMatchingPerson("Matching Person Two",40); - assertThat(session.fireAllRules()).isZero(); + assertThat(fireAllRules()).isZero(); assertThat(getResults()).isEmpty(); - insertString("T"); + insert("T"); failover(); restoreSession(BASIC_RULE, persistenceStrategy, safepointStrategy); - assertThat(session.fireAllRules()).isEqualTo(1); + assertThat(fireAllRules()).isEqualTo(1); assertThat(getResults()).containsExactlyInAnyOrder("Toshiya"); } @@ -224,19 +221,19 @@ void updateByObjectBeforeFailover_shouldMatchUpdatedFact(PersistedSessionOption. createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); - insertString("M"); + insert("M"); insertMatchingPerson("Mark", 37); FactHandle fhNicole = insertNonMatchingPerson("Nicole", 32); - assertThat(session.fireAllRules()).isEqualTo(1); + assertThat(fireAllRules()).isEqualTo(1); updateWithMatchingPerson(fhNicole,new Person("Mary", 32)); failover(); - restoreSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + KieSession session = restoreSession(BASIC_RULE, persistenceStrategy, safepointStrategy); - assertThat(session.fireAllRules()).isEqualTo(1); + assertThat(fireAllRules()).isEqualTo(1); failover(); @@ -248,7 +245,7 @@ void updateByObjectBeforeFailover_shouldMatchUpdatedFact(PersistedSessionOption. void insertFailover_propListShouldNotBeEmpty(PersistedSessionOption.PersistenceStrategy strategy){ createSession(BASIC_RULE, strategy); - insertString("M"); + insert("M"); insertMatchingPerson("Maria", 30); failover(); @@ -256,7 +253,7 @@ void insertFailover_propListShouldNotBeEmpty(PersistedSessionOption.PersistenceS restoreSession(BASIC_RULE, strategy); - assertThat(session.fireAllRules()).isEqualTo(1); + assertThat(fireAllRules()).isEqualTo(1); } @ParameterizedTest @@ -264,16 +261,16 @@ void insertFailover_propListShouldNotBeEmpty(PersistedSessionOption.PersistenceS void insertFireFailover_shouldNotRepeatFiredMatch(PersistedSessionOption.PersistenceStrategy strategy){ createSession(BASIC_RULE, strategy); - insertString("M"); + insert("M"); insertMatchingPerson("Maria", 30); - session.fireAllRules(); + fireAllRules(); failover(); restoreSession(BASIC_RULE, strategy); - assertThat(session.fireAllRules()).isZero(); + assertThat(fireAllRules()).isZero(); } @ParameterizedTest @@ -281,7 +278,7 @@ void insertFireFailover_shouldNotRepeatFiredMatch(PersistedSessionOption.Persist void insertUpdateFailover_shouldNotFiredMatch(PersistedSessionOption.PersistenceStrategy strategy){ createSession(BASIC_RULE, strategy); - insertString("M"); + insert("M"); FactHandle fhMaria = insertMatchingPerson("Maria", 30); updateWithNonMatchingPerson(fhMaria, new Person("Nicole", 32)); @@ -290,15 +287,15 @@ void insertUpdateFailover_shouldNotFiredMatch(PersistedSessionOption.Persistence restoreSession(BASIC_RULE, strategy); - assertThat(session.fireAllRules()).isZero(); + assertThat(fireAllRules()).isZero(); } @ParameterizedTest @MethodSource("strategyProviderFull") - void insertNonMatching_Failover_UpdateWithMatching_ShouldFiredMatch(PersistedSessionOption.PersistenceStrategy strategy){ + void insertNonMatching_Failover_UpdateWithMatching_ShouldFiredMatch(PersistedSessionOption.PersistenceStrategy strategy) { createSession(BASIC_RULE, strategy); - insertString("N"); + insert("N"); FactHandle fhMaria = insertMatchingPerson("Maria", 30); failover(); @@ -307,23 +304,23 @@ void insertNonMatching_Failover_UpdateWithMatching_ShouldFiredMatch(PersistedSes updateWithMatchingPerson(fhMaria, new Person("Nicole",32)); - assertThat(session.fireAllRules()).isEqualTo(1); + assertThat(fireAllRules()).isEqualTo(1); } @ParameterizedTest @MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints") // FAILS in STORES_ONLY, EXPLICIT void multipleKieSessions_BasicTest(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy) { - KieSession session1 = createSession_m(BASIC_RULE, persistenceStrategy, safepointStrategy); - KieSession session2 = createSession_m(BASIC_RULE, persistenceStrategy, safepointStrategy); + KieSession session1 = createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); + KieSession session2 = createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); - session1.insert("M"); - session2.insert("N"); + insert(session1, "M"); + insert(session2, "N"); - session1.insert(new Person("Mike-session1",27)); // insert matching person - session2.insert(new Person("Mary-session2",34)); // insert non matching person + insert(session1, new Person("Mike-session1",27)); // insert matching person + insert(session2, new Person("Mary-session2",34)); // insert non matching person - assertThat(session1.fireAllRules()).isEqualTo(1); - assertThat(session2.fireAllRules()).isEqualTo(0); + assertThat(fireAllRules(session1)).isEqualTo(1); + assertThat(fireAllRules(session2)).isEqualTo(0); failover(); @@ -331,14 +328,14 @@ void multipleKieSessions_BasicTest(PersistedSessionOption.PersistenceStrategy pe session2 = restoreSession(session2.getIdentifier(), BASIC_RULE, persistenceStrategy, safepointStrategy); // clear results - ((List) session1.getGlobal("results")).clear(); - ((List) session2.getGlobal("results")).clear(); + clearResults(session1); + clearResults(session2); - session1.insert(new Person("Michael-session1",42)); // insert matching person - session2.insert(new Person("Nancy-session2",25)); // insert matching person + insert(session1, new Person("Michael-session1",42)); // insert matching person + insert(session2, new Person("Nancy-session2",25)); // insert matching person - assertThat(session1.fireAllRules()).isEqualTo(1); - assertThat(session2.fireAllRules()).isEqualTo(1); + assertThat(fireAllRules(session1)).isEqualTo(1); + assertThat(fireAllRules(session2)).isEqualTo(1); } } diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTestBasics.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTestBasics.java index e8e58bf329f..a1d0df939fb 100644 --- a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTestBasics.java +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTestBasics.java @@ -37,12 +37,14 @@ import org.kie.api.runtime.conf.KieSessionOption; import org.kie.api.runtime.conf.PersistedSessionOption; import org.kie.api.runtime.rule.FactHandle; +import org.kie.api.time.SessionPseudoClock; import org.kie.internal.utils.KieHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.test.domain.Person; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Optional; import java.util.stream.Stream; @@ -57,10 +59,10 @@ public abstract class ReliabilityTestBasics { private static final Logger LOG = LoggerFactory.getLogger(ReliabilityTestBasics.class); private InfinispanContainer container; - protected long savedSessionId; - protected List savedSessionIds; - protected List sessions_m; - protected KieSession session; + + protected final List sessions = new ArrayList<>(); + + private long persistedSessionId = -1; protected PersistedSessionOption.SafepointStrategy safepointStrategy; @@ -124,13 +126,9 @@ public void tearDown() { public void failover() { if (safepointStrategy == PersistedSessionOption.SafepointStrategy.EXPLICIT) { - if (this.sessions_m!=null){ - this.sessions_m.forEach(session -> { - ((ReliableKieSession) session).safepoint(); - }); - this.sessions_m.clear(); - }else {((ReliableKieSession) session).safepoint();} + this.sessions.stream().map(ReliableKieSession.class::cast).forEach(ReliableKieSession::safepoint); } + sessions.clear(); if (((TestableStorageManager) StorageManagerFactory.get().getStorageManager()).isRemote()) { // fail-over means restarting Drools instance. Assuming remote infinispan keeps alive @@ -145,35 +143,75 @@ public void failover() { ReliableRuntimeComponentFactoryImpl.refreshCounterUsingStorage(); } - protected FactHandle insertString(String str) { - return session.insert(str); + protected FactHandle insert(Object obj) { + return insert(sessions.get(0), obj); + } + + protected FactHandle insert(KieSession session, Object obj) { + return session.insert(obj); + } + + protected void update(FactHandle fh, Object obj) { + update(sessions.get(0), fh, obj); + } + + protected void update(KieSession session, FactHandle fh, Object obj) { + session.update(fh, obj); } - protected FactHandle insertInteger(Integer number) { - return session.insert(number); + protected void delete(FactHandle fh) { + delete(sessions.get(0), fh); + } + + protected void delete(KieSession session, FactHandle fh) { + session.delete(fh); } protected FactHandle insertMatchingPerson(String name, Integer age) { + return insertMatchingPerson(sessions.get(0), name, age); + } + + protected FactHandle insertMatchingPerson(KieSession session, String name, Integer age) { return session.insert(new Person(name, age)); } - protected void updateWithMatchingPerson(FactHandle nonMatching, Object matching){ + protected void updateWithMatchingPerson(FactHandle nonMatching, Object matching) { + updateWithMatchingPerson(sessions.get(0), nonMatching, matching); + } + + protected void updateWithMatchingPerson(KieSession session, FactHandle nonMatching, Object matching) { session.update(nonMatching,matching); } - protected void updateWithNonMatchingPerson(FactHandle matching, Object nonMatching){ + protected void updateWithNonMatchingPerson(FactHandle matching, Object nonMatching) { + updateWithNonMatchingPerson(sessions.get(0), matching, nonMatching); + } + + protected void updateWithNonMatchingPerson(KieSession session, FactHandle matching, Object nonMatching) { session.update(matching, nonMatching); } protected FactHandle insertNonMatchingPerson(String name, Integer age) { + return insertNonMatchingPerson(sessions.get(0), name, age); + } + + protected FactHandle insertNonMatchingPerson(KieSession session, String name, Integer age) { return session.insert(new Person(name, age)); } protected List getResults() { + return getResults(sessions.get(0)); + } + + protected List getResults(KieSession session) { return (List) session.getGlobal("results"); } protected void clearResults() { + clearResults(sessions.get(0)); + } + + protected void clearResults(KieSession session) { ((List) session.getGlobal("results")).clear(); } @@ -182,17 +220,7 @@ protected KieSession createSession(String drl, PersistedSessionOption.Persistenc } protected KieSession createSession(String drl, PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy, Option... options) { - getKieSession(drl, persistenceStrategy != null ? PersistedSessionOption.newSession().withPersistenceStrategy(persistenceStrategy).withSafepointStrategy(safepointStrategy) : null, options); - savedSessionId = session.getIdentifier(); - return session; - } - - protected KieSession createSession_m(String drl, PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy, Option... options) { - KieSession newSession = getKieSession_m(drl, persistenceStrategy != null ? PersistedSessionOption.newSession().withPersistenceStrategy(persistenceStrategy).withSafepointStrategy(safepointStrategy) : null, options); - if (this.sessions_m==null){this.sessions_m=new ArrayList<>();} - this.sessions_m.add(newSession); - - return newSession; + return getKieSession(drl, persistenceStrategy != null ? PersistedSessionOption.newSession().withPersistenceStrategy(persistenceStrategy).withSafepointStrategy(safepointStrategy) : null, options); } protected KieSession restoreSession(String drl, PersistedSessionOption.PersistenceStrategy persistenceStrategy, Option... options) { @@ -200,37 +228,58 @@ protected KieSession restoreSession(String drl, PersistedSessionOption.Persisten } protected KieSession restoreSession(String drl, PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy, Option... options) { - return getKieSession(drl, PersistedSessionOption.fromSession(savedSessionId).withPersistenceStrategy(persistenceStrategy).withSafepointStrategy(safepointStrategy), options); + return restoreSession(persistedSessionId, drl, persistenceStrategy, safepointStrategy, options); } protected KieSession restoreSession(Long sessionId, String drl, PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy, Option... options) { - KieSession recoveredSession = getKieSession_m(drl, PersistedSessionOption.fromSession(sessionId).withPersistenceStrategy(persistenceStrategy).withSafepointStrategy(safepointStrategy), options); - this.sessions_m.add(recoveredSession); - return recoveredSession; + return getKieSession(drl, PersistedSessionOption.fromSession(sessionId).withPersistenceStrategy(persistenceStrategy).withSafepointStrategy(safepointStrategy), options); + } + + protected int fireAllRules() { + return fireAllRules(sessions.get(0)); + } + + protected int fireAllRules(KieSession session) { + return session.fireAllRules(); } protected void disposeSession() { + disposeSession(sessions.get(0)); + } + + protected void disposeSession(KieSession session) { session.dispose(); } - protected KieSession getKieSession(String drl, PersistedSessionOption persistedSessionOption, Option... options) { - OptionsFilter optionsFilter = new OptionsFilter(options); - KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(ExecutableModelProject.class, optionsFilter.getKieBaseOptions()); - KieSessionConfiguration conf = KieServices.get().newKieSessionConfiguration(); - if (persistedSessionOption != null) { - conf.setOption(persistedSessionOption); - safepointStrategy = persistedSessionOption.getSafepointStrategy(); - } - Stream.of(optionsFilter.getKieSessionOption()).forEach(conf::setOption); - session = kbase.newKieSession(conf, null); - if (persistedSessionOption == null || persistedSessionOption.isNewSession()) { - List results = new ArrayList<>(); - session.setGlobal("results", results); - } - return session; + protected SessionPseudoClock getSessionClock() { + return getSessionClock(sessions.get(0)); } - protected KieSession getKieSession_m(String drl, PersistedSessionOption persistedSessionOption, Option... options) { + protected SessionPseudoClock getSessionClock(KieSession session) { + return session.getSessionClock(); + } + + protected Collection getFactHandles() { + return getFactHandles(sessions.get(0)); + } + + protected Collection getFactHandles(KieSession session) { + return session.getFactHandles(); + } + + protected void safepoint() { + safepoint(sessions.get(0)); + } + + protected void safepoint(KieSession session) { + ((ReliableKieSession) session).safepoint(); + } + + protected long getSessionIdentifier() { + return sessions.get(0).getIdentifier(); + } + + protected KieSession getKieSession(String drl, PersistedSessionOption persistedSessionOption, Option... options) { OptionsFilter optionsFilter = new OptionsFilter(options); KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(ExecutableModelProject.class, optionsFilter.getKieBaseOptions()); KieSessionConfiguration conf = KieServices.get().newKieSessionConfiguration(); @@ -240,13 +289,19 @@ protected KieSession getKieSession_m(String drl, PersistedSessionOption persiste } Stream.of(optionsFilter.getKieSessionOption()).forEach(conf::setOption); KieSession session = kbase.newKieSession(conf, null); + sessions.add(session); if (persistedSessionOption == null || persistedSessionOption.isNewSession()) { List results = new ArrayList<>(); session.setGlobal("results", results); + persistedSessionId = session.getIdentifier(); } return session; } + protected Optional getPersonByName(String name) { + return getPersonByName(sessions.get(0), name); + } + protected Optional getPersonByName(KieSession kieSession, String name) { return kieSession.getObjects(new ClassObjectFilter(Person.class)) .stream() diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityUpdateInDrlTest.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityUpdateInDrlTest.java index 9edbc00f22e..6bb699c1c55 100644 --- a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityUpdateInDrlTest.java +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityUpdateInDrlTest.java @@ -49,12 +49,12 @@ void updateInRHS_insertFireFailoverFire_shouldMatchUpdatesFromFirstSession(Persi createSession(RULE_UPDATE, strategy); - insertString("M"); + insert("M"); insertMatchingPerson("Mike",22); insertNonMatchingPerson("Eleven", 17); - insertInteger(17); // person with age=17 will change to 18 (17+1) + insert(17); // person with age=17 will change to 18 (17+1) - assertThat(session.fireAllRules()).isEqualTo(2); // person with name that starts with M and has age>17 will be added to the results list + assertThat(fireAllRules()).isEqualTo(2); // person with name that starts with M and has age>17 will be added to the results list assertThat(getResults()).containsExactlyInAnyOrder(22); failover(); @@ -62,9 +62,9 @@ void updateInRHS_insertFireFailoverFire_shouldMatchUpdatesFromFirstSession(Persi restoreSession(RULE_UPDATE, strategy); clearResults(); - insertString("E"); // NonMatchingPerson will match rule X + insert("E"); // NonMatchingPerson will match rule X - assertThat(session.fireAllRules()).isEqualTo(1); + assertThat(fireAllRules()).isEqualTo(1); assertThat(getResults()).containsExactlyInAnyOrder(18); failover(); @@ -72,7 +72,7 @@ void updateInRHS_insertFireFailoverFire_shouldMatchUpdatesFromFirstSession(Persi restoreSession(RULE_UPDATE, strategy); clearResults(); - assertThat(session.fireAllRules()).isZero(); + assertThat(fireAllRules()).isZero(); assertThat(getResults()).isEmpty(); } diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/smoke/BaseSmokeTest.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/smoke/BaseSmokeTest.java index d7b4b1e9dab..d5fe0099eb1 100644 --- a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/smoke/BaseSmokeTest.java +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/smoke/BaseSmokeTest.java @@ -43,7 +43,7 @@ public class BaseSmokeTest extends ReliabilityTestBasics { void insertFailoverInsertFire_shouldRecoverFromFailover(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy) { createSession(BASIC_RULE, persistenceStrategy, safepointStrategy); - insertString("M"); + insert("M"); insertMatchingPerson("Matching Person One", 37); //-- Assume JVM down here. Fail-over to other JVM or rebooted JVM @@ -55,7 +55,7 @@ void insertFailoverInsertFire_shouldRecoverFromFailover(PersistedSessionOption.P insertNonMatchingPerson("Toshiya", 35); insertMatchingPerson("Matching Person Two", 40); - session.fireAllRules(); + fireAllRules(); assertThat(getResults()).containsExactlyInAnyOrder("Matching Person One", "Matching Person Two"); }