Skip to content

Commit

Permalink
Merge branch 'master' into opendocsUpgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
link108 authored Aug 6, 2024
2 parents 9916544 + 5baf9d8 commit d424ef8
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 229 deletions.
7 changes: 4 additions & 3 deletions kork-exceptions/kork-exceptions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ dependencies {
api(platform(project(":spinnaker-dependencies")))
api "com.google.code.findbugs:jsr305"

testImplementation "org.spockframework:spock-core"
testRuntimeOnly "cglib:cglib-nodep"
testRuntimeOnly "org.objenesis:objenesis"
testImplementation "org.mockito:mockito-core"
testImplementation "org.junit.jupiter:junit-jupiter-api"
testImplementation "org.junit.jupiter:junit-jupiter-params"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"

testImplementation "com.hubspot.jinjava:jinjava"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2024 OpsMx, Inc.
*
* 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 com.netflix.spinnaker.kork.core;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.spy;

import java.time.Duration;
import java.util.function.Supplier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class RetrySupportTest {

RetrySupport retrySupport;
int attemptCounter;

@BeforeEach
public void setup() {
retrySupport = spy(new RetrySupport());
attemptCounter = 0;

doNothing().when(retrySupport).sleep(10000);
doNothing().when(retrySupport).sleep(20000);
doNothing().when(retrySupport).sleep(40000);
doNothing().when(retrySupport).sleep(80000);
}

@ParameterizedTest(name = "should retry until success or {1} attempts is reached")
@CsvSource({"3,10,4,'empty'", "11,10,10,'Failed after 10 attempts'"})
void testRetryFailureWithMaxretries(int fail, int retry, int attempt, String msg) {
// given
String exceptionMessage = null;
int failures = fail;
int maxRetries = retry;
int expectedAttempts = attempt;
String expectedExceptionMessage = msg.equalsIgnoreCase("empty") ? null : msg;

Supplier fn =
() -> {
if (attemptCounter++ < failures) {
throw new IllegalStateException("Failed after " + attemptCounter + " attempts");
}
return null;
};

// when
try {
retrySupport.retry(fn, maxRetries, Duration.ofMillis(10000), false);
} catch (Exception e) {
exceptionMessage = e.getMessage();
}

// then
assertEquals(attemptCounter, expectedAttempts);
assertEquals(exceptionMessage, expectedExceptionMessage);
}

@Test
void testSleepExponentially() {
// given
int failures = 4;
int maxRetries = 20;
int expectedAttempts = 5;

Supplier fn =
() -> {
if (attemptCounter++ < failures) {
throw new IllegalStateException("Failed after " + attemptCounter + " attempts");
}
return null;
};

// when
retrySupport.retry(fn, maxRetries, Duration.ofMillis(10000), true);

// then
assertEquals(attemptCounter, expectedAttempts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.netflix.spinnaker.moniker;

import com.netflix.spinnaker.orchestration.OperationDescription;

/**
* A "Namer" takes some type "T" (e.g. a server group) and allows you to either
*
Expand All @@ -29,5 +31,18 @@
public interface Namer<T> {
void applyMoniker(T obj, Moniker moniker);

/**
* Sets a moniker on the given object with additional context provided by the description. This
* default implementation ignores the provided {@code description} and simply delegates to the
* {@link #applyMoniker(Object, Moniker)} method.
*
* @param obj the object to which the moniker will be applied
* @param moniker the moniker to apply
* @param description a description that provides additional context for the operation
*/
default void applyMoniker(T obj, Moniker moniker, OperationDescription description) {
applyMoniker(obj, moniker);
}

Moniker deriveMoniker(T obj);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.netflix.spinnaker.orchestration;

/** Marker interface for inputs to an AtomicOperation. */
public interface OperationDescription {}
4 changes: 4 additions & 0 deletions kork-pubsub-aws/kork-pubsub-aws.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ dependencies {
testImplementation "org.spockframework:spock-core"
testRuntimeOnly "cglib:cglib-nodep"
testRuntimeOnly "org.objenesis:objenesis"

testImplementation "org.mockito:mockito-core"
testImplementation "org.junit.jupiter:junit-jupiter-api"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.amazonaws.services.sqs.model.ReceiveMessageResult;
import com.netflix.spectator.api.Counter;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.kork.annotations.VisibleForTesting;
import com.netflix.spinnaker.kork.aws.ARN;
import com.netflix.spinnaker.kork.pubsub.aws.api.AmazonMessageAcknowledger;
import com.netflix.spinnaker.kork.pubsub.aws.api.AmazonPubsubMessageHandler;
Expand Down Expand Up @@ -119,7 +120,8 @@ public void run() {
}
}

private void initializeQueue() {
@VisibleForTesting
void initializeQueue() {
String queueUrl =
PubSubUtils.ensureQueueExists(
amazonSQS, queueARN, topicARN, subscription.getSqsMessageRetentionPeriodSeconds());
Expand All @@ -134,7 +136,8 @@ private void initializeQueue() {
.build();
}

private void listenForMessages() {
@VisibleForTesting
void listenForMessages() {
while (isEnabled.get()) {
ReceiveMessageResult receiveMessageResult =
amazonSQS.receiveMessage(
Expand Down
Loading

0 comments on commit d424ef8

Please sign in to comment.