-
Notifications
You must be signed in to change notification settings - Fork 14
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
Increase code coverage (pt2). #389
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package com.instacart.formula.internal | |
import com.instacart.formula.FormulaPlugins | ||
import com.instacart.formula.IFormula | ||
import com.instacart.formula.plugin.Inspector | ||
import java.lang.IllegalStateException | ||
|
||
/** | ||
* Keeps track of child formula managers. | ||
|
@@ -12,7 +11,7 @@ internal class ChildrenManager( | |
private val delegate: FormulaManagerImpl<*, *, *>, | ||
private val inspector: Inspector?, | ||
) { | ||
private var children: SingleRequestMap<Any, FormulaManager<*, *>>? = null | ||
private val children: SingleRequestMap<Any, FormulaManager<*, *>> = LinkedHashMap() | ||
private var indexes: MutableMap<Any, Int>? = null | ||
private var pendingRemoval: MutableList<FormulaManager<*, *>>? = null | ||
|
||
|
@@ -26,7 +25,7 @@ internal class ChildrenManager( | |
fun prepareForPostEvaluation() { | ||
indexes?.clear() | ||
|
||
children?.clearUnrequested(this::prepareForTermination) | ||
children.clearUnrequested(this::prepareForTermination) | ||
} | ||
|
||
fun terminateChildren(evaluationId: Long): Boolean { | ||
|
@@ -42,20 +41,20 @@ internal class ChildrenManager( | |
} | ||
|
||
fun markAsTerminated() { | ||
children?.forEachValue { it.markAsTerminated() } | ||
children.forEachValue { it.markAsTerminated() } | ||
} | ||
|
||
fun performTerminationSideEffects(executeTransitionQueue: Boolean) { | ||
children?.forEachValue { it.performTerminationSideEffects(executeTransitionQueue) } | ||
children.forEachValue { it.performTerminationSideEffects(executeTransitionQueue) } | ||
} | ||
|
||
fun <ChildInput, ChildOutput> findOrInitChild( | ||
key: Any, | ||
formula: IFormula<ChildInput, ChildOutput>, | ||
input: ChildInput, | ||
): FormulaManager<ChildInput, ChildOutput> { | ||
val childHolder = childFormulaHolder(key, formula, input) | ||
return if (childHolder.requested) { | ||
val childManagerEntry = getOrInitChildManager(key, formula, input) | ||
return if (childManagerEntry.requested) { | ||
val logs = duplicateKeyLogs ?: run { | ||
val newSet = mutableSetOf<Any>() | ||
duplicateKeyLogs = newSet | ||
|
@@ -76,38 +75,27 @@ internal class ChildrenManager( | |
) | ||
} | ||
|
||
if (key is IndexedKey) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing this as this will never happen |
||
// This should never happen, but added as safety | ||
throw IllegalStateException("Key already indexed (and still duplicate).") | ||
} | ||
|
||
val index = nextIndex(key) | ||
val indexedKey = IndexedKey(key, index) | ||
findOrInitChild(indexedKey, formula, input) | ||
} else { | ||
childHolder.requestAccess { | ||
"There already is a child with same key: $key. Override [Formula.key] function." | ||
} | ||
childManagerEntry.requested = true | ||
childManagerEntry.value | ||
} | ||
} | ||
|
||
private fun prepareForTermination(it: FormulaManager<*, *>) { | ||
pendingRemoval = pendingRemoval ?: mutableListOf() | ||
val list = pendingRemoval ?: mutableListOf() | ||
pendingRemoval = list | ||
it.markAsTerminated() | ||
pendingRemoval?.add(it) | ||
list.add(it) | ||
} | ||
|
||
private fun <ChildInput, ChildOutput> childFormulaHolder( | ||
private fun <ChildInput, ChildOutput> getOrInitChildManager( | ||
key: Any, | ||
formula: IFormula<ChildInput, ChildOutput>, | ||
input: ChildInput, | ||
): SingleRequestHolder<FormulaManager<ChildInput, ChildOutput>> { | ||
val children = children ?: run { | ||
val initialized: SingleRequestMap<Any, FormulaManager<*, *>> = LinkedHashMap() | ||
this.children = initialized | ||
initialized | ||
} | ||
|
||
val childFormulaHolder = children.findOrInit(key) { | ||
val implementation = formula.implementation | ||
FormulaManagerImpl( | ||
|
@@ -136,7 +124,12 @@ internal class ChildrenManager( | |
initialized | ||
} | ||
|
||
val index = indexes.getOrElse(key) { 0 } + 1 | ||
val previousIndex = indexes[key] | ||
val index = if (previousIndex == null) { | ||
0 | ||
} else { | ||
previousIndex + 1 | ||
} | ||
indexes[key] = index | ||
return index | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,20 @@ | ||
package com.instacart.formula.internal | ||
|
||
import java.lang.IllegalStateException | ||
|
||
internal class Listeners { | ||
private var listeners: SingleRequestMap<Any, ListenerImpl<*, *, *>>? = null | ||
private var indexes: MutableMap<Any, Int>? = null | ||
|
||
private fun duplicateKeyErrorMessage(key: Any): String { | ||
return "Listener $key is already defined. Unexpected issue." | ||
} | ||
|
||
fun <Input, State, Event> initOrFindListener(key: Any, useIndex: Boolean): ListenerImpl<Input, State, Event> { | ||
val currentHolder = listenerHolder<Input, State, Event>(key) | ||
return if (currentHolder.requested && useIndex) { | ||
if (key is IndexedKey) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't happen so no need to check |
||
// This should never happen, but added as safety | ||
throw IllegalStateException("Key already indexed (and still duplicate).") | ||
} | ||
|
||
return if (!currentHolder.requested) { | ||
currentHolder.requested = true | ||
currentHolder.value as ListenerImpl<Input, State, Event> | ||
} else if (useIndex) { | ||
val index = nextIndex(key) | ||
val indexedKey = IndexedKey(key, index) | ||
initOrFindListener(indexedKey, useIndex) | ||
} else { | ||
currentHolder | ||
.requestAccess { | ||
duplicateKeyErrorMessage(currentHolder.value.key) | ||
} as ListenerImpl<Input, State, Event> | ||
throw IllegalStateException("Listener $key is already defined. Unexpected issue.") | ||
} | ||
} | ||
|
||
|
@@ -61,7 +50,12 @@ internal class Listeners { | |
initialized | ||
} | ||
|
||
val index = indexes.getOrElse(key) { 0 } + 1 | ||
val previousIndex = indexes[key] | ||
val index = if (previousIndex == null) { | ||
0 | ||
} else { | ||
previousIndex + 1 | ||
} | ||
indexes[key] = index | ||
return index | ||
} | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to optimize
children
map initialization asFormulaManager
optimizes this already