Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TestFormula and key logic. #379

Merged
merged 3 commits into from
Aug 26, 2024
Merged

Conversation

Laimiux
Copy link
Collaborator

@Laimiux Laimiux commented Aug 23, 2024

There are a couple of changes around this PR

  • Making TestFormula into a final class constructed via testFormula extension.
  • Fixing issues with TestFormula key resolution
  • Adding the ability to get all inputs instead of only the most recent one.
  • Documentation around TestFormula usage.
  • Moving some of the internal test formula logic into RunningInstanceManager

import com.instacart.formula.Snapshot
import java.util.concurrent.atomic.AtomicLong

/**
* Test formula is used to provide a fake formula implementation. It allows you to [send][output]
* output updates and [inspect/interact][input] with input.
*/
abstract class TestFormula<Input, Output> :
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having both inheritance and factory approaches creates inconsistencies in the API. I've decided to make this a final class and keep the following API as the primary approach

class FakeUserFormula : UserFormula {
     override val implementation = testFormula(
         initialOutput = User(
             name = "Fake user name",
         )
     )
 }

@carrotkite
Copy link

carrotkite commented Aug 23, 2024

JaCoCo Code Coverage 88.01% ✅

Class Covered Meta Status
com/instacart/formula/test/utils/RunningInstanceManager 91% 0%
com/instacart/formula/StatelessFormula 100% 0%
com/instacart/formula/test/TestFormula 100% 0%

Generated by 🚫 Danger

* override fun key(input: ItemInput) = input.itemId
* ```
*/
override fun key(input: Input): Any? = null
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed since the default implementation uses implementation.key() which is null.

@Laimiux Laimiux force-pushed the laimonas/refactor-key-logic branch 6 times, most recently from b400b66 to c2bd013 Compare August 23, 2024 13:01
@Laimiux Laimiux force-pushed the laimonas/refactor-key-logic branch from c2bd013 to c826fc2 Compare August 23, 2024 13:21
private val stateMap = mutableMapOf<Any?, Value<Input, Output>>()

abstract fun initialOutput(): Output
private val runningInstanceManager = RunningInstanceManager<Input, Output>(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving the logic to keep track of individual formulas state to this class

return requireNotNull(stateMap.values.lastOrNull()) {
"Formula is not running"
}
fun mostRecentInputs(): List<Input> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the ability to get a list of inputs instead of the most recent one.

* output updates and [inspect/interact][input] with input.
* Test formula is used to replace a real formula with a fake implementation. This allows you to:
* - Verify that parent passes correct inputs. Take a look at [input].
* - Verify that parent deals with output changes correctly. Take a look at [output]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also call out that we support the running count? This is a handy feature that I think a lot of devs don't know about.

@Laimiux Laimiux merged commit ff581b2 into master Aug 26, 2024
4 checks passed
@Laimiux Laimiux deleted the laimonas/refactor-key-logic branch August 26, 2024 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants