Merge "Removed `which` dependency, now using proper shell builtin."
[controller.git] / opendaylight / config / threadpool-config-impl / src / test / java / org / opendaylight / controller / config / threadpool / fixed / FixedThreadPoolConfigBeanTest.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.threadpool.fixed;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThat;
12 import static org.junit.Assert.fail;
13 import static org.junit.matchers.JUnitMatchers.containsString;
14
15 import java.lang.management.ManagementFactory;
16 import java.lang.management.ThreadInfo;
17 import java.lang.management.ThreadMXBean;
18 import java.util.ArrayList;
19 import java.util.List;
20 import javax.management.InstanceAlreadyExistsException;
21 import javax.management.InstanceNotFoundException;
22 import javax.management.ObjectName;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.config.api.ConflictingVersionException;
26 import org.opendaylight.controller.config.api.ValidationException;
27 import org.opendaylight.controller.config.api.jmx.CommitStatus;
28 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
29 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
30 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
31 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleFactory;
32 import org.opendaylight.controller.config.yang.threadpool.impl.NamingThreadFactoryModuleMXBean;
33 import org.opendaylight.controller.config.yang.threadpool.impl.fixed.FixedThreadPoolModuleFactory;
34 import org.opendaylight.controller.config.yang.threadpool.impl.fixed.FixedThreadPoolModuleMXBean;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class FixedThreadPoolConfigBeanTest extends AbstractConfigTest {
39     private static final Logger logger = LoggerFactory.getLogger(FixedThreadPoolConfigBeanTest.class);
40
41     private FixedThreadPoolModuleFactory factory;
42     private final String nameInstance = "fixedInstance";
43
44     @Before
45     public void setUp() {
46         factory = new FixedThreadPoolModuleFactory();
47         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, factory,
48                 new NamingThreadFactoryModuleFactory()));
49     }
50
51     @Test
52     public void testCreateBean() throws InstanceAlreadyExistsException, ValidationException,
53             ConflictingVersionException {
54         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
55         createFixed(transaction, nameInstance, 2, nameInstance);
56
57         transaction.validateConfig();
58         CommitStatus status = transaction.commit();
59
60         assertBeanCount(1, factory.getImplementationName());
61         assertStatus(status, 2, 0, 0);
62     }
63
64     @Test
65     public void testReusingOldInstance() throws InstanceAlreadyExistsException, ConflictingVersionException,
66             ValidationException {
67         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
68         createFixed(transaction, nameInstance, 4, nameInstance);
69
70         transaction.validateConfig();
71         transaction.commit();
72
73         assertBeanCount(1, factory.getImplementationName());
74
75         transaction = configRegistryClient.createTransaction();
76         CommitStatus status = transaction.commit();
77
78         assertBeanCount(1, factory.getImplementationName());
79         assertStatus(status, 0, 0, 2);
80     }
81
82     @Test
83     public void testNegative() throws ConflictingVersionException, ValidationException, InstanceAlreadyExistsException {
84         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
85
86         createFixed(transaction, nameInstance, 5, nameInstance);
87         transaction.commit();
88
89         transaction = configRegistryClient.createTransaction();
90         try {
91             createFixed(transaction, nameInstance, 0, nameInstance);
92             fail();
93         } catch (InstanceAlreadyExistsException e) {
94             assertThat(
95                     e.getMessage(),
96                     containsString("There is an instance registered with name ModuleIdentifier{factoryName='threadpool-fixed', instanceName='fixedInstance'}"));
97         }
98     }
99
100     private int countThreadsByPrefix(String prefix) {
101         ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
102         int result = 0;
103         List<String> names = new ArrayList<>();
104         for (ThreadInfo threadInfo : threadMXBean.dumpAllThreads(false, false)) {
105             names.add(threadInfo.getThreadName());
106             if (threadInfo.getThreadName().startsWith(prefix)) {
107                 result++;
108             }
109         }
110         logger.info("Current threads {}", names);
111         return result;
112     }
113
114     @Test
115     public void testDestroy() throws InstanceAlreadyExistsException, ValidationException, ConflictingVersionException,
116             InstanceNotFoundException, InterruptedException {
117
118         String prefix = org.apache.commons.lang3.RandomStringUtils.randomAlphabetic(10);
119
120         int numberOfThreads = 100;
121         int threadCount1 = countThreadsByPrefix(prefix);
122         assertEquals(0, threadCount1);
123         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
124
125         createFixed(transaction, nameInstance, numberOfThreads, prefix);
126         transaction.commit();
127         int threadCount2 = countThreadsByPrefix(prefix);
128         assertEquals(numberOfThreads, threadCount2);
129
130         transaction = configRegistryClient.createTransaction();
131         transaction.destroyModule(factory.getImplementationName(), nameInstance);
132         CommitStatus status = transaction.commit();
133
134         assertBeanCount(0, factory.getImplementationName());
135         assertStatus(status, 0, 0, 1);
136
137         for (int i = 0; i < 60; i++) {
138             if (countThreadsByPrefix(prefix) == 0) {
139                 return;
140             }
141             Thread.sleep(1000);
142         }
143         assertEquals(0, countThreadsByPrefix(prefix));
144     }
145
146     @Test
147     public void testValidationException() throws InstanceAlreadyExistsException {
148         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
149         createFixed(transaction, nameInstance, -1, nameInstance);
150         try {
151             transaction.validateConfig();
152             fail();
153         } catch (ValidationException e) {
154             assertThat(e.getMessage(), containsString("MaxThreadCount must be greater than zero"));
155         }
156     }
157
158     private ObjectName createFixed(ConfigTransactionJMXClient transaction, String name, int numberOfThreads, String prefix)
159             throws InstanceAlreadyExistsException {
160         ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), name);
161         FixedThreadPoolModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, FixedThreadPoolModuleMXBean.class);
162         mxBean.setMaxThreadCount(numberOfThreads);
163
164         ObjectName threadFactoryON = transaction.createModule(NamingThreadFactoryModuleFactory.NAME, "naming");
165         NamingThreadFactoryModuleMXBean namingThreadFactoryModuleMXBean = transaction.newMXBeanProxy(threadFactoryON,
166                 NamingThreadFactoryModuleMXBean.class);
167         namingThreadFactoryModuleMXBean.setNamePrefix(prefix);
168
169         mxBean.setThreadFactory(threadFactoryON);
170
171         return nameCreated;
172     }
173
174 }