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;
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.List;
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;
38 import org.junit.Test;
39 import org.onap.so.BaseTest;
40 import org.onap.so.client.RestPropertiesLoader;
41 import org.onap.so.client.aai.AAIProperties;
43 public class ThreadedReadTest {
45 public void allAtOnce() throws InterruptedException {
46 ExecutorService executorService = Executors.newFixedThreadPool(10);
48 Callable<AAIProperties> callableTask = () -> {
49 return RestPropertiesLoader.getInstance().getNewImpl(AAIProperties.class);
51 List<Callable<AAIProperties>> callableTasks = new ArrayList<>();
53 callableTasks.add(callableTask);
54 callableTasks.add(callableTask);
55 callableTasks.add(callableTask);
56 callableTasks.add(callableTask);
57 callableTasks.add(callableTask);
59 List<Future<AAIProperties>> futures = executorService.invokeAll(callableTasks);
61 Set<AAIProperties> results = new HashSet<>();
62 futures.forEach(item -> {
64 results.add(item.get());
65 } catch (InterruptedException | ExecutionException e) {
66 throw new RuntimeException(e);
70 assertThat("expect all unique results", results.size(), equalTo(callableTasks.size()));
75 public void executeOverTime() {
76 ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);
78 Callable<AAIProperties> callableTask = () -> {
79 TimeUnit.MILLISECONDS.sleep(500);
80 return RestPropertiesLoader.getInstance().getNewImpl(AAIProperties.class);
82 List<Callable<AAIProperties>> callableTasks = new ArrayList<>();
84 callableTasks.add(callableTask);
85 callableTasks.add(callableTask);
86 callableTasks.add(callableTask);
87 callableTasks.add(callableTask);
88 callableTasks.add(callableTask);
90 Set<AAIProperties> results = new HashSet<>();
91 callableTasks.forEach(callable -> {
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);
101 assertThat("expect all unique results", results.size(), equalTo(callableTasks.size()));