e4d78599999d00eb7a7e952f184df6bf15123f31
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 Nordix Foundation. 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.ccsdk.oran.a1policymanagementservice.repository;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
25
26 import java.io.IOException;
27 import java.lang.invoke.MethodHandles;
28
29 import org.junit.jupiter.api.DisplayName;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockito.junit.jupiter.MockitoExtension;
33 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
34 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import reactor.core.publisher.Flux;
39 import reactor.core.publisher.Mono;
40 import reactor.test.StepVerifier;
41
42 @ExtendWith(MockitoExtension.class)
43 class LockTest {
44
45     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46
47     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
48     private void sleep() {
49         try {
50             Thread.sleep(100);
51         } catch (InterruptedException e) {
52             // Do nothing.
53         }
54     }
55
56     private void asynchUnlock(Lock.Grant grant, Lock lock) {
57         logger.info("Lock {} cnt: {}, exclusive: {}, queue: {}", grant.getLabel(), lock.getLockCounter(),
58                 lock.isExclusive, lock.lockRequestQueue.size());
59
60         Thread thread = new Thread(() -> {
61             sleep();
62             grant.unlockBlocking();
63         });
64         thread.start();
65     }
66
67     @Test
68     @DisplayName("test Lock")
69     void testLock() throws IOException, ServiceException {
70         Lock lock = new Lock("l1");
71         Lock.Grant grant = lock.lockBlocking(LockType.SHARED, "test");
72         grant.unlockBlocking();
73         assertThat(grant.getLabel()).isEqualTo("test");
74
75         grant = lock.lockBlocking(LockType.EXCLUSIVE, "");
76         asynchUnlock(grant, lock);
77
78         grant = lock.lockBlocking(LockType.SHARED, "");
79         grant.unlockBlocking();
80
81         assertThat(lock.getLockCounter()).isZero();
82     }
83
84     @Test
85     @DisplayName("test Reactive Lock")
86     void testReactiveLock() {
87         Lock lock = new Lock("l1");
88
89         Mono<?> l0 = lock.lock(LockType.EXCLUSIVE, "1").doOnNext(grant -> asynchUnlock(grant, lock));
90         Mono<?> l1 = lock.lock(LockType.SHARED, "2").doOnNext(grant -> asynchUnlock(grant, lock));
91         Mono<?> l2 = lock.lock(LockType.SHARED, "3").doOnNext(grant -> asynchUnlock(grant, lock));
92         Mono<?> l3 = lock.lock(LockType.EXCLUSIVE, "4").doOnNext(grant -> asynchUnlock(grant, lock));
93         Mono<?> l4 = lock.lock(LockType.SHARED, "5").doOnNext(grant -> asynchUnlock(grant, lock));
94
95         StepVerifier.create(Flux.zip(l0, l1, l2, l3, l4)) //
96                 .expectSubscription() //
97                 .expectNextCount(1) //
98                 .verifyComplete();
99
100         await().untilAsserted(() -> assertThat(lock.getLockCounter()).isZero());
101     }
102 }