diff --git a/src/main/java/io/lettuce/core/cluster/topology/NodeTopologyView.java b/src/main/java/io/lettuce/core/cluster/topology/NodeTopologyView.java index af0265ef17..f12c964868 100644 --- a/src/main/java/io/lettuce/core/cluster/topology/NodeTopologyView.java +++ b/src/main/java/io/lettuce/core/cluster/topology/NodeTopologyView.java @@ -15,15 +15,16 @@ */ package io.lettuce.core.cluster.topology; +import java.util.function.Function; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import io.lettuce.core.RedisFuture; import io.lettuce.core.RedisURI; import io.lettuce.core.cluster.models.partitions.ClusterPartitionParser; import io.lettuce.core.cluster.models.partitions.Partitions; import io.lettuce.core.cluster.models.partitions.RedisClusterNode; - -import java.util.function.Function; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import io.lettuce.core.internal.LettuceStrings; /** * @author Mark Paluch @@ -47,8 +48,9 @@ class NodeTopologyView { private final String info; - private static final Pattern CONNECTED_CLIENTS_PATTERN = makePatternForProperty("connected_clients"); - private static final Pattern MASTER_REPL_OFFSET_PATTERN = makePatternForProperty("master_repl_offset"); + private static final Pattern CONNECTED_CLIENTS_PATTERN = patternFor("connected_clients"); + + private static final Pattern MASTER_REPL_OFFSET_PATTERN = patternFor("master_repl_offset"); private NodeTopologyView(RedisURI redisURI) { @@ -75,25 +77,27 @@ private NodeTopologyView(RedisURI redisURI) { this.latency = latency; } - private static Pattern makePatternForProperty(String propertyName) { - return Pattern.compile("^" + Pattern.quote(propertyName) + ":(.*)$", Pattern.MULTILINE); - } - - private static T matchOrDefault(String info, Pattern pattern, T defaultValue, Function converterIfFound) { - Matcher matcher = pattern.matcher(info); - if(!matcher.find()) { - return defaultValue; - } - String foundValue = matcher.group(1); - return foundValue == null ? defaultValue : converterIfFound.apply(foundValue); + private static Pattern patternFor(String propertyName) { + return Pattern.compile(String.format("^%s:(.*)$", Pattern.quote(propertyName)), Pattern.MULTILINE); } private int getClientCount(String info) { - return matchOrDefault(info, CONNECTED_CLIENTS_PATTERN, 0, Integer::parseInt); + return getMatchOrDefault(info, CONNECTED_CLIENTS_PATTERN, Integer::parseInt, 0); } private long getReplicationOffset(String info) { - return matchOrDefault(info, MASTER_REPL_OFFSET_PATTERN, -1L, Long::parseLong); + return getMatchOrDefault(info, MASTER_REPL_OFFSET_PATTERN, Long::parseLong, -1L); + } + + private static T getMatchOrDefault(String haystack, Pattern pattern, Function converter, T defaultValue) { + + Matcher matcher = pattern.matcher(haystack); + + if (matcher.find() && LettuceStrings.isNotEmpty(matcher.group(1))) { + return converter.apply(matcher.group(1)); + } + + return defaultValue; } static NodeTopologyView from(RedisURI redisURI, Requests clusterNodesRequests, Requests infoRequests) { diff --git a/src/test/java/io/lettuce/core/cluster/topology/NodeTopologyViewsUnitTests.java b/src/test/java/io/lettuce/core/cluster/topology/NodeTopologyViewsUnitTests.java index 9e4f363a11..4ebefc660a 100644 --- a/src/test/java/io/lettuce/core/cluster/topology/NodeTopologyViewsUnitTests.java +++ b/src/test/java/io/lettuce/core/cluster/topology/NodeTopologyViewsUnitTests.java @@ -15,18 +15,18 @@ */ package io.lettuce.core.cluster.topology; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.*; import java.util.Arrays; import java.util.Set; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import io.lettuce.core.RedisURI; /** + * Unit tests for {@link NodeTopologyViews}. + * * @author Mark Paluch */ class NodeTopologyViewsUnitTests { @@ -63,21 +63,24 @@ void shouldFailWithoutOwnPartition() { String viewByLocalhost = "1 127.0.0.1:6479 master - 0 1401258245007 2 connected 8000-11999\n"; - assertThatThrownBy(() -> new NodeTopologyView(localhost, viewByLocalhost, "", 0).getOwnPartition()).isInstanceOf( - IllegalStateException.class); + assertThatThrownBy(() -> new NodeTopologyView(localhost, viewByLocalhost, "", 0).getOwnPartition()) + .isInstanceOf(IllegalStateException.class); } @Test void infoParsingShouldNotFailWithWindowsPaths() { + RedisURI localhost = RedisURI.create("127.0.0.1", 6479); String viewByLocalhost = "1 127.0.0.1:6479 master - 0 1401258245007 2 connected 8000-11999\n"; - String info = "executable:c:\\users\\user~1.after\\appdata\\local\\temp\\1657742252598-0\\redis-server-7.0.2.exe\n" + - "connected_clients:2\n" + - "config_file:C:\\Users\\user~1.after\\AppData\\Local\\Temp\\redis-server_496893189609231874520793.conf\n" + - "master_repl_offset:5\n"; + String info = "executable:c:\\users\\user~1.after\\appdata\\local\\temp\\1657742252598-0\\redis-server-7.0.2.exe\n" + + "connected_clients:2\n" + + "config_file:C:\\Users\\user~1.after\\AppData\\Local\\Temp\\redis-server_496893189609231874520793.conf\n" + + "master_repl_offset:5\n"; + NodeTopologyView nodeTopologyView = new NodeTopologyView(localhost, viewByLocalhost, info, 0); - Assertions.assertEquals(2, nodeTopologyView.getConnectedClients()); - Assertions.assertEquals(5, nodeTopologyView.getReplicationOffset()); + + assertThat(nodeTopologyView.getConnectedClients()).isEqualTo(2); + assertThat(nodeTopologyView.getReplicationOffset()).isEqualTo(5); } }