Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Use a Function as randomGenerator instead of a public method
Use a UnaryOperator as random generator instead of a public method whose only purpose is to serve as a hook for Mockito during test cases.
  • Loading branch information
brenuart committed Sep 29, 2021
commit 43cea47ac6e38a8adcd82dc9b49109e47fbb3bb3
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,36 @@
*/
package net.logstash.logback.appender.destination;

import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.UnaryOperator;

/**
* This strategy attempts connections to the destination in a random order.
* If a connection fails, the next random destination is attempted.
*
* The connectionTTL can be set to gracefully close connections after a specific duration.
* <p>The connectionTTL can be set to gracefully close connections after a specific duration.
* This will force the the appender to reattempt to connect to the next random destination.
*/
public class RandomDestinationConnectionStrategy extends DestinationConnectionStrategyWithTtl {

/**
* Random number generator. Function argument is the maximum value allowed for the generated
* random int number.
*/
private final UnaryOperator<Integer> randomSupplier;


public RandomDestinationConnectionStrategy() {
this(bound -> ThreadLocalRandom.current().nextInt(bound));
}

public RandomDestinationConnectionStrategy(UnaryOperator<Integer> randomSupplier) {
this.randomSupplier = Objects.requireNonNull(randomSupplier);
}

@Override
public int selectNextDestinationIndex(int previousDestinationIndex, int numDestinations) {
return nextInt(numDestinations);
}

/* Note: Currently made visible only to support some test cases */
public int nextInt(int bound) {
return ThreadLocalRandom.current().nextInt(bound);
return randomSupplier.apply(numDestinations);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -47,6 +46,7 @@
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.function.UnaryOperator;

import javax.net.SocketFactory;

Expand Down Expand Up @@ -314,16 +314,17 @@ public void testReconnectToSecondaryOnOpen() throws Exception {
public void testRandomDestinationAndReconnectToSecondaryOnOpen() throws Exception {
appender.addDestination("localhost:10000");
appender.addDestination("localhost:10001");
RandomDestinationConnectionStrategy strategy = spy(new RandomDestinationConnectionStrategy());
//doReturn(random).when(strategy).getRandom();
appender.setConnectionStrategy(strategy);

@SuppressWarnings("unchecked")
UnaryOperator<Integer> randomGenerator = mock(UnaryOperator.class);
appender.setConnectionStrategy(new RandomDestinationConnectionStrategy(randomGenerator));

// Make it fail to connect to second destination
doThrow(SocketTimeoutException.class)
.when(socket).connect(host("localhost", 10001), anyInt());

// The first index is second destination.
when(strategy.nextInt(appender.getDestinations().size())).thenReturn(1).thenReturn(0);
when(randomGenerator.apply(appender.getDestinations().size())).thenReturn(1).thenReturn(0);

// Start the appender and verify it is actually started.
// It should try to connect to the second destination, fail then retry on first destination.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,35 @@
package net.logstash.logback.appender.destination;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.util.function.UnaryOperator;

import ch.qos.logback.core.util.Duration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class RandomDestinationConnectionStrategyTest {

private RandomDestinationConnectionStrategy strategy = spy(new RandomDestinationConnectionStrategy());
@Mock
private UnaryOperator<Integer> randomGenerator;

private RandomDestinationConnectionStrategy strategy;


@BeforeEach
public void setup() {
this.strategy = new RandomDestinationConnectionStrategy(randomGenerator);
}

@Test
public void testNoConnectionTtl_success() {

when(strategy.nextInt(3))
when(randomGenerator.apply(3))
.thenReturn(0)
.thenReturn(1);

Expand All @@ -48,7 +60,7 @@ public void testNoConnectionTtl_success() {
@Test
public void testNoConnectionTtl_failed() {

when(strategy.nextInt(3))
when(randomGenerator.apply(3))
.thenReturn(0)
.thenReturn(2)
.thenReturn(1);
Expand All @@ -72,7 +84,7 @@ public void testNoConnectionTtl_failed() {
@Test
public void testConnectionTtl_success() {

when(strategy.nextInt(3))
when(randomGenerator.apply(3))
.thenReturn(0)
.thenReturn(1);

Expand All @@ -90,7 +102,7 @@ public void testConnectionTtl_success() {
@Test
public void testConnectionTtl_failed() {

when(strategy.nextInt(3))
when(randomGenerator.apply(3))
.thenReturn(0)
.thenReturn(2)
.thenReturn(1);
Expand Down