-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
209 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
formula/src/test/java/com/instacart/formula/DirectRuntimeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.instacart.formula | ||
|
||
import com.google.common.truth.Truth | ||
import com.google.common.truth.Truth.assertThat | ||
import com.instacart.formula.internal.Try | ||
import com.instacart.formula.test.TestEventCallback | ||
import com.instacart.formula.types.InputIdentityFormula | ||
import org.junit.Test | ||
|
||
/** | ||
* [FormulaRuntimeTest] runs both `toObservable` and `toFlow` internally to ensure that both | ||
* implementations function identically. This test is used to capture various edge-cases | ||
* within [FormulaRuntime] that are not possible via indirect tests. | ||
*/ | ||
class DirectRuntimeTest { | ||
|
||
@Test fun `requireInput will throw illegal state exception if it is null`() { | ||
val root = InputIdentityFormula<Int>() | ||
|
||
val onOutput = TestEventCallback<Int>() | ||
val onError = TestEventCallback<Throwable>() | ||
val runtime = FormulaRuntime( | ||
formula = root, | ||
onOutput = onOutput, | ||
onError = onError, | ||
config = RuntimeConfig() | ||
) | ||
|
||
val result = Try { | ||
runtime.requireInput() | ||
} | ||
assertThat(result.errorOrNull()).isInstanceOf(IllegalStateException::class.java) | ||
} | ||
|
||
@Test fun `requireManager will throw illegal state exception if it is null`() { | ||
val root = InputIdentityFormula<Int>() | ||
|
||
val onOutput = TestEventCallback<Int>() | ||
val onError = TestEventCallback<Throwable>() | ||
val runtime = FormulaRuntime( | ||
formula = root, | ||
onOutput = onOutput, | ||
onError = onError, | ||
config = RuntimeConfig() | ||
) | ||
|
||
val result = Try { | ||
runtime.requireManager() | ||
} | ||
assertThat(result.errorOrNull()).isInstanceOf(IllegalStateException::class.java) | ||
} | ||
|
||
@Test | ||
fun `input change when runtime is terminated does nothing`() { | ||
val root = InputIdentityFormula<Int>() | ||
|
||
val onOutput = TestEventCallback<Int>() | ||
val onError = TestEventCallback<Throwable>() | ||
val runtime = FormulaRuntime( | ||
formula = root, | ||
onOutput = onOutput, | ||
onError = onError, | ||
config = RuntimeConfig() | ||
) | ||
|
||
runtime.onInput(0) | ||
runtime.terminate() | ||
runtime.onInput(1) | ||
|
||
assertThat(onOutput.values()).containsExactly(0).inOrder() | ||
} | ||
|
||
@Test fun `it is safe to call terminate before first input initializes formula`() { | ||
val root = InputIdentityFormula<Int>() | ||
|
||
val onOutput = TestEventCallback<Int>() | ||
val onError = TestEventCallback<Throwable>() | ||
val runtime = FormulaRuntime( | ||
formula = root, | ||
onOutput = onOutput, | ||
onError = onError, | ||
config = RuntimeConfig() | ||
) | ||
|
||
runtime.terminate() | ||
} | ||
|
||
@Test fun `it is safe to call terminate multiple times`() { | ||
val root = InputIdentityFormula<Int>() | ||
|
||
val onOutput = TestEventCallback<Int>() | ||
val onError = TestEventCallback<Throwable>() | ||
val runtime = FormulaRuntime( | ||
formula = root, | ||
onOutput = onOutput, | ||
onError = onError, | ||
config = RuntimeConfig() | ||
) | ||
|
||
runtime.onInput(0) | ||
runtime.terminate() | ||
runtime.terminate() | ||
runtime.terminate() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters