rdhabalia closed pull request #1480: GC improvement: Reduce string objects from 
Publisher and Consumer stats
URL: https://github.com/apache/incubator-pulsar/pull/1480
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java
index 4c5dbbca9..5b2fc5874 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java
@@ -130,10 +130,10 @@ public Consumer(Subscription subscription, SubType 
subType, String topicName, lo
         this.metadata = metadata != null ? metadata : Collections.emptyMap();
 
         stats = new ConsumerStats();
-        stats.address = cnx.clientAddress().toString();
+        stats.setAddress(cnx.clientAddress().toString());
         stats.consumerName = consumerName;
-        stats.connectedSince = DateFormatter.now();
-        stats.clientVersion = cnx.getClientVersion();
+        stats.setConnectedSince(DateFormatter.now());
+        stats.setClientVersion(cnx.getClientVersion());
         stats.metadata = this.metadata;
 
         if (subType == SubType.Shared) {
diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Producer.java 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Producer.java
index 35bc3151b..3e42dfbbb 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Producer.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Producer.java
@@ -98,10 +98,10 @@ public Producer(Topic topic, ServerCnx cnx, long 
producerId, String producerName
         this.metadata = metadata != null ? metadata : Collections.emptyMap();
 
         this.stats = isNonPersistentTopic ? new NonPersistentPublisherStats() 
: new PublisherStats();
-        stats.address = cnx.clientAddress().toString();
-        stats.connectedSince = DateFormatter.now();
-        stats.clientVersion = cnx.getClientVersion();
-        stats.producerName = producerName;
+        stats.setAddress(cnx.clientAddress().toString());
+        stats.setConnectedSince(DateFormatter.now());
+        stats.setClientVersion(cnx.getClientVersion());
+        stats.setProducerName(producerName);
         stats.producerId = producerId;
         stats.metadata = this.metadata;
 
diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java
index bada69ca2..9cd172f9e 100644
--- 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java
+++ 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java
@@ -437,8 +437,8 @@ protected void handleConsumerStats(CommandConsumerStats 
commandConsumerStats) {
         
commandConsumerStatsResponseBuilder.setAvailablePermits(consumerStats.availablePermits);
         
commandConsumerStatsResponseBuilder.setUnackedMessages(consumerStats.unackedMessages);
         
commandConsumerStatsResponseBuilder.setBlockedConsumerOnUnackedMsgs(consumerStats.blockedConsumerOnUnackedMsgs);
-        commandConsumerStatsResponseBuilder.setAddress(consumerStats.address);
-        
commandConsumerStatsResponseBuilder.setConnectedSince(consumerStats.connectedSince);
+        
commandConsumerStatsResponseBuilder.setAddress(consumerStats.getAddress());
+        
commandConsumerStatsResponseBuilder.setConnectedSince(consumerStats.getConnectedSince());
 
         Subscription subscription = consumer.getSubscription();
         
commandConsumerStatsResponseBuilder.setMsgBacklog(subscription.getNumberOfEntriesInBacklog());
diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/nonpersistent/NonPersistentTopic.java
 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/nonpersistent/NonPersistentTopic.java
index 056062a74..6a6aecbaa 100644
--- 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/nonpersistent/NonPersistentTopic.java
+++ 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/nonpersistent/NonPersistentTopic.java
@@ -739,10 +739,10 @@ public void updateRates(NamespaceStats nsStats, 
NamespaceBundleStats bundleStats
 
                     // Populate consumer specific stats here
                     topicStatsStream.startObject();
-                    topicStatsStream.writePair("address", 
consumerStats.address);
+                    topicStatsStream.writePair("address", 
consumerStats.getAddress());
                     topicStatsStream.writePair("consumerName", 
consumerStats.consumerName);
                     topicStatsStream.writePair("availablePermits", 
consumerStats.availablePermits);
-                    topicStatsStream.writePair("connectedSince", 
consumerStats.connectedSince);
+                    topicStatsStream.writePair("connectedSince", 
consumerStats.getConnectedSince());
                     topicStatsStream.writePair("msgRateOut", 
consumerStats.msgRateOut);
                     topicStatsStream.writePair("msgThroughputOut", 
consumerStats.msgThroughputOut);
                     topicStatsStream.writePair("msgRateRedeliver", 
consumerStats.msgRateRedeliver);
@@ -752,8 +752,8 @@ public void updateRates(NamespaceStats nsStats, 
NamespaceBundleStats bundleStats
                         
topicStatsStream.writePair("blockedConsumerOnUnackedMsgs",
                                 consumerStats.blockedConsumerOnUnackedMsgs);
                     }
-                    if (consumerStats.clientVersion != null) {
-                        topicStatsStream.writePair("clientVersion", 
consumerStats.clientVersion);
+                    if (consumerStats.getClientVersion() != null) {
+                        topicStatsStream.writePair("clientVersion", 
consumerStats.getClientVersion());
                     }
                     topicStatsStream.endObject();
                 }
@@ -848,8 +848,8 @@ public NonPersistentTopicStats getStats() {
             if (pubStats != null) {
                 ReplicatorStats.msgRateIn = pubStats.msgRateIn;
                 ReplicatorStats.msgThroughputIn = pubStats.msgThroughputIn;
-                ReplicatorStats.inboundConnection = pubStats.address;
-                ReplicatorStats.inboundConnectedSince = 
pubStats.connectedSince;
+                ReplicatorStats.inboundConnection = pubStats.getAddress();
+                ReplicatorStats.inboundConnectedSince = 
pubStats.getConnectedSince();
             }
 
             stats.msgRateOut += ReplicatorStats.msgRateOut;
diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
index c179aef8c..513fd8718 100644
--- 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
+++ 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
@@ -1077,8 +1077,8 @@ public void updateRates(NamespaceStats nsStats, 
NamespaceBundleStats bundleStats
             if (pubStats != null) {
                 rStat.msgRateIn = pubStats.msgRateIn;
                 rStat.msgThroughputIn = pubStats.msgThroughputIn;
-                rStat.inboundConnection = pubStats.address;
-                rStat.inboundConnectedSince = pubStats.connectedSince;
+                rStat.inboundConnection = pubStats.getAddress();
+                rStat.inboundConnectedSince = pubStats.getConnectedSince();
             }
 
             topicStats.aggMsgRateOut += rStat.msgRateOut;
@@ -1152,10 +1152,10 @@ public void updateRates(NamespaceStats nsStats, 
NamespaceBundleStats bundleStats
 
                     // Populate consumer specific stats here
                     topicStatsStream.startObject();
-                    topicStatsStream.writePair("address", 
consumerStats.address);
+                    topicStatsStream.writePair("address", 
consumerStats.getAddress());
                     topicStatsStream.writePair("consumerName", 
consumerStats.consumerName);
                     topicStatsStream.writePair("availablePermits", 
consumerStats.availablePermits);
-                    topicStatsStream.writePair("connectedSince", 
consumerStats.connectedSince);
+                    topicStatsStream.writePair("connectedSince", 
consumerStats.getConnectedSince());
                     topicStatsStream.writePair("msgRateOut", 
consumerStats.msgRateOut);
                     topicStatsStream.writePair("msgThroughputOut", 
consumerStats.msgThroughputOut);
                     topicStatsStream.writePair("msgRateRedeliver", 
consumerStats.msgRateRedeliver);
@@ -1165,8 +1165,8 @@ public void updateRates(NamespaceStats nsStats, 
NamespaceBundleStats bundleStats
                         
topicStatsStream.writePair("blockedConsumerOnUnackedMsgs",
                                 consumerStats.blockedConsumerOnUnackedMsgs);
                     }
-                    if (consumerStats.clientVersion != null) {
-                        topicStatsStream.writePair("clientVersion", 
consumerStats.clientVersion);
+                    if (consumerStats.getClientVersion() != null) {
+                        topicStatsStream.writePair("clientVersion", 
consumerStats.getClientVersion());
                     }
                     topicStatsStream.endObject();
                 }
@@ -1270,8 +1270,8 @@ public PersistentTopicStats getStats() {
             if (pubStats != null) {
                 replicatorStats.msgRateIn = pubStats.msgRateIn;
                 replicatorStats.msgThroughputIn = pubStats.msgThroughputIn;
-                replicatorStats.inboundConnection = pubStats.address;
-                replicatorStats.inboundConnectedSince = 
pubStats.connectedSince;
+                replicatorStats.inboundConnection = pubStats.getAddress();
+                replicatorStats.inboundConnectedSince = 
pubStats.getConnectedSince();
             }
 
             stats.msgRateOut += replicatorStats.msgRateOut;
diff --git 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest2.java 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest2.java
index dbc36f393..78839f372 100644
--- 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest2.java
+++ 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest2.java
@@ -54,6 +54,7 @@
 import org.apache.pulsar.common.naming.TopicDomain;
 import org.apache.pulsar.common.naming.TopicName;
 import org.apache.pulsar.common.policies.data.ClusterData;
+import org.apache.pulsar.common.policies.data.ConsumerStats;
 import org.apache.pulsar.common.policies.data.FailureDomain;
 import org.apache.pulsar.common.policies.data.NonPersistentTopicStats;
 import org.apache.pulsar.common.policies.data.PartitionedTopicStats;
@@ -62,6 +63,7 @@
 import org.apache.pulsar.common.policies.data.PersistentTopicStats;
 import org.apache.pulsar.common.policies.data.PropertyAdmin;
 import org.apache.pulsar.common.policies.data.RetentionPolicies;
+import org.apache.pulsar.common.policies.data.SubscriptionStats;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.DataProvider;
@@ -722,4 +724,49 @@ public void testNonPersistentTopics() throws Exception {
         topicsInNs.removeAll(topicNames);
         assertEquals(topicsInNs.size(), 0);
     }
+
+    @Test
+    public void testPublishConsumerStats() throws Exception {
+        final String topicName = "statTopic";
+        final String subscriberName = topicName + "-my-sub-1";
+        final String topic = "persistent://prop-xyz/use/ns1/" + topicName;
+        final String producerName = "myProducer";
+
+        URL pulsarUrl = new URL("http://127.0.0.1"; + ":" + 
BROKER_WEBSERVICE_PORT);
+
+        PulsarClient client = 
PulsarClient.builder().serviceUrl(pulsarUrl.toString()).build();
+        Consumer<byte[]> consumer = 
client.newConsumer().topic(topic).subscriptionName(subscriberName)
+                .subscriptionType(SubscriptionType.Shared).subscribe();
+        Producer<byte[]> producer = 
client.newProducer().topic(topic).producerName(producerName).create();
+
+        retryStrategically((test) -> {
+            PersistentTopicStats stats;
+            try {
+                stats = admin.persistentTopics().getStats(topic);
+                return stats.publishers.size() > 0 && 
stats.subscriptions.get(subscriberName) != null
+                        && 
stats.subscriptions.get(subscriberName).consumers.size() > 0;
+            } catch (PulsarAdminException e) {
+                return false;
+            }
+        }, 5, 200);
+
+        PersistentTopicStats topicStats = 
admin.persistentTopics().getStats(topic);
+        assertEquals(topicStats.publishers.size(), 1);
+        assertNotNull(topicStats.publishers.get(0).getAddress());
+        assertNotNull(topicStats.publishers.get(0).getClientVersion());
+        assertNotNull(topicStats.publishers.get(0).getConnectedSince());
+        assertNotNull(topicStats.publishers.get(0).getProducerName());
+        assertEquals(topicStats.publishers.get(0).getProducerName(), 
producerName);
+
+        SubscriptionStats subscriber = 
topicStats.subscriptions.get(subscriberName);
+        assertNotNull(subscriber);
+        assertEquals(subscriber.consumers.size(), 1);
+        ConsumerStats consumerStats = subscriber.consumers.get(0);
+        assertNotNull(consumerStats.getAddress());
+        assertNotNull(consumerStats.getClientVersion());
+        assertNotNull(consumerStats.getConnectedSince());
+
+        producer.close();
+        consumer.close();
+    }
 }
diff --git 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceTest.java
 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceTest.java
index c8dc63a85..c5b8e5aa0 100644
--- 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceTest.java
+++ 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceTest.java
@@ -174,7 +174,7 @@ public void testBrokerServicePersistentTopicStats() throws 
Exception {
         assertTrue(stats.publishers.get(0).msgRateIn > 0.0);
         assertTrue(stats.publishers.get(0).msgThroughputIn > 0.0);
         assertTrue(stats.publishers.get(0).averageMsgSize > 0.0);
-        assertNotNull(stats.publishers.get(0).clientVersion);
+        assertNotNull(stats.publishers.get(0).getClientVersion());
 
         // aggregated publish stats
         assertEquals(stats.msgRateIn, stats.publishers.get(0).msgRateIn);
@@ -191,7 +191,7 @@ public void testBrokerServicePersistentTopicStats() throws 
Exception {
         assertEquals(subStats.msgThroughputOut, 
subStats.consumers.get(0).msgThroughputOut);
         assertEquals(stats.msgRateOut, subStats.consumers.get(0).msgRateOut);
         assertEquals(stats.msgThroughputOut, 
subStats.consumers.get(0).msgThroughputOut);
-        assertNotNull(subStats.consumers.get(0).clientVersion);
+        assertNotNull(subStats.consumers.get(0).getClientVersion());
 
         Message<byte[]> msg;
         for (int i = 0; i < 10; i++) {
diff --git 
a/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/ConsumerStats.java
 
b/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/ConsumerStats.java
index d5669a0ee..b8e2ee186 100644
--- 
a/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/ConsumerStats.java
+++ 
b/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/ConsumerStats.java
@@ -39,25 +39,34 @@
 
     /** Number of available message permits for the consumer */
     public int availablePermits;
-    
+
     /** Number of unacknowledged messages for the consumer */
     public int unackedMessages;
-    
+
     /** Flag to verify if consumer is blocked due to reaching threshold of 
unacked messages */
     public boolean blockedConsumerOnUnackedMsgs;
 
     /** Address of this consumer */
-    public String address;
+    private int addressOffset = -1;
+    private int addressLength;
 
     /** Timestamp of connection */
-    public String connectedSince;
-    
+    private int connectedSinceOffset = -1;
+    private int connectedSinceLength;
+
     /** Client library version */
-    public String clientVersion;
+    private int clientVersionOffset = -1;
+    private int clientVersionLength;
 
     /** Metadata (key/value strings) associated with this consumer */
     public Map<String, String> metadata;
 
+    /**
+     * In order to prevent multiple string object allocation under stats: 
create a string-buffer that stores data for all string
+     * place-holders
+     */
+    private StringBuilder stringBuffer = new StringBuilder();
+
     public ConsumerStats add(ConsumerStats stats) {
         checkNotNull(stats);
         this.msgRateOut += stats.msgRateOut;
@@ -68,4 +77,48 @@ public ConsumerStats add(ConsumerStats stats) {
         this.blockedConsumerOnUnackedMsgs = stats.blockedConsumerOnUnackedMsgs;
         return this;
     }
+
+    public String getAddress() {
+        return addressOffset == -1 ? null : 
stringBuffer.substring(addressOffset, addressOffset + addressLength);
+    }
+
+    public void setAddress(String address) {
+        if (address == null) {
+            this.addressOffset = -1;
+            return;
+        }
+        this.addressOffset = this.stringBuffer.length();
+        this.addressLength = address.length();
+        this.stringBuffer.append(address);
+    }
+
+    public String getConnectedSince() {
+        return connectedSinceOffset == -1 ? null
+                : stringBuffer.substring(connectedSinceOffset, 
connectedSinceOffset + connectedSinceLength);
+    }
+
+    public void setConnectedSince(String connectedSince) {
+        if (connectedSince == null) {
+            this.connectedSinceOffset = -1;
+            return;
+        }
+        this.connectedSinceOffset = this.stringBuffer.length();
+        this.connectedSinceLength = connectedSince.length();
+        this.stringBuffer.append(connectedSince);
+    }
+
+    public String getClientVersion() {
+        return clientVersionOffset == -1 ? null
+                : stringBuffer.substring(clientVersionOffset, 
clientVersionOffset + clientVersionLength);
+    }
+
+    public void setClientVersion(String clientVersion) {
+        if (clientVersion == null) {
+            this.clientVersionOffset = -1;
+            return;
+        }
+        this.clientVersionOffset = this.stringBuffer.length();
+        this.clientVersionLength = clientVersion.length();
+        this.stringBuffer.append(clientVersion);
+    }
 }
diff --git 
a/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/PublisherStats.java
 
b/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/PublisherStats.java
index 4819b35ab..977ac9b42 100644
--- 
a/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/PublisherStats.java
+++ 
b/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/PublisherStats.java
@@ -38,16 +38,26 @@
     public long producerId;
 
     /** Producer name */
-    public String producerName;
+    private int producerNameOffset = -1;
+    private int producerNameLength;
 
     /** Address of this publisher */
-    public String address;
+    private int addressOffset = -1;
+    private int addressLength;
 
     /** Timestamp of connection */
-    public String connectedSince;
-    
+    private int connectedSinceOffset = -1;
+    private int connectedSinceLength;
+
     /** Client library version */
-    public String clientVersion;
+    private int clientVersionOffset = -1;
+    private int clientVersionLength;
+
+    /**
+     * In order to prevent multiple string objects under stats: create a 
string-buffer that stores data for all string
+     * place-holders
+     */
+    private StringBuilder stringBuffer = new StringBuilder();
 
     /** Metadata (key/value strings) associated with this publisher */
     public Map<String, String> metadata;
@@ -59,4 +69,63 @@ public PublisherStats add(PublisherStats stats) {
         this.averageMsgSize += stats.averageMsgSize;
         return this;
     }
+
+    public String getProducerName() {
+        return producerNameOffset == -1 ? null
+                : stringBuffer.substring(producerNameOffset, 
producerNameOffset + producerNameLength);
+    }
+
+    public void setProducerName(String producerName) {
+        if (producerName == null) {
+            this.producerNameOffset = -1;
+            return;
+        }
+        this.producerNameOffset = this.stringBuffer.length();
+        this.producerNameLength = producerName.length();
+        this.stringBuffer.append(producerName);
+    }
+
+    public String getAddress() {
+        return addressOffset == -1 ? null : 
stringBuffer.substring(addressOffset, addressOffset + addressLength);
+    }
+
+    public void setAddress(String address) {
+        if (address == null) {
+            this.addressOffset = -1;
+            return;
+        }
+        this.addressOffset = this.stringBuffer.length();
+        this.addressLength = address.length();
+        this.stringBuffer.append(address);
+    }
+
+    public String getConnectedSince() {
+        return connectedSinceOffset == -1 ? null
+                : stringBuffer.substring(connectedSinceOffset, 
connectedSinceOffset + connectedSinceLength);
+    }
+
+    public void setConnectedSince(String connectedSince) {
+        if (connectedSince == null) {
+            this.connectedSinceOffset = -1;
+            return;
+        }
+        this.connectedSinceOffset = this.stringBuffer.length();
+        this.connectedSinceLength = connectedSince.length();
+        this.stringBuffer.append(connectedSince);
+    }
+
+    public String getClientVersion() {
+        return clientVersionOffset == -1 ? null
+                : stringBuffer.substring(clientVersionOffset, 
clientVersionOffset + clientVersionLength);
+    }
+
+    public void setClientVersion(String clientVersion) {
+        if (clientVersion == null) {
+            this.clientVersionOffset = -1;
+            return;
+        }
+        this.clientVersionOffset = this.stringBuffer.length();
+        this.clientVersionLength = clientVersion.length();
+        this.stringBuffer.append(clientVersion);
+    }
 }
diff --git 
a/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/ConsumerStatsTest.java
 
b/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/ConsumerStatsTest.java
new file mode 100644
index 000000000..731f52e75
--- /dev/null
+++ 
b/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/ConsumerStatsTest.java
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.common.policies.data;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class ConsumerStatsTest {
+
+    @Test
+    public void testConsumerStats() {
+        ConsumerStats stats = new ConsumerStats();
+        Assert.assertNull(stats.getAddress());
+        Assert.assertNull(stats.getClientVersion());
+        Assert.assertNull(stats.getConnectedSince());
+        
+        stats.setAddress("address");
+        Assert.assertEquals(stats.getAddress(), "address");
+        stats.setAddress("address1");
+        Assert.assertEquals(stats.getAddress(), "address1");
+        
+        stats.setClientVersion("version");
+        Assert.assertEquals(stats.getClientVersion(), "version");
+        Assert.assertEquals(stats.getAddress(), "address1");
+        
+        stats.setConnectedSince("connected");
+        Assert.assertEquals(stats.getConnectedSince(), "connected");
+        Assert.assertEquals(stats.getAddress(), "address1");
+        Assert.assertEquals(stats.getClientVersion(), "version");
+        
+        stats.setAddress(null);
+        Assert.assertEquals(stats.getAddress(), null);
+        
+        stats.setConnectedSince("");
+        Assert.assertEquals(stats.getConnectedSince(), "");
+        
+        stats.setClientVersion("version2");
+        Assert.assertEquals(stats.getClientVersion(), "version2");
+        
+        Assert.assertEquals(stats.getAddress(), null);
+        
+        Assert.assertEquals(stats.getClientVersion(), "version2");
+        
+        stats.setConnectedSince(null);
+        stats.setClientVersion(null);
+        Assert.assertNull(stats.getConnectedSince());
+        Assert.assertNull(stats.getClientVersion());
+    }
+}
diff --git 
a/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/PublisherStatsTest.java
 
b/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/PublisherStatsTest.java
new file mode 100644
index 000000000..7b305914f
--- /dev/null
+++ 
b/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/PublisherStatsTest.java
@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.common.policies.data;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class PublisherStatsTest {
+
+    @Test
+    public void testPublisherStats() {
+        PublisherStats stats = new PublisherStats();
+        Assert.assertNull(stats.getAddress());
+        Assert.assertNull(stats.getClientVersion());
+        Assert.assertNull(stats.getConnectedSince());
+        Assert.assertNull(stats.getProducerName());
+        
+        stats.setAddress("address");
+        Assert.assertEquals(stats.getAddress(), "address");
+        stats.setAddress("address1");
+        Assert.assertEquals(stats.getAddress(), "address1");
+        
+        stats.setClientVersion("version");
+        Assert.assertEquals(stats.getClientVersion(), "version");
+        Assert.assertEquals(stats.getAddress(), "address1");
+        
+        stats.setConnectedSince("connected");
+        Assert.assertEquals(stats.getConnectedSince(), "connected");
+        Assert.assertEquals(stats.getAddress(), "address1");
+        Assert.assertEquals(stats.getClientVersion(), "version");
+        
+        stats.setProducerName("producer");
+        Assert.assertEquals(stats.getProducerName(), "producer");
+        Assert.assertEquals(stats.getConnectedSince(), "connected");
+        Assert.assertEquals(stats.getAddress(), "address1");
+        Assert.assertEquals(stats.getClientVersion(), "version");
+        
+        stats.setAddress(null);
+        Assert.assertEquals(stats.getAddress(), null);
+        
+        stats.setConnectedSince("");
+        Assert.assertEquals(stats.getConnectedSince(), "");
+        
+        stats.setClientVersion("version2");
+        Assert.assertEquals(stats.getClientVersion(), "version2");
+        
+        stats.setProducerName(null);
+        Assert.assertEquals(stats.getProducerName(), null);
+        
+        Assert.assertEquals(stats.getAddress(), null);
+        
+        Assert.assertEquals(stats.getClientVersion(), "version2");
+        
+        stats.setConnectedSince(null);
+        stats.setClientVersion(null);
+        Assert.assertNull(stats.getConnectedSince());
+        Assert.assertNull(stats.getClientVersion());
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to