2 * ========================LICENSE_START=================================
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
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.ccsdk.oran.a1policymanagementservice.repository;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
26 import java.io.IOException;
27 import java.lang.invoke.MethodHandles;
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;
38 import reactor.core.publisher.Flux;
39 import reactor.core.publisher.Mono;
40 import reactor.test.StepVerifier;
42 @ExtendWith(MockitoExtension.class)
45 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
47 @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
48 private void sleep() {
51 } catch (InterruptedException e) {
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());
60 Thread thread = new Thread(() -> {
62 grant.unlockBlocking();
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");
75 grant = lock.lockBlocking(LockType.EXCLUSIVE, "");
76 asynchUnlock(grant, lock);
78 grant = lock.lockBlocking(LockType.SHARED, "");
79 grant.unlockBlocking();
81 assertThat(lock.getLockCounter()).isZero();
85 @DisplayName("test Reactive Lock")
86 void testReactiveLock() {
87 Lock lock = new Lock("l1");
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));
95 StepVerifier.create(Flux.zip(l0, l1, l2, l3, l4)) //
96 .expectSubscription() //
97 .expectNextCount(1) //
100 await().untilAsserted(() -> assertThat(lock.getLockCounter()).isZero());