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