5fe5f9496e2741beaf8721db19fac513acf5a37b
[controller.git] / opendaylight / config / netty-timer-config / src / test / java / org / opendaylight / controller / config / yang / netty / timer / HashedWheelTimerModuleTest.java
1 /*
2  * Copyright (c) 2013 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.config.yang.netty.timer;
9
10 import javax.management.InstanceAlreadyExistsException;
11 import javax.management.InstanceNotFoundException;
12 import javax.management.ObjectName;
13
14 import junit.framework.Assert;
15
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.config.api.ConflictingVersionException;
19 import org.opendaylight.controller.config.api.ValidationException;
20 import org.opendaylight.controller.config.api.jmx.CommitStatus;
21 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
22 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
23 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
24 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleFactory;
25 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleMXBean;
26
27 public class HashedWheelTimerModuleTest extends AbstractConfigTest {
28
29     private HashedWheelTimerModuleFactory factory;
30     private NamingThreadFactoryModuleFactory threadFactory;
31     private final String instanceName = "hashed-wheel-timer1";
32
33     @Before
34     public void setUp() {
35         factory = new HashedWheelTimerModuleFactory();
36         threadFactory = new NamingThreadFactoryModuleFactory();
37         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, factory, threadFactory));
38     }
39
40     public void testValidationExceptionTickDuration() throws InstanceAlreadyExistsException {
41         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
42         try {
43             createInstance(transaction, instanceName, 0L, 10, true);
44             transaction.validateConfig();
45             Assert.fail();
46         } catch (ValidationException e) {
47             Assert.assertTrue(e.getMessage().contains("TickDuration value must be greater than 0"));
48         }
49     }
50
51     public void testValidationExceptionTicksPerWheel() throws InstanceAlreadyExistsException {
52         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
53         try {
54             createInstance(transaction, instanceName, 500L, 0, true);
55             transaction.validateConfig();
56             Assert.fail();
57         } catch (ValidationException e) {
58             Assert.assertTrue(e.getMessage().contains("TicksPerWheel value must be greater than 0"));
59         }
60     }
61
62     @Test
63     public void testCreateBean() throws InstanceAlreadyExistsException, ValidationException,
64             ConflictingVersionException {
65         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
66
67         createInstance(transaction, instanceName, 500L, 10, true);
68         createInstance(transaction, instanceName + 1, null, null, false);
69         createInstance(transaction, instanceName + 2, 500L, 10, false);
70         createInstance(transaction, instanceName + 3, 500L, null, false);
71         transaction.validateConfig();
72         CommitStatus status = transaction.commit();
73
74         assertBeanCount(4, factory.getImplementationName());
75         assertStatus(status, 5, 0, 0);
76     }
77
78     @Test
79     public void testReusingOldInstance() throws InstanceAlreadyExistsException, ConflictingVersionException,
80             ValidationException {
81
82         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
83         createInstance(transaction, instanceName, 500L, 10, true);
84
85         transaction.commit();
86
87         transaction = configRegistryClient.createTransaction();
88         assertBeanCount(1, factory.getImplementationName());
89         CommitStatus status = transaction.commit();
90
91         assertBeanCount(1, factory.getImplementationName());
92         assertStatus(status, 0, 0, 2);
93     }
94
95     @Test
96     public void testReconfigure() throws InstanceAlreadyExistsException, ConflictingVersionException,
97             ValidationException, InstanceNotFoundException {
98
99         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
100         createInstance(transaction, instanceName, 500L, 10, true);
101         transaction.commit();
102
103         transaction = configRegistryClient.createTransaction();
104         assertBeanCount(1, factory.getImplementationName());
105         HashedWheelTimerModuleMXBean mxBean = transaction.newMBeanProxy(
106                 transaction.lookupConfigBean(factory.getImplementationName(), instanceName),
107                 HashedWheelTimerModuleMXBean.class);
108         mxBean.setTicksPerWheel(20);
109         CommitStatus status = transaction.commit();
110
111         assertBeanCount(1, factory.getImplementationName());
112         assertStatus(status, 0, 1, 1);
113     }
114
115     private ObjectName createInstance(ConfigTransactionJMXClient transaction, String instanceName,
116             final Long tickDuration, final Integer ticksPerWheel, final boolean hasThreadfactory)
117             throws InstanceAlreadyExistsException {
118         ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), instanceName);
119         HashedWheelTimerModuleMXBean mxBean = transaction
120                 .newMBeanProxy(nameCreated, HashedWheelTimerModuleMXBean.class);
121         mxBean.setTickDuration(tickDuration);
122         mxBean.setTicksPerWheel(ticksPerWheel);
123         if (hasThreadfactory) {
124             mxBean.setThreadFactory(createThreadfactoryInstance(transaction, "thread-factory1", "th"));
125         }
126         return nameCreated;
127     }
128
129     private ObjectName createThreadfactoryInstance(ConfigTransactionJMXClient transaction, String instanceName,
130             final String namePrefix) throws InstanceAlreadyExistsException {
131         ObjectName nameCreated = transaction.createModule(threadFactory.getImplementationName(), instanceName);
132         NamingThreadFactoryModuleMXBean mxBean = transaction.newMBeanProxy(nameCreated,
133                 NamingThreadFactoryModuleMXBean.class);
134         mxBean.setNamePrefix(namePrefix);
135         return nameCreated;
136     }
137
138 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.