Skip to content

Commit

Permalink
cache state, only recreate when new resource is set (#613)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Jun 30, 2023
1 parent 6437bb8 commit 6de8d4e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ open class EditorResource(
&& !areEqual(new, existing)) {
this.resource = new
}
setState(null) // reset state
}
}

Expand Down Expand Up @@ -94,13 +95,18 @@ open class EditorResource(
*/
fun getState(): EditorResourceState {
return resourceChangeMutex.withLock {
val state = getState(resource, state)
this.state = state
state
val existingState = this.state
if (existingState == null) {
val newState = createState(resource, null)
setState(newState)
newState
} else {
existingState
}
}
}

private fun getState(resource: HasMetadata, existingState: EditorResourceState?): EditorResourceState {
private fun createState(resource: HasMetadata, existingState: EditorResourceState?): EditorResourceState {
val isModified = isModified(resource)
return when {
!isConnected() ->
Expand All @@ -122,12 +128,14 @@ open class EditorResource(
isOutdatedVersion()
)

existingState is Error ->
existingState

isOutdatedVersion() ->
Outdated()

existingState is Pulled
|| existingState is Pushed
|| existingState is Error ->
|| existingState is Pushed ->
existingState

!existsOnCluster() ->
Expand All @@ -149,7 +157,7 @@ open class EditorResource(
* Store resource that we tried to push but failed.
* In this way this resource is not in modified state anymore
* @see isModified
* @see getState(resource: HasMetadata, existingState: EditorResourceState?)
* @see createState(resource: HasMetadata, existingState: EditorResourceState?)
*/
setLastPushedPulled(resource)
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ class EditorResourceTest {
val error = Error("oh my!")
editorResource.setState(error)
editorResource.setLastPushedPulled(POD3) // modified = (current resource != lastPushedPulled)
editorResource.setResource(POD2) // cause state to be recreated
// when
val state = editorResource.getState()
// then
Expand Down Expand Up @@ -403,6 +404,10 @@ class EditorResourceTest {
@Test
fun `#getState should return Modified if resource is modified when compared to last pulled pushed resource`() {
// given
doReturn(true)
.whenever(clusterResource).isSupported()
doReturn(true) // don't create modified state because it doesnt exist on cluster
.whenever(clusterResource).exists()
val editorResource = createEditorResource(POD2)
val modified = PodBuilder(POD2)
.editMetadata()
Expand All @@ -411,10 +416,6 @@ class EditorResourceTest {
.build()
editorResource.setResource(modified)
editorResource.setLastPushedPulled(POD2)
doReturn(true)
.whenever(clusterResource).isSupported()
doReturn(true) // don't create modified state because it doesnt exist on cluster
.whenever(clusterResource).exists()
// when
val state = editorResource.getState()
// then
Expand All @@ -438,6 +439,24 @@ class EditorResourceTest {
assertThat(state).isInstanceOf(Outdated::class.java)
}

@Test
fun `#getState should return Error if resource is outdated but in error`() {
// given
val editorResource = createEditorResource(POD2)
doReturn(true)
.whenever(clusterResource).isSupported()
doReturn(true)
.whenever(clusterResource).isOutdatedVersion(any())
doReturn(true) // don't return modified state because it doesnt exist
.whenever(clusterResource).exists()
editorResource.setLastPushedPulled(POD2) // don't return modified because last pulled pushed is different
editorResource.setState(mock<Error>())
// when
val state = editorResource.getState()
// then
assertThat(state).isInstanceOf(Error::class.java)
}

@Test
fun `#dispose should close clusterResource that was created`() {
// given
Expand Down

0 comments on commit 6de8d4e

Please sign in to comment.