2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.common.test;
24 import java.util.Random;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.onap.ccsdk.features.sdnr.wt.common.threading.GenericRunnableFactory;
29 import org.onap.ccsdk.features.sdnr.wt.common.threading.KeyBasedThreadpool;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class TestKeybasedThreadpool {
36 private static final Logger LOG = LoggerFactory.getLogger(TestKeybasedThreadpool.class);
37 private static final String KEY_A = "a";
38 private static final String KEY_B = "b";
39 private static final String KEY_C = "c";
40 private static final String KEY_D = "d";
45 GenericRunnableFactory<String, TestClass> factory1 =
46 new GenericRunnableFactory<String, TestKeybasedThreadpool.TestClass>() {
48 public Runnable create(final String key, final TestClass arg) {
49 return new Runnable() {
53 final String key2 = arg.value;
54 final long sleep = arg.sleep;
55 LOG.info("{}: sleeping now for {} seconds",key2, sleep);
57 Thread.sleep(sleep*1000);
58 } catch (InterruptedException e) {
59 LOG.error("InterruptedException",e);
60 Thread.currentThread().interrupt();
62 LOG.info("{}: finished",key2);
68 KeyBasedThreadpool<String, TestClass> threadpool = new KeyBasedThreadpool<String, TestClass>(10, 1, factory1);
69 threadpool.execute(KEY_A, new TestClass(KEY_A));
70 threadpool.execute(KEY_A, new TestClass(KEY_A));
71 threadpool.execute(KEY_A, new TestClass(KEY_A));
72 threadpool.execute(KEY_B, new TestClass(KEY_B));
73 threadpool.execute(KEY_C, new TestClass(KEY_C));
74 threadpool.execute(KEY_D, new TestClass(KEY_D));
75 threadpool.execute(KEY_D, new TestClass(KEY_D));
80 private static int counter=0;
83 public class TestClass {
84 protected final long sleep;
85 private final String value;
87 public TestClass(String value) {
89 this.value = value+ String.valueOf(counter++);
90 Random rnd = new Random();
91 this.sleep = rnd.nextInt(20);
92 LOG.info("instatiate {}",this);
96 public String toString() {
97 return "TestClass [sleep=" + sleep + ", value=" + value + "]";