Skip to content

Commit

Permalink
rework drools-reliability tests to make them work seamlessly with one…
Browse files Browse the repository at this point in the history
… or more sessions (#5330)
  • Loading branch information
mariofusco authored Jun 19, 2023
1 parent 966e4c3 commit 9de8d0b
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

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;
import org.junit.jupiter.params.provider.MethodSource;
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;

Expand Down Expand Up @@ -54,7 +54,7 @@ void removeAllSessionCaches_shouldRemoveAllSessionCachesEvenAfterFailover(Persis

restoreSession(EMPTY_RULE, strategy); // restored but no objects in the cache

Optional<Person> toshiya = getPersonByName(session, "Toshiya");
Optional<Person> toshiya = getPersonByName("Toshiya");
assertThat(toshiya).isEmpty(); // So cannot recover the fact
}

Expand All @@ -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
Expand All @@ -82,27 +83,27 @@ void missingDispose_shouldNotReuseOrphanedCache(PersistedSessionOption.Persisten

createSession(EMPTY_RULE, strategy); // new session. If sessionId = 0, it will potentially reuse the orphaned cache

Optional<Person> toshiya = getPersonByName(session, "Toshiya");
Optional<Person> 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
}

@ParameterizedTest
@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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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);
}
}
Loading

0 comments on commit 9de8d0b

Please sign in to comment.