@ -140,14 +140,14 @@ public class KafkaStreamsTest {
}
}
@Test
@Test
public void testStateCloseAfterCreate ( ) {
public void stateShouldTransitToNotRunningIfCloseRightAfterCreated ( ) {
globalStreams . close ( ) ;
globalStreams . close ( ) ;
Assert . assertEquals ( KafkaStreams . State . NOT_RUNNING , globalStreams . state ( ) ) ;
Assert . assertEquals ( KafkaStreams . State . NOT_RUNNING , globalStreams . state ( ) ) ;
}
}
@Test
@Test
public void testStateOneThreadDeadButRebalanceFinish ( ) throws InterruptedException {
public void stateShouldTransitToRunningIfNonDeadThreadsBackToRunning ( ) throws InterruptedException {
final StateListenerStub stateListener = new StateListenerStub ( ) ;
final StateListenerStub stateListener = new StateListenerStub ( ) ;
globalStreams . setStateListener ( stateListener ) ;
globalStreams . setStateListener ( stateListener ) ;
@ -171,7 +171,7 @@ public class KafkaStreamsTest {
Assert . assertEquals ( 3 , stateListener . numChanges ) ;
Assert . assertEquals ( 3 , stateListener . numChanges ) ;
Assert . assertEquals ( KafkaStreams . State . REBALANCING , globalStreams . state ( ) ) ;
Assert . assertEquals ( KafkaStreams . State . REBALANCING , globalStreams . state ( ) ) ;
for ( final StreamThread thread : globalStreams . threads ) {
for ( final StreamThread thread : globalStreams . threads ) {
thread . stateListener ( ) . onChange (
thread . stateListener ( ) . onChange (
thread ,
thread ,
StreamThread . State . PARTITIONS_ASSIGNED ,
StreamThread . State . PARTITIONS_ASSIGNED ,
@ -194,7 +194,7 @@ public class KafkaStreamsTest {
Assert . assertEquals ( 3 , stateListener . numChanges ) ;
Assert . assertEquals ( 3 , stateListener . numChanges ) ;
Assert . assertEquals ( KafkaStreams . State . REBALANCING , globalStreams . state ( ) ) ;
Assert . assertEquals ( KafkaStreams . State . REBALANCING , globalStreams . state ( ) ) ;
for ( final StreamThread thread : globalStreams . threads ) {
for ( final StreamThread thread : globalStreams . threads ) {
if ( thread ! = globalStreams . threads [ NUM_THREADS - 1 ] ) {
if ( thread ! = globalStreams . threads [ NUM_THREADS - 1 ] ) {
thread . stateListener ( ) . onChange (
thread . stateListener ( ) . onChange (
thread ,
thread ,
@ -214,6 +214,70 @@ public class KafkaStreamsTest {
Assert . assertEquals ( KafkaStreams . State . NOT_RUNNING , globalStreams . state ( ) ) ;
Assert . assertEquals ( KafkaStreams . State . NOT_RUNNING , globalStreams . state ( ) ) ;
}
}
@Test
public void stateShouldTransitToErrorIfAllThreadsDead ( ) throws InterruptedException {
final StateListenerStub stateListener = new StateListenerStub ( ) ;
globalStreams . setStateListener ( stateListener ) ;
Assert . assertEquals ( 0 , stateListener . numChanges ) ;
Assert . assertEquals ( KafkaStreams . State . CREATED , globalStreams . state ( ) ) ;
globalStreams . start ( ) ;
TestUtils . waitForCondition (
( ) - > stateListener . numChanges = = 2 ,
"Streams never started." ) ;
Assert . assertEquals ( KafkaStreams . State . RUNNING , globalStreams . state ( ) ) ;
for ( final StreamThread thread : globalStreams . threads ) {
thread . stateListener ( ) . onChange (
thread ,
StreamThread . State . PARTITIONS_REVOKED ,
StreamThread . State . RUNNING ) ;
}
Assert . assertEquals ( 3 , stateListener . numChanges ) ;
Assert . assertEquals ( KafkaStreams . State . REBALANCING , globalStreams . state ( ) ) ;
globalStreams . threads [ NUM_THREADS - 1 ] . stateListener ( ) . onChange (
globalStreams . threads [ NUM_THREADS - 1 ] ,
StreamThread . State . PENDING_SHUTDOWN ,
StreamThread . State . PARTITIONS_REVOKED ) ;
globalStreams . threads [ NUM_THREADS - 1 ] . stateListener ( ) . onChange (
globalStreams . threads [ NUM_THREADS - 1 ] ,
StreamThread . State . DEAD ,
StreamThread . State . PENDING_SHUTDOWN ) ;
Assert . assertEquals ( 3 , stateListener . numChanges ) ;
Assert . assertEquals ( KafkaStreams . State . REBALANCING , globalStreams . state ( ) ) ;
for ( final StreamThread thread : globalStreams . threads ) {
if ( thread ! = globalStreams . threads [ NUM_THREADS - 1 ] ) {
thread . stateListener ( ) . onChange (
thread ,
StreamThread . State . PENDING_SHUTDOWN ,
StreamThread . State . PARTITIONS_REVOKED ) ;
thread . stateListener ( ) . onChange (
thread ,
StreamThread . State . DEAD ,
StreamThread . State . PENDING_SHUTDOWN ) ;
}
}
Assert . assertEquals ( 4 , stateListener . numChanges ) ;
Assert . assertEquals ( KafkaStreams . State . ERROR , globalStreams . state ( ) ) ;
globalStreams . close ( ) ;
// the state should not stuck with ERROR, but transit to NOT_RUNNING in the end
TestUtils . waitForCondition (
( ) - > stateListener . numChanges = = 6 ,
"Streams never closed." ) ;
Assert . assertEquals ( KafkaStreams . State . NOT_RUNNING , globalStreams . state ( ) ) ;
}
@Test
@Test
public void shouldCleanupResourcesOnCloseWithoutPreviousStart ( ) throws Exception {
public void shouldCleanupResourcesOnCloseWithoutPreviousStart ( ) throws Exception {
builder . globalTable ( "anyTopic" ) ;
builder . globalTable ( "anyTopic" ) ;