86827569028cc4bc23ef22898aed93d0a762ec9f
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.common.test;
23
24 import java.util.Random;
25
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;
32
33 public class TestKeybasedThreadpool {
34
35
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";
41
42     @Ignore
43     @Test
44     public void test1() {
45         GenericRunnableFactory<String, TestClass> factory1 =
46                 new GenericRunnableFactory<String, TestKeybasedThreadpool.TestClass>() {
47                     @Override
48                     public Runnable create(final String key, final TestClass arg) {
49                         return new Runnable() {
50
51                             @Override
52                             public void run() {
53                                 final String key2 = arg.value;
54                                 final long sleep = arg.sleep;
55                                 LOG.info("{}: sleeping now for {} seconds",key2, sleep);
56                                 try {
57                                     Thread.sleep(sleep*1000);
58                                 } catch (InterruptedException e) {
59                                     LOG.error("InterruptedException",e);
60                                     Thread.currentThread().interrupt();
61                                 }
62                                 LOG.info("{}: finished",key2);
63                             }
64                         };
65                     }
66                 };
67         LOG.info("starting");
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));
76         threadpool.join();
77         LOG.info("done");
78     }
79
80     private static int counter=0;
81
82
83     public class TestClass {
84         protected final long sleep;
85         private final String value;
86
87         public TestClass(String value) {
88
89             this.value = value+ String.valueOf(counter++);
90             Random rnd = new Random();
91             this.sleep = rnd.nextInt(20);
92             LOG.info("instatiate {}",this);
93         }
94
95         @Override
96         public String toString() {
97             return "TestClass [sleep=" + sleep + ", value=" + value + "]";
98         }
99     }
100 }