2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore;
10 import java.lang.management.ManagementFactory;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13 import javax.management.MBeanServer;
14 import javax.management.ObjectName;
15 import org.junit.After;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
21 public class ShardStatsTest {
22 private MBeanServer mbeanServer;
23 private ShardStats shardStats;
24 private ObjectName testMBeanName;
27 public void setUp() throws Exception {
29 shardStats = new ShardStats("shard-1", "DataStore", null);
30 shardStats.registerMBean();
31 mbeanServer = ManagementFactory.getPlatformMBeanServer();
32 String objectName = AbstractMXBean.BASE_JMX_PREFIX + "type=" + shardStats.getMBeanType() + ",Category="
33 + shardStats.getMBeanCategory() + ",name=" + shardStats.getMBeanName();
34 testMBeanName = new ObjectName(objectName);
38 public void tearDown() {
39 shardStats.unregisterMBean();
43 public void testGetShardName() throws Exception {
45 Object attribute = mbeanServer.getAttribute(testMBeanName, "ShardName");
46 Assert.assertEquals(attribute, "shard-1");
51 public void testGetCommittedTransactionsCount() throws Exception {
52 //let us increment some transactions count and then check
53 shardStats.incrementCommittedTransactionCount();
54 shardStats.incrementCommittedTransactionCount();
55 shardStats.incrementCommittedTransactionCount();
57 //now let us get from MBeanServer what is the transaction count.
58 Object attribute = mbeanServer.getAttribute(testMBeanName,
59 "CommittedTransactionsCount");
60 Assert.assertEquals(attribute, 3L);
66 public void testGetLastCommittedTransactionTime() throws Exception {
67 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
68 Assert.assertEquals(shardStats.getLastCommittedTransactionTime(),
69 sdf.format(new Date(0L)));
70 long millis = System.currentTimeMillis();
71 shardStats.setLastCommittedTransactionTime(millis);
73 //now let us get from MBeanServer what is the transaction count.
74 Object attribute = mbeanServer.getAttribute(testMBeanName,
75 "LastCommittedTransactionTime");
76 Assert.assertEquals(attribute, sdf.format(new Date(millis)));
77 Assert.assertNotEquals(attribute,
78 sdf.format(new Date(millis - 1)));
83 public void testGetFailedTransactionsCount() throws Exception {
84 //let us increment some transactions count and then check
85 shardStats.incrementFailedTransactionsCount();
86 shardStats.incrementFailedTransactionsCount();
89 //now let us get from MBeanServer what is the transaction count.
91 mbeanServer.getAttribute(testMBeanName, "FailedTransactionsCount");
92 Assert.assertEquals(attribute, 2L);
96 public void testGetAbortTransactionsCount() throws Exception {
97 //let us increment AbortTransactions count and then check
98 shardStats.incrementAbortTransactionsCount();
99 shardStats.incrementAbortTransactionsCount();
102 //now let us get from MBeanServer what is the transaction count.
104 mbeanServer.getAttribute(testMBeanName, "AbortTransactionsCount");
105 Assert.assertEquals(attribute, 2L);
109 public void testGetFailedReadTransactionsCount() throws Exception {
110 //let us increment FailedReadTransactions count and then check
111 shardStats.incrementFailedReadTransactionsCount();
112 shardStats.incrementFailedReadTransactionsCount();
115 //now let us get from MBeanServer what is the transaction count.
117 mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
118 Assert.assertEquals(attribute, 2L);
122 public void testResetTransactionCounters() throws Exception {
124 //let us increment committed transactions count and then check
125 shardStats.incrementCommittedTransactionCount();
126 shardStats.incrementCommittedTransactionCount();
127 shardStats.incrementCommittedTransactionCount();
129 //now let us get from MBeanServer what is the transaction count.
130 Object attribute = mbeanServer.getAttribute(testMBeanName,
131 "CommittedTransactionsCount");
132 Assert.assertEquals(attribute, 3L);
134 //let us increment FailedReadTransactions count and then check
135 shardStats.incrementFailedReadTransactionsCount();
136 shardStats.incrementFailedReadTransactionsCount();
139 //now let us get from MBeanServer what is the transaction count.
141 mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
142 Assert.assertEquals(attribute, 2L);
145 //here we will reset the counters and check the above ones are 0 after reset
146 mbeanServer.invoke(testMBeanName, "resetTransactionCounters", null, null);
148 //now let us get from MBeanServer what is the transaction count.
149 attribute = mbeanServer.getAttribute(testMBeanName,
150 "CommittedTransactionsCount");
151 Assert.assertEquals(attribute, 0L);
154 mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
155 Assert.assertEquals(attribute, 0L);