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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
13 import java.lang.management.ManagementFactory;
14 import java.text.SimpleDateFormat;
15 import java.util.Date;
16 import javax.management.MBeanServer;
17 import javax.management.ObjectName;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
23 public class ShardStatsTest {
24 private MBeanServer mbeanServer;
25 private ShardStats shardStats;
26 private ObjectName testMBeanName;
29 public void setUp() throws Exception {
30 shardStats = new ShardStats("shard-1", "DataStore", null);
31 shardStats.registerMBean();
32 mbeanServer = ManagementFactory.getPlatformMBeanServer();
33 String objectName = AbstractMXBean.BASE_JMX_PREFIX + "type=" + shardStats.getMBeanType() + ",Category="
34 + shardStats.getMBeanCategory() + ",name=" + shardStats.getMBeanName();
35 testMBeanName = new ObjectName(objectName);
39 public void tearDown() {
40 shardStats.unregisterMBean();
44 public void testGetShardName() throws Exception {
45 assertEquals("shard-1", mbeanServer.getAttribute(testMBeanName, "ShardName"));
49 public void testGetCommittedTransactionsCount() throws Exception {
50 //let us increment some transactions count and then check
51 shardStats.incrementCommittedTransactionCount();
52 shardStats.incrementCommittedTransactionCount();
53 shardStats.incrementCommittedTransactionCount();
55 //now let us get from MBeanServer what is the transaction count.
56 assertEquals(3L, mbeanServer.getAttribute(testMBeanName, "CommittedTransactionsCount"));
60 public void testGetLastCommittedTransactionTime() throws Exception {
61 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
62 assertEquals(sdf.format(new Date(0L)), shardStats.getLastCommittedTransactionTime());
63 long millis = System.currentTimeMillis();
64 shardStats.setLastCommittedTransactionTime(millis);
66 //now let us get from MBeanServer what is the transaction count.
67 Object attribute = mbeanServer.getAttribute(testMBeanName,
68 "LastCommittedTransactionTime");
69 assertEquals(sdf.format(new Date(millis)), attribute);
70 assertNotEquals(attribute, sdf.format(new Date(millis - 1)));
74 public void testGetFailedTransactionsCount() throws Exception {
75 //let us increment some transactions count and then check
76 shardStats.incrementFailedTransactionsCount();
77 shardStats.incrementFailedTransactionsCount();
79 //now let us get from MBeanServer what is the transaction count.
80 assertEquals(2L, mbeanServer.getAttribute(testMBeanName, "FailedTransactionsCount"));
84 public void testGetAbortTransactionsCount() throws Exception {
85 //let us increment AbortTransactions count and then check
86 shardStats.incrementAbortTransactionsCount();
87 shardStats.incrementAbortTransactionsCount();
89 //now let us get from MBeanServer what is the transaction count.
90 assertEquals(2L, mbeanServer.getAttribute(testMBeanName, "AbortTransactionsCount"));
94 public void testGetFailedReadTransactionsCount() throws Exception {
95 //let us increment FailedReadTransactions count and then check
96 shardStats.incrementFailedReadTransactionsCount();
97 shardStats.incrementFailedReadTransactionsCount();
99 //now let us get from MBeanServer what is the transaction count.
100 assertEquals(2L, mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount"));
104 public void testResetTransactionCounters() throws Exception {
105 //let us increment committed transactions count and then check
106 shardStats.incrementCommittedTransactionCount();
107 shardStats.incrementCommittedTransactionCount();
108 shardStats.incrementCommittedTransactionCount();
110 //now let us get from MBeanServer what is the transaction count.
111 assertEquals(3L, mbeanServer.getAttribute(testMBeanName, "CommittedTransactionsCount"));
113 //let us increment FailedReadTransactions count and then check
114 shardStats.incrementFailedReadTransactionsCount();
115 shardStats.incrementFailedReadTransactionsCount();
117 //now let us get from MBeanServer what is the transaction count.
118 assertEquals(2L, mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount"));
120 //here we will reset the counters and check the above ones are 0 after reset
121 mbeanServer.invoke(testMBeanName, "resetTransactionCounters", null, null);
123 //now let us get from MBeanServer what is the transaction count.
124 assertEquals(0L, mbeanServer.getAttribute(testMBeanName, "CommittedTransactionsCount"));
125 assertEquals(0L, mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount"));