Update Logging
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / client / restproperties / ThreadedReadTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client.restproperties;
22
23 import static org.hamcrest.CoreMatchers.equalTo;
24 import static org.junit.Assert.assertThat;
25
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30 import java.util.concurrent.Callable;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Executors;
34 import java.util.concurrent.Future;
35 import java.util.concurrent.ScheduledExecutorService;
36 import java.util.concurrent.TimeUnit;
37
38 import org.junit.Test;
39 import org.onap.so.client.RestPropertiesLoader;
40 import org.onap.so.client.aai.AAIProperties;
41 import org.onap.so.BaseTest;
42
43 public class ThreadedReadTest {
44         @Test
45         public void allAtOnce() throws InterruptedException {
46                 ExecutorService executorService = Executors.newFixedThreadPool(10);
47                 
48                 Callable<AAIProperties> callableTask = () -> {
49                         return RestPropertiesLoader.getInstance().getNewImpl(AAIProperties.class);
50                 };
51                 List<Callable<AAIProperties>> callableTasks = new ArrayList<>();
52                 
53                 callableTasks.add(callableTask);
54                 callableTasks.add(callableTask);
55                 callableTasks.add(callableTask);
56                 callableTasks.add(callableTask);
57                 callableTasks.add(callableTask);
58                 
59                 List<Future<AAIProperties>> futures = executorService.invokeAll(callableTasks);
60                 
61                 Set<AAIProperties> results = new HashSet<>();
62                 futures.forEach(item -> {
63                         try {
64                                 results.add(item.get());
65                         } catch (InterruptedException | ExecutionException e) {
66                                 throw new RuntimeException(e);
67                         }
68                 });
69                 
70                 assertThat("expect all unique results", results.size(), equalTo(callableTasks.size()));
71                 
72         }
73         
74         @Test
75         public void executeOverTime() {
76                 ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);
77                 
78                 Callable<AAIProperties> callableTask = () -> {
79                         TimeUnit.MILLISECONDS.sleep(500);
80                         return RestPropertiesLoader.getInstance().getNewImpl(AAIProperties.class);
81                 };
82                 List<Callable<AAIProperties>> callableTasks = new ArrayList<>();
83                 
84                 callableTasks.add(callableTask);
85                 callableTasks.add(callableTask);
86                 callableTasks.add(callableTask);
87                 callableTasks.add(callableTask);
88                 callableTasks.add(callableTask);
89                 
90                 Set<AAIProperties> results = new HashSet<>();
91                 callableTasks.forEach(callable -> {
92                         try {
93                                 TimeUnit.MILLISECONDS.sleep(300);
94                                 Future<AAIProperties> result = executorService.submit(callable);
95                                 results.add(result.get());
96                         } catch (InterruptedException | ExecutionException e) {
97                                 throw new RuntimeException(e);
98                         }
99                 });
100                 
101                 assertThat("expect all unique results", results.size(), equalTo(callableTasks.size()));
102         }
103         
104 }