+ private static class ConcurrencyTestRunnable implements Runnable {
+ private final RestTemplate restTemplate = new RestTemplate();
+ private final String baseUrl;
+ static AtomicInteger nextCount = new AtomicInteger(0);
+ private final int count;
+ private final RepositorySupervision supervision;
+
+ ConcurrencyTestRunnable(String baseUrl, RepositorySupervision supervision) {
+ this.baseUrl = baseUrl;
+ this.count = nextCount.incrementAndGet();
+ this.supervision = supervision;
+ }
+
+ @Override
+ public void run() {
+ for (int i = 0; i < 100; ++i) {
+ if (i % 10 == 0) {
+ this.supervision.checkAllRics();
+ }
+ String name = "policy:" + count + ":" + i;
+ putPolicy(name);
+ deletePolicy(name);
+ }
+ }
+
+ private void putPolicy(String name) {
+ String putUrl = baseUrl + "/policy?type=type1&instance=" + name + "&ric=ric1&service=service1";
+ this.restTemplate.put(putUrl, createJsonHttpEntity("{}"));
+ }
+
+ private void deletePolicy(String name) {
+ String deleteUrl = baseUrl + "/policy?instance=" + name;
+ this.restTemplate.delete(deleteUrl);
+ }
+ }
+
+ @Test
+ public void testConcurrency() throws Exception {
+ final Instant startTime = Instant.now();
+ List<Thread> threads = new ArrayList<>();
+ addRic("ric1");
+ addPolicyType("type1", "ric1");
+
+ for (int i = 0; i < 100; ++i) {
+ Thread t = new Thread(new ConcurrencyTestRunnable(baseUrl(), this.supervision), "TestThread_" + i);
+ t.start();
+ threads.add(t);
+ }
+ for (Thread t : threads) {
+ t.join();
+ }
+ assertThat(policies.size()).isEqualTo(0);
+ System.out.println("Concurrency test took " + Duration.between(startTime, Instant.now()));
+ }
+