-
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.
[Bugfix] Fix old listener crash. (#310)
* Adding tests to replicate a crash. * [Bugfix] Fix old listener crash.
- Loading branch information
Showing
5 changed files
with
260 additions
and
0 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
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
120 changes: 120 additions & 0 deletions
120
formula/src/test/java/com/instacart/formula/subjects/MultiChildIndirectStateChangeRobot.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,120 @@ | ||
package com.instacart.formula.subjects | ||
|
||
import com.instacart.formula.Evaluation | ||
import com.instacart.formula.Formula | ||
import com.instacart.formula.Snapshot | ||
import com.instacart.formula.rxjava3.RxAction | ||
import com.instacart.formula.test.TestableRuntime | ||
import io.reactivex.rxjava3.core.Observable | ||
|
||
class MultiChildIndirectStateChangeRobot(runtime: TestableRuntime) { | ||
val subject = runtime.test(Parent()) | ||
|
||
fun start() = apply { | ||
subject.input(Unit) | ||
} | ||
|
||
class Child : Formula<Child.Input, Child.State, Child.Output>() { | ||
data class Input( | ||
val preAction: () -> Unit = {}, | ||
) | ||
|
||
data class State( | ||
val actionId: Int = 0, | ||
val value: Int = 0, | ||
) | ||
|
||
data class Output( | ||
val value: Int, | ||
val onChildAction: () -> Unit, | ||
) | ||
|
||
override fun initialState(input: Input): State = State() | ||
|
||
override fun Snapshot<Input, State>.evaluate(): Evaluation<Output> { | ||
return Evaluation( | ||
output = Output( | ||
value = state.value, | ||
onChildAction = context.callback { | ||
val newState = state.copy(actionId = state.actionId + 1) | ||
transition(newState) | ||
} | ||
), | ||
actions = context.actions { | ||
if (state.actionId > 0) { | ||
RxAction.fromObservable(state.actionId) { | ||
input.preAction() | ||
|
||
// Increment two times | ||
Observable.just(1, 1) | ||
}.onEvent { | ||
val newState = state.copy(value = state.value + it) | ||
transition(newState) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} | ||
|
||
class Parent : Formula<Unit, Parent.State, Parent.Output>() { | ||
data class State( | ||
val actionId: Int = 0, | ||
val value: Int = 0, | ||
) | ||
|
||
data class Output( | ||
val parentValue: Int, | ||
val childValue: Int, | ||
val onAction: () -> Unit | ||
) | ||
|
||
private val incrementFormula = Child() | ||
|
||
override fun initialState(input: Unit): State = State() | ||
|
||
override fun Snapshot<Unit, State>.evaluate(): Evaluation<Output> { | ||
val next = context.callback { | ||
val newState = state.copy(actionId = state.actionId + 1) | ||
transition(newState) | ||
} | ||
|
||
val firstChild = context.key("first") { | ||
context.child(incrementFormula, Child.Input()) | ||
} | ||
|
||
val secondChild = context.key("second") { | ||
context.child( | ||
formula = incrementFormula, | ||
input = Child.Input(firstChild.onChildAction), | ||
) | ||
} | ||
|
||
return Evaluation( | ||
output = Output( | ||
parentValue = state.value, | ||
childValue = secondChild.value, | ||
onAction = context.callback { | ||
transition { | ||
next() | ||
} | ||
} | ||
), | ||
actions = context.actions { | ||
if (state.actionId > 0) { | ||
RxAction.fromObservable(state.actionId) { | ||
// Call child | ||
secondChild.onChildAction() | ||
|
||
// Emit events | ||
Observable.just(1, 1, 1) | ||
}.onEvent { | ||
val newState = state.copy(value = state.value + it) | ||
transition(newState) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
formula/src/test/java/com/instacart/formula/subjects/NestedCallbackCallRobot.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,39 @@ | ||
package com.instacart.formula.subjects | ||
|
||
import com.instacart.formula.Evaluation | ||
import com.instacart.formula.Formula | ||
import com.instacart.formula.Snapshot | ||
import com.instacart.formula.test.TestableRuntime | ||
|
||
class NestedCallbackCallRobot(runtime: TestableRuntime) { | ||
val subject = runtime.test(Parent()) | ||
|
||
fun start() = apply { | ||
subject.input(Unit) | ||
} | ||
|
||
class Parent : Formula<Unit, Int, Parent.Output>() { | ||
data class Output( | ||
val value: Int, | ||
val onAction: () -> Unit, | ||
) | ||
|
||
override fun initialState(input: Unit): Int = 0 | ||
|
||
override fun Snapshot<Unit, Int>.evaluate(): Evaluation<Output> { | ||
val next = context.callback { | ||
transition(state + 1) | ||
} | ||
return Evaluation( | ||
output = Output( | ||
value = state, | ||
onAction = context.callback { | ||
transition { | ||
next() | ||
} | ||
} | ||
) | ||
) | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
formula/src/test/java/com/instacart/formula/subjects/ParentUpdateChildAndSelfOnEventRobot.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,65 @@ | ||
package com.instacart.formula.subjects | ||
|
||
import com.instacart.formula.Evaluation | ||
import com.instacart.formula.Formula | ||
import com.instacart.formula.Snapshot | ||
import com.instacart.formula.rxjava3.RxAction | ||
import com.instacart.formula.test.TestableRuntime | ||
import com.instacart.formula.types.IncrementFormula | ||
import io.reactivex.rxjava3.core.Observable | ||
|
||
class ParentUpdateChildAndSelfOnEventRobot( | ||
runtime: TestableRuntime, | ||
) { | ||
|
||
val subject = runtime.test(Parent()) | ||
|
||
fun start() = apply { | ||
subject.input(Unit) | ||
} | ||
|
||
class Parent : Formula<Unit, Parent.State, Parent.Output>() { | ||
data class State( | ||
val actionId: Int = 0, | ||
val value: Int = 0, | ||
) | ||
|
||
data class Output( | ||
val parentValue: Int, | ||
val childValue: Int, | ||
val onAction: () -> Unit | ||
) | ||
|
||
private val incrementFormula = IncrementFormula() | ||
|
||
override fun initialState(input: Unit): State = State() | ||
|
||
override fun Snapshot<Unit, State>.evaluate(): Evaluation<Output> { | ||
val incrementOutput = context.child(incrementFormula) | ||
return Evaluation( | ||
output = Output( | ||
parentValue = state.value, | ||
childValue = incrementOutput.value, | ||
onAction = context.callback { | ||
val newState = state.copy(actionId = state.actionId + 1) | ||
transition(newState) | ||
} | ||
), | ||
actions = context.actions { | ||
if (state.actionId > 0) { | ||
RxAction.fromObservable(state.actionId) { | ||
// Call child | ||
incrementOutput.onIncrement() | ||
|
||
// Emit events | ||
Observable.just(1, 1, 1) | ||
}.onEvent { | ||
val newState = state.copy(value = state.value + it) | ||
transition(newState) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} |