Skip to content

cache state, only recreate when new resource is set (#613) #624

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

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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