2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.restproperties;
23 import static org.hamcrest.CoreMatchers.equalTo;
24 import static org.junit.Assert.assertThat;
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
29 import java.util.concurrent.Callable;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.ExecutorService;
32 import java.util.concurrent.Executors;
33 import java.util.concurrent.Future;
34 import java.util.concurrent.ScheduledExecutorService;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.Test;
37 import org.onap.so.client.RestPropertiesLoader;
38 import org.onap.so.client.aai.AAIProperties;
39 import org.onap.so.BaseTest;
41 public class ThreadedReadTest {
43 public void allAtOnce() throws InterruptedException {
44 ExecutorService executorService = Executors.newFixedThreadPool(10);
46 Callable<AAIProperties> callableTask = () -> {
47 return RestPropertiesLoader.getInstance().getNewImpl(AAIProperties.class);
49 List<Callable<AAIProperties>> callableTasks = new ArrayList<>();
51 callableTasks.add(callableTask);
52 callableTasks.add(callableTask);
53 callableTasks.add(callableTask);
54 callableTasks.add(callableTask);
55 callableTasks.add(callableTask);
57 List<Future<AAIProperties>> futures = executorService.invokeAll(callableTasks);
59 Set<AAIProperties> results = new HashSet<>();
60 futures.forEach(item -> {
62 results.add(item.get());
63 } catch (InterruptedException | ExecutionException e) {
64 throw new RuntimeException(e);
68 assertThat("expect all unique results", results.size(), equalTo(callableTasks.size()));
73 public void executeOverTime() {
74 ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);
76 Callable<AAIProperties> callableTask = () -> {
77 TimeUnit.MILLISECONDS.sleep(500);
78 return RestPropertiesLoader.getInstance().getNewImpl(AAIProperties.class);
80 List<Callable<AAIProperties>> callableTasks = new ArrayList<>();
82 callableTasks.add(callableTask);
83 callableTasks.add(callableTask);
84 callableTasks.add(callableTask);
85 callableTasks.add(callableTask);
86 callableTasks.add(callableTask);
88 Set<AAIProperties> results = new HashSet<>();
89 callableTasks.forEach(callable -> {
91 TimeUnit.MILLISECONDS.sleep(300);
92 Future<AAIProperties> result = executorService.submit(callable);
93 results.add(result.get());
94 } catch (InterruptedException | ExecutionException e) {
95 throw new RuntimeException(e);
99 assertThat("expect all unique results", results.size(), equalTo(callableTasks.size()));