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 java.lang.RuntimeException
28 import kotlin.test.assertEquals
30 class BluePrintClusterExtensionsTest {
32 private lateinit var clusterLockMock: ClusterLock
36 clusterLockMock = mockk()
37 every { clusterLockMock.name() } returns "mock-lock"
41 fun `executeWithLock - should call unlock and return block result`() {
43 every { runBlocking { clusterLockMock.tryLock(more(0L)) } } returns true
44 every { runBlocking { clusterLockMock.unLock() } } returns Unit
46 val result = clusterLockMock.executeWithLock(1_000) { "result" }
48 verify { runBlocking { clusterLockMock.unLock() } }
49 assertEquals("result", result)
54 fun `executeWithLock - should call unlock even when block throws exception`() {
56 every { runBlocking { clusterLockMock.tryLock(more(0L)) } } returns true
57 every { runBlocking { clusterLockMock.unLock() } } returns Unit
60 clusterLockMock.executeWithLock(1_000) { throw RuntimeException("It crashed") }
61 } 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" }