Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardStatsTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12
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;
22
23 public class ShardStatsTest {
24     private MBeanServer mbeanServer;
25     private ShardStats shardStats;
26     private ObjectName testMBeanName;
27
28     @Before
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);
36     }
37
38     @After
39     public void tearDown() {
40         shardStats.unregisterMBean();
41     }
42
43     @Test
44     public void testGetShardName() throws Exception {
45         assertEquals("shard-1", mbeanServer.getAttribute(testMBeanName, "ShardName"));
46     }
47
48     @Test
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();
54
55         //now let us get from MBeanServer what is the transaction count.
56         assertEquals(3L, mbeanServer.getAttribute(testMBeanName, "CommittedTransactionsCount"));
57     }
58
59     @Test
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);
65
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)));
71     }
72
73     @Test
74     public void testGetFailedTransactionsCount() throws Exception {
75         //let us increment some transactions count and then check
76         shardStats.incrementFailedTransactionsCount();
77         shardStats.incrementFailedTransactionsCount();
78
79         //now let us get from MBeanServer what is the transaction count.
80         assertEquals(2L, mbeanServer.getAttribute(testMBeanName, "FailedTransactionsCount"));
81     }
82
83     @Test
84     public void testGetAbortTransactionsCount() throws Exception {
85         //let us increment AbortTransactions count and then check
86         shardStats.incrementAbortTransactionsCount();
87         shardStats.incrementAbortTransactionsCount();
88
89         //now let us get from MBeanServer what is the transaction count.
90         assertEquals(2L, mbeanServer.getAttribute(testMBeanName, "AbortTransactionsCount"));
91     }
92
93     @Test
94     public void testGetFailedReadTransactionsCount() throws Exception {
95         //let us increment FailedReadTransactions count and then check
96         shardStats.incrementFailedReadTransactionsCount();
97         shardStats.incrementFailedReadTransactionsCount();
98
99         //now let us get from MBeanServer what is the transaction count.
100         assertEquals(2L, mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount"));
101     }
102
103     @Test
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();
109
110         //now let us get from MBeanServer what is the transaction count.
111         assertEquals(3L, mbeanServer.getAttribute(testMBeanName, "CommittedTransactionsCount"));
112
113         //let us increment FailedReadTransactions count and then check
114         shardStats.incrementFailedReadTransactionsCount();
115         shardStats.incrementFailedReadTransactionsCount();
116
117         //now let us get from MBeanServer what is the transaction count.
118         assertEquals(2L, mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount"));
119
120         //here we will reset the counters and check the above ones are 0 after reset
121         mbeanServer.invoke(testMBeanName, "resetTransactionCounters", null, null);
122
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"));
126     }
127 }