-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
216 additions
and
27 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
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/io/github/gradlenexus/publishplugin/NexusPublishExceptions.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,26 @@ | ||
/* | ||
* Copyright 2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.github.gradlenexus.publishplugin | ||
|
||
import java.lang.RuntimeException | ||
|
||
open class NexusPublishException : RuntimeException { | ||
constructor(message: String) : super(message) | ||
constructor(message: String, cause: Throwable) : super(message, cause) | ||
} | ||
|
||
open class RepositoryTransitionException(message: String) : NexusPublishException(message) |
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
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/io/github/gradlenexus/publishplugin/internal/ActionRetrier.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,21 @@ | ||
/* | ||
* Copyright 2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.github.gradlenexus.publishplugin.internal | ||
|
||
interface ActionRetrier<R> { | ||
fun execute(operationToExecuteWithRetrying: () -> R): R | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/kotlin/io/github/gradlenexus/publishplugin/internal/BasicActionRetrier.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,41 @@ | ||
/* | ||
* Copyright 2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.github.gradlenexus.publishplugin.internal | ||
|
||
import io.github.alexo.retrier.Retrier | ||
import io.github.gradlenexus.publishplugin.RetryingConfig | ||
import io.github.gradlenexus.publishplugin.StagingRepository | ||
|
||
open class BasicActionRetrier<R>(retryingConfig: RetryingConfig, stopFunction: (R) -> Boolean) : ActionRetrier<R> { | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
private val retrier: Retrier = Retrier.Builder() | ||
.withWaitStrategy(Retrier.Strategies.waitConstantly(retryingConfig.delayBetween.get().toMillis())) | ||
.withStopStrategy(Retrier.Strategies.stopAfter(retryingConfig.maxNumber.get())) | ||
.withResultRetryStrategy(stopFunction as ((Any) -> Boolean)) | ||
.build() | ||
|
||
override fun execute(operationToExecuteWithRetrying: () -> R): R { | ||
return retrier.execute(operationToExecuteWithRetrying) | ||
} | ||
|
||
companion object { | ||
fun retryUntilRepoTransitionIsCompletedRetrier(retryingConfig: RetryingConfig): BasicActionRetrier<StagingRepository> { | ||
return BasicActionRetrier(retryingConfig) { repo: StagingRepository -> repo.transitioning } | ||
} | ||
} | ||
} |
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
105 changes: 105 additions & 0 deletions
105
.../kotlin/io/github/gradlenexus/publishplugin/internal/StagingRepositoryTransitionerTest.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,105 @@ | ||
/* | ||
* Copyright 2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.github.gradlenexus.publishplugin.internal | ||
|
||
import com.nhaarman.mockitokotlin2.anyOrNull | ||
import io.github.gradlenexus.publishplugin.RepositoryTransitionException | ||
import io.github.gradlenexus.publishplugin.StagingRepository | ||
import org.assertj.core.api.Assertions.assertThatExceptionOfType | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.mockito.BDDMockito.given | ||
import org.mockito.Mock | ||
import org.mockito.Mockito.inOrder | ||
import org.mockito.invocation.InvocationOnMock | ||
import org.mockito.junit.jupiter.MockitoExtension | ||
|
||
@ExtendWith(MockitoExtension::class) | ||
internal class StagingRepositoryTransitionerTest { | ||
|
||
companion object { | ||
private const val TEST_STAGING_REPO_ID = "orgexample-42" | ||
} | ||
|
||
@Mock | ||
private lateinit var nexusClient: NexusClient | ||
@Mock | ||
private lateinit var retrier: ActionRetrier<StagingRepository> | ||
|
||
private lateinit var transitioner: StagingRepositoryTransitioner | ||
|
||
@BeforeEach | ||
internal fun setUp() { | ||
transitioner = StagingRepositoryTransitioner(nexusClient, retrier) | ||
} | ||
|
||
@Test | ||
internal fun `request repository close and get its state after execution by retrier`() { | ||
given(nexusClient.getStagingRepositoryStateById(TEST_STAGING_REPO_ID)) | ||
.willReturn(StagingRepository(TEST_STAGING_REPO_ID, StagingRepository.State.CLOSED, false)) | ||
given(retrier.execute(anyOrNull())).willAnswer(executeFunctionPassedAsFirstArgument()) | ||
|
||
transitioner.effectivelyClose(TEST_STAGING_REPO_ID) | ||
|
||
val inOrder = inOrder(nexusClient, retrier) | ||
inOrder.verify(nexusClient).closeStagingRepository(TEST_STAGING_REPO_ID) | ||
inOrder.verify(nexusClient).getStagingRepositoryStateById(TEST_STAGING_REPO_ID) | ||
} | ||
|
||
@Test | ||
internal fun `request release repository and get its state after execution by retrier`() { | ||
given(nexusClient.getStagingRepositoryStateById(TEST_STAGING_REPO_ID)) | ||
.willReturn(StagingRepository(TEST_STAGING_REPO_ID, StagingRepository.State.NOT_FOUND, false)) | ||
given(retrier.execute(anyOrNull())).willAnswer(executeFunctionPassedAsFirstArgument()) | ||
|
||
transitioner.effectivelyRelease(TEST_STAGING_REPO_ID) | ||
|
||
val inOrder = inOrder(nexusClient, retrier) | ||
inOrder.verify(nexusClient).releaseStagingRepository(TEST_STAGING_REPO_ID) | ||
inOrder.verify(nexusClient).getStagingRepositoryStateById(TEST_STAGING_REPO_ID) | ||
} | ||
|
||
@Test | ||
internal fun `throw meaningful exception on repository still in transition on close`() { | ||
given(nexusClient.getStagingRepositoryStateById(TEST_STAGING_REPO_ID)) | ||
.willReturn(StagingRepository(TEST_STAGING_REPO_ID, StagingRepository.State.CLOSED, true)) | ||
given(retrier.execute(anyOrNull())).willAnswer(executeFunctionPassedAsFirstArgument()) | ||
|
||
assertThatExceptionOfType(RepositoryTransitionException::class.java) | ||
.isThrownBy { transitioner.effectivelyClose(TEST_STAGING_REPO_ID) } | ||
.withMessageContainingAll(TEST_STAGING_REPO_ID, "transitioning=true") | ||
} | ||
|
||
@Test | ||
internal fun `throw meaningful exception on repository still in wrong state on close`() { | ||
given(nexusClient.getStagingRepositoryStateById(TEST_STAGING_REPO_ID)) | ||
.willReturn(StagingRepository(TEST_STAGING_REPO_ID, StagingRepository.State.OPEN, false)) | ||
given(retrier.execute(anyOrNull())).willAnswer(executeFunctionPassedAsFirstArgument()) | ||
|
||
assertThatExceptionOfType(RepositoryTransitionException::class.java) | ||
.isThrownBy { transitioner.effectivelyClose(TEST_STAGING_REPO_ID) } | ||
.withMessageContainingAll(TEST_STAGING_REPO_ID, StagingRepository.State.OPEN.toString(), StagingRepository.State.CLOSED.toString()) | ||
} | ||
|
||
private fun executeFunctionPassedAsFirstArgument(): (InvocationOnMock) -> StagingRepository { | ||
return { invocation: InvocationOnMock -> | ||
val passedFunction: () -> StagingRepository = invocation.getArgument(0) | ||
passedFunction.invoke() | ||
} | ||
} | ||
} |