From 1dd3442637f38a5482f8485a06de67a0016462c3 Mon Sep 17 00:00:00 2001 From: David Griffin Date: Wed, 30 Oct 2024 14:23:58 -0700 Subject: [PATCH] Refactor some defaults, use QueryOptions.DEFAULT in E2E test. --- src/main/java/com/fauna/client/FaunaConfig.java | 10 +++++++--- src/main/java/com/fauna/constants/Defaults.java | 12 ++++++++++++ src/main/java/com/fauna/event/FeedOptions.java | 4 ++-- src/main/java/com/fauna/query/QueryOptions.java | 3 ++- src/test/java/com/fauna/e2e/E2EQueryTest.java | 8 ++++---- 5 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/fauna/constants/Defaults.java diff --git a/src/main/java/com/fauna/client/FaunaConfig.java b/src/main/java/com/fauna/client/FaunaConfig.java index e8f008e2..123f4cbf 100644 --- a/src/main/java/com/fauna/client/FaunaConfig.java +++ b/src/main/java/com/fauna/client/FaunaConfig.java @@ -6,6 +6,10 @@ import java.util.logging.Handler; import java.util.logging.Level; +import static com.fauna.constants.Defaults.LOCAL_FAUNA_SECRET; +import static com.fauna.constants.Defaults.MAX_CONTENTION_RETRIES; +import static com.fauna.constants.Defaults.CLIENT_TIMEOUT_BUFFER; + /** * FaunaConfig is a configuration class used to set up and configure a connection to Fauna. * It encapsulates various settings such as the endpoint URL, secret key, and more. @@ -25,7 +29,7 @@ public static class FaunaEndpoint { private final StatsCollector statsCollector; public static final FaunaConfig DEFAULT = FaunaConfig.builder().build(); public static final FaunaConfig LOCAL = FaunaConfig.builder().endpoint( - FaunaEndpoint.LOCAL).secret("secret").build(); + FaunaEndpoint.LOCAL).secret(LOCAL_FAUNA_SECRET).build(); /** * Private constructor for FaunaConfig. @@ -107,8 +111,8 @@ public static Builder builder() { public static class Builder { private String endpoint = FaunaEnvironment.faunaEndpoint().orElse(FaunaEndpoint.DEFAULT); private String secret = FaunaEnvironment.faunaSecret().orElse(""); - private int maxContentionRetries = 3; - private Duration clientTimeoutBuffer = Duration.ofSeconds(5); + private int maxContentionRetries = MAX_CONTENTION_RETRIES; + private Duration clientTimeoutBuffer = CLIENT_TIMEOUT_BUFFER; private Handler logHandler = defaultLogHandler(); private StatsCollector statsCollector = new StatsCollectorImpl(); diff --git a/src/main/java/com/fauna/constants/Defaults.java b/src/main/java/com/fauna/constants/Defaults.java new file mode 100644 index 00000000..1177b904 --- /dev/null +++ b/src/main/java/com/fauna/constants/Defaults.java @@ -0,0 +1,12 @@ +package com.fauna.constants; + +import java.time.Duration; + +public class Defaults { + + public static final Duration CLIENT_TIMEOUT_BUFFER = Duration.ofSeconds(5); + public static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(5); + public static final String LOCAL_FAUNA_SECRET = "secret"; + public static final int MAX_CONTENTION_RETRIES = 3; + +} diff --git a/src/main/java/com/fauna/event/FeedOptions.java b/src/main/java/com/fauna/event/FeedOptions.java index 6ac91b29..4c7e0c01 100644 --- a/src/main/java/com/fauna/event/FeedOptions.java +++ b/src/main/java/com/fauna/event/FeedOptions.java @@ -1,6 +1,6 @@ package com.fauna.event; -import com.fauna.query.QueryOptions; +import static com.fauna.constants.Defaults.DEFAULT_TIMEOUT; import java.time.Duration; import java.util.Optional; @@ -43,7 +43,7 @@ public static class Builder { public String cursor = null; public Long startTs = null; public Integer pageSize = null; - public Duration timeout = QueryOptions.DEFAULT_TIMEOUT; + public Duration timeout = DEFAULT_TIMEOUT; public Builder cursor(String cursor) { if (startTs != null) { diff --git a/src/main/java/com/fauna/query/QueryOptions.java b/src/main/java/com/fauna/query/QueryOptions.java index 112cd3b2..104129e6 100644 --- a/src/main/java/com/fauna/query/QueryOptions.java +++ b/src/main/java/com/fauna/query/QueryOptions.java @@ -3,6 +3,8 @@ import java.time.Duration; import java.util.Optional; +import static com.fauna.constants.Defaults.DEFAULT_TIMEOUT; + public class QueryOptions { private final Boolean linearized; private final Boolean typeCheck; @@ -10,7 +12,6 @@ public class QueryOptions { private final QueryTags queryTags; private final String traceParent; - public static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(5); public static QueryOptions DEFAULT = QueryOptions.builder().build(); public QueryOptions(Builder builder) { diff --git a/src/test/java/com/fauna/e2e/E2EQueryTest.java b/src/test/java/com/fauna/e2e/E2EQueryTest.java index 4f1061c9..623b8e34 100644 --- a/src/test/java/com/fauna/e2e/E2EQueryTest.java +++ b/src/test/java/com/fauna/e2e/E2EQueryTest.java @@ -99,7 +99,7 @@ public void query_syncWithParameterized() { @Test public void query_syncWithClassAndOptions() { var q = fql("42"); - var res = c.query(q, int.class, QueryOptions.builder().build()); + var res = c.query(q, int.class, QueryOptions.DEFAULT); var exp = 42; assertEquals(exp, res.getData()); } @@ -107,7 +107,7 @@ public void query_syncWithClassAndOptions() { @Test public void query_syncWithParameterizedAndOptions() { var q = fql("[42]"); - var res = c.query(q, listOf(int.class), QueryOptions.builder().build()); + var res = c.query(q, listOf(int.class), QueryOptions.DEFAULT); var exp = List.of(42); assertEquals(exp, res.getData()); } @@ -139,7 +139,7 @@ public void query_asyncWithParameterized() throws ExecutionException, Interrupte @Test public void query_asyncWithClassAndOptions() throws ExecutionException, InterruptedException { var q = fql("42"); - var res = c.asyncQuery(q, int.class, QueryOptions.builder().build()).get(); + var res = c.asyncQuery(q, int.class, QueryOptions.DEFAULT).get(); var exp = 42; assertEquals(exp, res.getData()); } @@ -147,7 +147,7 @@ public void query_asyncWithClassAndOptions() throws ExecutionException, Interrup @Test public void query_asyncWithParameterizedAndOptions() throws ExecutionException, InterruptedException { var q = fql("[42]"); - var res = c.asyncQuery(q, listOf(int.class), QueryOptions.builder().build()).get(); + var res = c.asyncQuery(q, listOf(int.class), QueryOptions.DEFAULT).get(); var exp = List.of(42); assertEquals(exp, res.getData()); }