2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.impl.inventory.sync
24 import com.hazelcast.map.IMap
25 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
26 import org.onap.cps.spi.model.DataNode
27 import spock.lang.Specification
29 import java.util.concurrent.ArrayBlockingQueue
30 import java.util.concurrent.locks.Lock
32 class ModuleSyncWatchdogSpec extends Specification {
34 def mockSyncUtils = Mock(ModuleOperationsUtils)
36 def static testQueueCapacity = 50 + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE
38 def moduleSyncWorkQueue = new ArrayBlockingQueue(testQueueCapacity)
40 def mockModuleSyncStartedOnCmHandles = Mock(IMap<String, Object>)
42 def mockModuleSyncTasks = Mock(ModuleSyncTasks)
44 def spiedAsyncTaskExecutor = Spy(AsyncTaskExecutor)
46 def mockWorkQueueLock = Mock(Lock)
48 def objectUnderTest = new ModuleSyncWatchdog(mockSyncUtils, moduleSyncWorkQueue , mockModuleSyncStartedOnCmHandles, mockModuleSyncTasks, spiedAsyncTaskExecutor, mockWorkQueueLock)
51 spiedAsyncTaskExecutor.setupThreadPool()
52 mockWorkQueueLock.tryLock() >> true
55 def 'Module sync advised cm handles with #scenario.'() {
56 given: 'sync utilities returns #numberOfAdvisedCmHandles advised cm handles'
57 mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(numberOfAdvisedCmHandles)
58 and: 'the executor has enough available threads'
59 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 3
60 when: ' module sync is started'
61 objectUnderTest.moduleSyncAdvisedCmHandles()
62 then: 'it performs #expectedNumberOfTaskExecutions tasks'
63 expectedNumberOfTaskExecutions * spiedAsyncTaskExecutor.executeTask(*_)
64 where: 'the following parameter are used'
65 scenario | numberOfAdvisedCmHandles || expectedNumberOfTaskExecutions
66 'less then 1 batch' | 1 || 1
67 'exactly 1 batch' | ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 1
68 '2 batches' | 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 2
69 'queue capacity' | testQueueCapacity || 3
70 'over queue capacity' | testQueueCapacity + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 3
73 def 'Module sync advised cm handles starts with no available threads.'() {
74 given: 'sync utilities returns a advise cm handles'
75 mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(1)
76 and: 'the executor first has no threads but has one thread on the second attempt'
77 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >>> [ 0, 1 ]
78 when: ' module sync is started'
79 objectUnderTest.moduleSyncAdvisedCmHandles()
80 then: 'it performs one task'
81 1 * spiedAsyncTaskExecutor.executeTask(*_)
84 def 'Module sync advised cm handles already handled.'() {
85 given: 'sync utilities returns a advise cm handles'
86 mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(1)
87 and: 'the executor has a thread available'
88 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 1
89 and: 'the semaphore cache indicates the cm handle is already being processed'
90 mockModuleSyncStartedOnCmHandles.putIfAbsent(*_) >> 'Started'
91 when: ' module sync is started'
92 objectUnderTest.moduleSyncAdvisedCmHandles()
93 then: 'it does NOT execute a task to process the (empty) batch'
94 0 * spiedAsyncTaskExecutor.executeTask(*_)
97 def 'Module sync with previous cm handle(s) left in work queue.'() {
98 given: 'there is still a cm handle in the queue'
99 moduleSyncWorkQueue.offer(new DataNode())
100 and: 'sync utilities returns many advise cm handles'
101 mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(500)
102 and: 'the executor has plenty threads available'
103 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 10
104 when: ' module sync is started'
105 objectUnderTest.moduleSyncAdvisedCmHandles()
106 then: 'it does executes only one task to process the remaining handle in the queue'
107 1 * spiedAsyncTaskExecutor.executeTask(*_)
110 def 'Reset failed cm handles.'() {
111 given: 'sync utilities returns failed cm handles'
112 def failedCmHandles = [new YangModelCmHandle()]
113 mockSyncUtils.getCmHandlesThatFailedModelSyncOrUpgrade() >> failedCmHandles
114 when: 'reset failed cm handles is started'
115 objectUnderTest.setPreviouslyLockedCmHandlesToAdvised()
116 then: 'it is delegated to the module sync task (service)'
117 1 * mockModuleSyncTasks.setCmHandlesToAdvised(failedCmHandles)
120 def createDataNodes(numberOfDataNodes) {
122 (1..numberOfDataNodes).each {dataNodes.add(new DataNode())}