diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/conf/Configuration.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/conf/Configuration.java index 71759806b..c55c822a3 100644 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/conf/Configuration.java +++ b/jraft-core/src/main/java/com/alipay/sofa/jraft/conf/Configuration.java @@ -297,7 +297,7 @@ public boolean parse(final String conf) { peerStr = peerStr.substring(0, index); isLearner = true; } - if (peer.parse(peerStr)) { + if (peer.parse(StringUtils.trim(peerStr))) { if (isLearner) { addLearner(peer); } else { diff --git a/jraft-core/src/test/java/com/alipay/sofa/jraft/conf/ConfigurationTest.java b/jraft-core/src/test/java/com/alipay/sofa/jraft/conf/ConfigurationTest.java index 0fca94f2b..61c20af4d 100644 --- a/jraft-core/src/test/java/com/alipay/sofa/jraft/conf/ConfigurationTest.java +++ b/jraft-core/src/test/java/com/alipay/sofa/jraft/conf/ConfigurationTest.java @@ -41,6 +41,23 @@ public void testToStringParseEmpty() { assertEquals(conf, newConf); } + @Test + public void testToStringParseWithSpace() { + final String confStr = "localhost:8081, localhost:8082, localhost:8083"; + final Configuration conf = JRaftUtils.getConfiguration(confStr); + assertEquals(3, conf.size()); + for (final PeerId peer : conf) { + assertTrue(peer.toString().startsWith("localhost:80")); + } + assertFalse(conf.isEmpty()); + final Configuration newConf = new Configuration(); + assertTrue(newConf.parse(conf.toString())); + assertEquals(3, newConf.getPeerSet().size()); + assertTrue(newConf.contains(new PeerId("localhost", 8081))); + assertTrue(newConf.contains(new PeerId("localhost", 8082))); + assertTrue(newConf.contains(new PeerId("localhost", 8083))); + } + @Test public void testToStringParseStuff() { final String confStr = "localhost:8081,localhost:8082,localhost:8083";