Merge "Bug 809: Enhancements to the toaster example"
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / threadpool / TestingFixedThreadPool.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.manager.testingservices.threadpool;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import java.io.Closeable;
14 import java.io.IOException;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.concurrent.Executor;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ThreadPoolExecutor;
21
22 import com.google.common.collect.Lists;
23
24 public class TestingFixedThreadPool implements TestingThreadPoolIfc, Closeable,
25         TestingModifiableThreadPoolIfc {
26     private final ThreadPoolExecutor executorService;
27     private final String uniqueName;
28
29     public static void cleanUp() {
30         for (ExecutorService executorService : allExecutors) {
31             executorService.shutdown();
32         }
33         allExecutors.clear();
34     }
35
36     // for verification purposes:
37     public static final List<ThreadPoolExecutor> allExecutors = Collections
38             .synchronizedList(Lists.<ThreadPoolExecutor>newLinkedList());
39
40     public TestingFixedThreadPool(int threadCount, String uniqueName) {
41         checkNotNull(uniqueName);
42         this.uniqueName = uniqueName;
43         executorService = (ThreadPoolExecutor) Executors
44                 .newFixedThreadPool(threadCount);
45         allExecutors.add(executorService);
46     }
47
48     @Override
49     public Executor getExecutor() {
50         return executorService;
51     }
52
53     @Override
54     public void close() throws IOException {
55         executorService.shutdown();
56         allExecutors.remove(executorService);
57
58     }
59
60     @Override
61     public int getMaxNumberOfThreads() {
62         return executorService.getMaximumPoolSize();
63     }
64
65     public String getUniqueName() {
66         return uniqueName;
67     }
68
69     @Override
70     public void setMaximumNumberOfThreads(int activeCount) {
71         checkArgument(activeCount > 0);
72         executorService.setMaximumPoolSize(activeCount);
73     }
74
75 }