Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
/ FlowRecorder Public archive

A utility class to make it easier to test emissions on kotlin's Flow Type over time

License

Notifications You must be signed in to change notification settings

freeletics/FlowRecorder

Repository files navigation

Deprecated

We've stopped maintaining this library. Check out Turbine as an alternative.

Flow Emission Recorder

This library helps to record emissions of kotlin Flow type over time. No need to use runTestBlocking { ... } or apply any synchronized dispatchers.

What this library will do is:

  1. Launch your Flow that you want to test.

  2. Keep recording all emissions of the Flow under test.

  3. If it takes some time to get the emissions because the Flow runs async operations that take some time to execute, the test will stop and wait for the emission to come (or until a timeout happens).

    @Test
    fun shouldEmit_1_2_3(){
        val flow = flow {
            emit(1)
            emit(2)
            delay(1000)
            emit(3)
        }
        
        val emissions = flow.record() // launches the flow and starts recording
        
        emissions shouldEmitNext 1
        emissions shouldEmitNext 2
        emissions shouldEmitNext 3 // although this emission happens later, the test execution will wait here until next emission (or fails with timeout)
    
        emissions.stopRecordingAndCleanUp() // releases resources to avoid OutOfMemoryExceptions
    }
  4. But it works also the other way around: if the emissions are faster than the test thread for whatever reason:

    @Test
    fun shouldEmit_1_2_3(){
        val flow = flow {
            emit(1)
            emit(2)
            emit(3)
        }
        
        val emissions = flow.record() // launches the flow and starts recording
        
        emissions shouldEmitNext 1
        // for whatever reason test thread would take longer, 
        // we don't miss any emissions that happened async. in the meantime. 
        Thread.sleep(1000)
        emissions shouldEmitNext 2
        emissions shouldEmitNext 3
    
        emissions.stopRecordingAndCleanUp() // releases resources to avoid OutOfMemoryExceptions
    }

Customization

@Test
fun shouldEmit_1_2_3(){
    val flow = flow {
        emit(1)
        emit(2)
        emit(3)
    }
    
    val emissions = flow.record(
        emissionTimeoutMilliseconds = 10_000, // specify timeout
        coroutineScopeToLaunchFlowIn = MyCoroutineScope // specify scope to launch Flow in
    )
    
    emissions.shouldEmitNext(1,2,3) // you can also use this instead of infix
    
    emissions.stopRecordingAndCleanUp() // releases resources to avoid OutOfMemoryExceptions
}

Multiplatform

Coming soon.

Get it

On maven central:

implementation 'com.freeletics.flow.test:recorder:0.2.1'

Snapshot

Latest development snapshot (whatever is on master is published as snapshot):

implementation 'com.freeletics.flow.test:recorder:0.2.2-SNAPSHOT'

About

A utility class to make it easier to test emissions on kotlin's Flow Type over time

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages