From 41923d3c40ec303a2125241e13d31aec3101dafb Mon Sep 17 00:00:00 2001 From: Matilda Clerke Date: Thu, 26 Sep 2024 16:37:28 +1000 Subject: [PATCH] 7311: Convert PeerTaskExecutorResult to a record Signed-off-by: Matilda Clerke --- .../manager/peertask/PeerTaskExecutor.java | 19 +++++----- .../peertask/PeerTaskExecutorResult.java | 22 +++--------- .../peertask/PeerTaskExecutorTest.java | 36 +++++++++---------- 3 files changed, 33 insertions(+), 44 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutor.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutor.java index 6fdb89f8c6..3187149a0e 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutor.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutor.java @@ -26,6 +26,7 @@ import org.hyperledger.besu.plugin.services.metrics.OperationTimer; import java.util.ArrayList; import java.util.Collection; +import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; @@ -80,10 +81,10 @@ public class PeerTaskExecutor { executorResult = executeAgainstPeer(peerTask, peer); } catch (NoAvailablePeerException e) { executorResult = - new PeerTaskExecutorResult<>(null, PeerTaskExecutorResponseCode.NO_PEER_AVAILABLE); + new PeerTaskExecutorResult<>(Optional.empty(), PeerTaskExecutorResponseCode.NO_PEER_AVAILABLE); } } while (--triesRemaining > 0 - && executorResult.getResponseCode() != PeerTaskExecutorResponseCode.SUCCESS); + && executorResult.responseCode() != PeerTaskExecutorResponseCode.SUCCESS); return executorResult; } @@ -112,28 +113,28 @@ public class PeerTaskExecutor { result = peerTask.parseResponse(responseMessageData); } peer.recordUsefulResponse(); - executorResult = new PeerTaskExecutorResult<>(result, PeerTaskExecutorResponseCode.SUCCESS); + executorResult = new PeerTaskExecutorResult<>(Optional.ofNullable(result), PeerTaskExecutorResponseCode.SUCCESS); } catch (PeerConnection.PeerNotConnected e) { executorResult = - new PeerTaskExecutorResult<>(null, PeerTaskExecutorResponseCode.PEER_DISCONNECTED); + new PeerTaskExecutorResult<>(Optional.empty(), PeerTaskExecutorResponseCode.PEER_DISCONNECTED); } catch (InterruptedException | TimeoutException e) { peer.recordRequestTimeout(requestMessageData.getCode()); - executorResult = new PeerTaskExecutorResult<>(null, PeerTaskExecutorResponseCode.TIMEOUT); + executorResult = new PeerTaskExecutorResult<>(Optional.empty(), PeerTaskExecutorResponseCode.TIMEOUT); } catch (InvalidPeerTaskResponseException e) { peer.recordUselessResponse(e.getMessage()); executorResult = - new PeerTaskExecutorResult<>(null, PeerTaskExecutorResponseCode.INVALID_RESPONSE); + new PeerTaskExecutorResult<>(Optional.empty(), PeerTaskExecutorResponseCode.INVALID_RESPONSE); } catch (ExecutionException e) { executorResult = - new PeerTaskExecutorResult<>(null, PeerTaskExecutorResponseCode.INTERNAL_SERVER_ERROR); + new PeerTaskExecutorResult<>(Optional.empty(), PeerTaskExecutorResponseCode.INTERNAL_SERVER_ERROR); } } while (--triesRemaining > 0 - && executorResult.getResponseCode() != PeerTaskExecutorResponseCode.SUCCESS - && executorResult.getResponseCode() != PeerTaskExecutorResponseCode.PEER_DISCONNECTED + && executorResult.responseCode() != PeerTaskExecutorResponseCode.SUCCESS + && executorResult.responseCode() != PeerTaskExecutorResponseCode.PEER_DISCONNECTED && sleepBetweenRetries()); return executorResult; diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutorResult.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutorResult.java index f89bc67f61..ef528cd2ae 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutorResult.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutorResult.java @@ -16,20 +16,8 @@ package org.hyperledger.besu.ethereum.eth.manager.peertask; import java.util.Optional; -public class PeerTaskExecutorResult { - private final Optional result; - private final PeerTaskExecutorResponseCode responseCode; - - public PeerTaskExecutorResult(final T result, final PeerTaskExecutorResponseCode responseCode) { - this.result = Optional.ofNullable(result); - this.responseCode = responseCode; - } - - public Optional getResult() { - return result; - } - - public PeerTaskExecutorResponseCode getResponseCode() { - return responseCode; - } -} +public record PeerTaskExecutorResult ( + Optional result, + PeerTaskExecutorResponseCode responseCode +) +{} diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutorTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutorTest.java index 413b68f77c..297ce08b05 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutorTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutorTest.java @@ -84,9 +84,9 @@ public class PeerTaskExecutorTest { Mockito.verify(ethPeer).recordUsefulResponse(); Assertions.assertNotNull(result); - Assertions.assertTrue(result.getResult().isPresent()); - Assertions.assertSame(responseObject, result.getResult().get()); - Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.getResponseCode()); + Assertions.assertTrue(result.result().isPresent()); + Assertions.assertSame(responseObject, result.result().get()); + Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.responseCode()); } @Test @@ -117,9 +117,9 @@ public class PeerTaskExecutorTest { Mockito.verify(ethPeer).recordUsefulResponse(); Assertions.assertNotNull(result); - Assertions.assertTrue(result.getResult().isPresent()); - Assertions.assertSame(responseObject, result.getResult().get()); - Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.getResponseCode()); + Assertions.assertTrue(result.result().isPresent()); + Assertions.assertSame(responseObject, result.result().get()); + Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.responseCode()); } @Test @@ -139,9 +139,9 @@ public class PeerTaskExecutorTest { PeerTaskExecutorResult result = peerTaskExecutor.executeAgainstPeer(peerTask, ethPeer); Assertions.assertNotNull(result); - Assertions.assertTrue(result.getResult().isEmpty()); + Assertions.assertTrue(result.result().isEmpty()); Assertions.assertEquals( - PeerTaskExecutorResponseCode.PEER_DISCONNECTED, result.getResponseCode()); + PeerTaskExecutorResponseCode.PEER_DISCONNECTED, result.responseCode()); } @Test @@ -165,8 +165,8 @@ public class PeerTaskExecutorTest { Mockito.verify(ethPeer).recordRequestTimeout(requestMessageDataCode); Assertions.assertNotNull(result); - Assertions.assertTrue(result.getResult().isEmpty()); - Assertions.assertEquals(PeerTaskExecutorResponseCode.TIMEOUT, result.getResponseCode()); + Assertions.assertTrue(result.result().isEmpty()); + Assertions.assertEquals(PeerTaskExecutorResponseCode.TIMEOUT, result.responseCode()); } @Test @@ -191,9 +191,9 @@ public class PeerTaskExecutorTest { Mockito.verify(ethPeer).recordUselessResponse(null); Assertions.assertNotNull(result); - Assertions.assertTrue(result.getResult().isEmpty()); + Assertions.assertTrue(result.result().isEmpty()); Assertions.assertEquals( - PeerTaskExecutorResponseCode.INVALID_RESPONSE, result.getResponseCode()); + PeerTaskExecutorResponseCode.INVALID_RESPONSE, result.responseCode()); } @Test @@ -222,9 +222,9 @@ public class PeerTaskExecutorTest { Mockito.verify(ethPeer).recordUsefulResponse(); Assertions.assertNotNull(result); - Assertions.assertTrue(result.getResult().isPresent()); - Assertions.assertSame(responseObject, result.getResult().get()); - Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.getResponseCode()); + Assertions.assertTrue(result.result().isPresent()); + Assertions.assertSame(responseObject, result.result().get()); + Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.responseCode()); } @Test @@ -262,8 +262,8 @@ public class PeerTaskExecutorTest { Mockito.verify(peer2).recordUsefulResponse(); Assertions.assertNotNull(result); - Assertions.assertTrue(result.getResult().isPresent()); - Assertions.assertSame(responseObject, result.getResult().get()); - Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.getResponseCode()); + Assertions.assertTrue(result.result().isPresent()); + Assertions.assertSame(responseObject, result.result().get()); + Assertions.assertEquals(PeerTaskExecutorResponseCode.SUCCESS, result.responseCode()); } }