2 * Copyright (c) 2013 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.config.threadpool.fixed;
10 import static org.junit.Assert.assertThat;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.junit.matchers.JUnitMatchers.containsString;
15 import java.lang.management.ManagementFactory;
16 import javax.management.InstanceAlreadyExistsException;
17 import javax.management.InstanceNotFoundException;
18 import javax.management.ObjectName;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.config.api.ConflictingVersionException;
22 import org.opendaylight.controller.config.api.ValidationException;
23 import org.opendaylight.controller.config.api.jmx.CommitStatus;
24 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
25 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
26 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
27 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleFactory;
28 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleMXBean;
29 import org.opendaylight.controller.config.yang.threadpool.impl.fixed.FixedThreadPoolModuleFactory;
30 import org.opendaylight.controller.config.yang.threadpool.impl.fixed.FixedThreadPoolModuleMXBean;
32 public class FixedThreadPoolConfigBeanTest extends AbstractConfigTest {
34 private FixedThreadPoolModuleFactory factory;
35 private final String nameInstance = "fixedInstance";
39 factory = new FixedThreadPoolModuleFactory();
40 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,factory,
41 new NamingThreadFactoryModuleFactory()));
45 public void testCreateBean() throws InstanceAlreadyExistsException, ValidationException,
46 ConflictingVersionException {
47 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
48 createFixed(transaction, nameInstance, 2);
50 transaction.validateConfig();
51 CommitStatus status = transaction.commit();
53 assertBeanCount(1, factory.getImplementationName());
54 assertStatus(status, 2, 0, 0);
58 public void testReusingOldInstance() throws InstanceAlreadyExistsException, ConflictingVersionException,
60 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
61 createFixed(transaction, nameInstance, 4);
63 transaction.validateConfig();
66 assertBeanCount(1, factory.getImplementationName());
68 transaction = configRegistryClient.createTransaction();
69 CommitStatus status = transaction.commit();
71 assertBeanCount(1, factory.getImplementationName());
72 assertStatus(status, 0, 0, 2);
76 public void testNegative() throws ConflictingVersionException, ValidationException, InstanceAlreadyExistsException {
77 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
79 createFixed(transaction, nameInstance, 5);
82 transaction = configRegistryClient.createTransaction();
84 createFixed(transaction, nameInstance, 0);
86 } catch (InstanceAlreadyExistsException e) {
89 containsString("There is an instance registered with name ModuleIdentifier{factoryName='threadpool-fixed', instanceName='fixedInstance'}"));
94 public void testDestroy() throws InstanceAlreadyExistsException, ValidationException, ConflictingVersionException,
95 InstanceNotFoundException {
97 int numberOfThreads = 100;
98 int threadCount1 = ManagementFactory.getThreadMXBean().getThreadCount();
99 assertTrue("Expected less than " + numberOfThreads + " threads, got " + threadCount1, threadCount1 < numberOfThreads);
100 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
101 createFixed(transaction, nameInstance, numberOfThreads);
102 transaction.commit();
103 int threadCount2 = ManagementFactory.getThreadMXBean().getThreadCount();
104 assertTrue("Expected more than " + numberOfThreads + " threads, got " + threadCount2, threadCount2 > numberOfThreads);
106 transaction = configRegistryClient.createTransaction();
107 transaction.destroyModule(factory.getImplementationName(), nameInstance);
108 CommitStatus status = transaction.commit();
110 assertBeanCount(0, factory.getImplementationName());
111 assertStatus(status, 0, 0, 1);
112 int threadCount3 = ManagementFactory.getThreadMXBean().getThreadCount();
113 assertTrue("Expected less than " + numberOfThreads + " threads, got " + threadCount3, threadCount3 < numberOfThreads);
117 public void testValidationException() throws InstanceAlreadyExistsException {
118 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
119 createFixed(transaction, nameInstance, -1);
121 transaction.validateConfig();
123 } catch (ValidationException e) {
124 assertThat(e.getMessage(), containsString("MaxThreadCount must be greater than zero"));
128 private ObjectName createFixed(ConfigTransactionJMXClient transaction, String name, int numberOfThreads)
129 throws InstanceAlreadyExistsException {
130 ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), name);
131 FixedThreadPoolModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, FixedThreadPoolModuleMXBean.class);
132 mxBean.setMaxThreadCount(numberOfThreads);
134 ObjectName threadFactoryON = transaction.createModule(NamingThreadFactoryModuleFactory.NAME, "naming");
135 NamingThreadFactoryModuleMXBean namingThreadFactoryModuleMXBean = transaction.newMXBeanProxy(threadFactoryON,
136 NamingThreadFactoryModuleMXBean.class);
137 namingThreadFactoryModuleMXBean.setNamePrefix("prefix");
139 mxBean.setThreadFactory(threadFactoryON);