2 * Copyright © 2019 Bell Canada.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.blueprintsprocessor.core.cluster
21 import io.mockk.verify
22 import kotlinx.coroutines.runBlocking
23 import org.junit.Before
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.ClusterLock
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
27 import kotlin.test.assertEquals
29 class BluePrintClusterExtensionsTest {
31 private lateinit var clusterLockMock: ClusterLock
35 clusterLockMock = mockk()
36 every { clusterLockMock.name() } returns "mock-lock"
40 fun `executeWithLock - should call unlock and return block result`() {
42 every { runBlocking { clusterLockMock.tryLock(more(0L)) } } returns true
43 every { runBlocking { clusterLockMock.unLock() } } returns Unit
45 val result = clusterLockMock.executeWithLock(1_000) { "result" }
47 verify { runBlocking { clusterLockMock.unLock() } }
48 assertEquals("result", result)
53 fun `executeWithLock - should call unlock even when block throws exception`() {
55 every { runBlocking { clusterLockMock.tryLock(more(0L)) } } returns true
56 every { runBlocking { clusterLockMock.unLock() } } returns Unit
59 clusterLockMock.executeWithLock(1_000) { throw RuntimeException("It crashed") }
60 } catch (e: Exception) {
63 verify { runBlocking { clusterLockMock.unLock() } }
67 @Test(expected = BluePrintException::class)
68 fun `executeWithLock - should throw exception when lock was not acquired within timeout`() {
70 every { runBlocking { clusterLockMock.tryLock(eq(0L)) } } returns false
71 clusterLockMock.executeWithLock(0) { "Will not run" }