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.jmx.mbeans.shard;
10 import org.junit.After;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
16 import javax.management.MBeanServer;
17 import javax.management.ObjectName;
19 import java.lang.management.ManagementFactory;
20 import java.text.SimpleDateFormat;
21 import java.util.Date;
23 public class ShardStatsTest {
24 private MBeanServer mbeanServer;
25 private ShardStats shardStats;
26 private ObjectName testMBeanName;
29 public void setUp() throws Exception {
31 shardStats = new ShardStats("shard-1", "DataStore");
32 shardStats.registerMBean();
33 mbeanServer = ManagementFactory.getPlatformMBeanServer();
35 AbstractMXBean.BASE_JMX_PREFIX + "type=" + shardStats
36 .getMBeanType() + ",Category=" +
37 shardStats.getMBeanCategory() + ",name=" +
38 shardStats.getMBeanName();
39 testMBeanName = new ObjectName(objectName);
43 public void tearDown() throws Exception {
44 shardStats.unregisterMBean();
48 public void testGetShardName() throws Exception {
50 Object attribute = mbeanServer.getAttribute(testMBeanName, "ShardName");
51 Assert.assertEquals(attribute, "shard-1");
56 public void testGetCommittedTransactionsCount() throws Exception {
57 //let us increment some transactions count and then check
58 shardStats.incrementCommittedTransactionCount();
59 shardStats.incrementCommittedTransactionCount();
60 shardStats.incrementCommittedTransactionCount();
62 //now let us get from MBeanServer what is the transaction count.
63 Object attribute = mbeanServer.getAttribute(testMBeanName,
64 "CommittedTransactionsCount");
65 Assert.assertEquals(attribute, 3L);
71 public void testGetLastCommittedTransactionTime() throws Exception {
72 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
73 Assert.assertEquals(shardStats.getLastCommittedTransactionTime(),
74 sdf.format(new Date(0L)));
75 long millis = System.currentTimeMillis();
76 shardStats.setLastCommittedTransactionTime(millis);
78 //now let us get from MBeanServer what is the transaction count.
79 Object attribute = mbeanServer.getAttribute(testMBeanName,
80 "LastCommittedTransactionTime");
81 Assert.assertEquals(attribute, sdf.format(new Date(millis)));
82 Assert.assertNotEquals(attribute,
83 sdf.format(new Date(millis - 1)));
88 public void testGetFailedTransactionsCount() throws Exception {
89 //let us increment some transactions count and then check
90 shardStats.incrementFailedTransactionsCount();
91 shardStats.incrementFailedTransactionsCount();
94 //now let us get from MBeanServer what is the transaction count.
96 mbeanServer.getAttribute(testMBeanName, "FailedTransactionsCount");
97 Assert.assertEquals(attribute, 2L);
101 public void testGetAbortTransactionsCount() throws Exception {
102 //let us increment AbortTransactions count and then check
103 shardStats.incrementAbortTransactionsCount();
104 shardStats.incrementAbortTransactionsCount();
107 //now let us get from MBeanServer what is the transaction count.
109 mbeanServer.getAttribute(testMBeanName, "AbortTransactionsCount");
110 Assert.assertEquals(attribute, 2L);
114 public void testGetFailedReadTransactionsCount() throws Exception {
115 //let us increment FailedReadTransactions count and then check
116 shardStats.incrementFailedReadTransactionsCount();
117 shardStats.incrementFailedReadTransactionsCount();
120 //now let us get from MBeanServer what is the transaction count.
122 mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
123 Assert.assertEquals(attribute, 2L);
127 public void testResetTransactionCounters() throws Exception {
129 //let us increment committed transactions count and then check
130 shardStats.incrementCommittedTransactionCount();
131 shardStats.incrementCommittedTransactionCount();
132 shardStats.incrementCommittedTransactionCount();
134 //now let us get from MBeanServer what is the transaction count.
135 Object attribute = mbeanServer.getAttribute(testMBeanName,
136 "CommittedTransactionsCount");
137 Assert.assertEquals(attribute, 3L);
139 //let us increment FailedReadTransactions count and then check
140 shardStats.incrementFailedReadTransactionsCount();
141 shardStats.incrementFailedReadTransactionsCount();
144 //now let us get from MBeanServer what is the transaction count.
146 mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
147 Assert.assertEquals(attribute, 2L);
150 //here we will reset the counters and check the above ones are 0 after reset
151 mbeanServer.invoke(testMBeanName, "resetTransactionCounters", null, null);
153 //now let us get from MBeanServer what is the transaction count.
154 attribute = mbeanServer.getAttribute(testMBeanName,
155 "CommittedTransactionsCount");
156 Assert.assertEquals(attribute, 0L);
159 mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
160 Assert.assertEquals(attribute, 0L);