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.ncmp.impl.utils.Sleeper
27 import org.onap.cps.api.model.DataNode
28 import spock.lang.Specification
30 import java.util.concurrent.ArrayBlockingQueue
31 import java.util.concurrent.locks.Lock
33 class ModuleSyncWatchdogSpec extends Specification {
35 def mockModuleOperationsUtils = Mock(ModuleOperationsUtils)
37 def static testQueueCapacity = 50 + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE
39 def moduleSyncWorkQueue = new ArrayBlockingQueue(testQueueCapacity)
41 def mockModuleSyncStartedOnCmHandles = Mock(IMap<String, Object>)
43 def mockModuleSyncTasks = Mock(ModuleSyncTasks)
45 def spiedAsyncTaskExecutor = Spy(AsyncTaskExecutor)
47 def mockWorkQueueLock = Mock(Lock)
49 def spiedSleeper = Spy(Sleeper)
51 def objectUnderTest = new ModuleSyncWatchdog(mockModuleOperationsUtils, moduleSyncWorkQueue , mockModuleSyncStartedOnCmHandles, mockModuleSyncTasks, spiedAsyncTaskExecutor, mockWorkQueueLock, spiedSleeper)
54 spiedAsyncTaskExecutor.setupThreadPool()
57 def 'Module sync advised cm handles with #scenario.'() {
58 given: 'module sync utilities returns #numberOfAdvisedCmHandles advised cm handles'
59 mockModuleOperationsUtils.getAdvisedCmHandleIds() >> createCmHandleIds(numberOfAdvisedCmHandles)
60 and: 'module sync utilities returns no failed (locked) cm handles'
61 mockModuleOperationsUtils.getCmHandlesThatFailedModelSyncOrUpgrade() >> []
62 and: 'the work queue is not locked'
63 mockWorkQueueLock.tryLock() >> true
64 and: 'the executor has enough available threads'
65 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 3
66 when: ' module sync is started'
67 objectUnderTest.moduleSyncAdvisedCmHandles()
68 then: 'it performs #expectedNumberOfTaskExecutions tasks'
69 expectedNumberOfTaskExecutions * spiedAsyncTaskExecutor.executeTask(*_)
70 where: 'the following parameter are used'
71 scenario | numberOfAdvisedCmHandles || expectedNumberOfTaskExecutions
72 'none at all' | 0 || 0
73 'less then 1 batch' | 1 || 1
74 'exactly 1 batch' | ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 1
75 '2 batches' | 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 2
76 'queue capacity' | testQueueCapacity || 3
77 'over queue capacity' | testQueueCapacity + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 3
80 def 'Module sync cm handles starts with no available threads.'() {
81 given: 'module sync utilities returns a advise cm handles'
82 mockModuleOperationsUtils.getAdvisedCmHandleIds() >> createCmHandleIds(1)
83 and: 'the work queue is not locked'
84 mockWorkQueueLock.tryLock() >> true
85 and: 'the executor first has no threads but has one thread on the second attempt'
86 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >>> [ 0, 1 ]
87 when: ' module sync is started'
88 objectUnderTest.moduleSyncAdvisedCmHandles()
89 then: 'it performs one task'
90 1 * spiedAsyncTaskExecutor.executeTask(*_)
93 def 'Module sync advised cm handle already handled by other thread.'() {
94 given: 'module sync utilities returns an advised cm handle'
95 mockModuleOperationsUtils.getAdvisedCmHandleIds() >> createCmHandleIds(1)
96 and: 'the work queue is not locked'
97 mockWorkQueueLock.tryLock() >> true
98 and: 'the executor has a thread available'
99 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 1
100 and: 'the semaphore cache indicates the cm handle is already being processed'
101 mockModuleSyncStartedOnCmHandles.putIfAbsent(*_) >> 'Started'
102 when: ' module sync is started'
103 objectUnderTest.moduleSyncAdvisedCmHandles()
104 then: 'it does NOT execute a task to process the (empty) batch'
105 0 * spiedAsyncTaskExecutor.executeTask(*_)
108 def 'Module sync with previous cm handle(s) left in work queue.'() {
109 given: 'there is still a cm handle in the queue'
110 moduleSyncWorkQueue.offer('ch-1')
111 and: 'sync utilities returns many advise cm handles'
112 mockModuleOperationsUtils.getAdvisedCmHandleIds() >> createCmHandleIds(500)
113 and: 'the executor has plenty threads available'
114 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 10
115 when: ' module sync is started'
116 objectUnderTest.moduleSyncAdvisedCmHandles()
117 then: 'it does executes only one task to process the remaining handle in the queue'
118 1 * spiedAsyncTaskExecutor.executeTask(*_)
121 def 'Reset failed cm handles.'() {
122 given: 'module sync utilities returns failed cm handles'
123 def failedCmHandles = [new YangModelCmHandle()]
124 mockModuleOperationsUtils.getCmHandlesThatFailedModelSyncOrUpgrade() >> failedCmHandles
125 when: 'reset failed cm handles is started'
126 objectUnderTest.setPreviouslyLockedCmHandlesToAdvised()
127 then: 'it is delegated to the module sync task (service)'
128 1 * mockModuleSyncTasks.setCmHandlesToAdvised(failedCmHandles)
131 def 'Module Sync Locking.'() {
132 given: 'module sync utilities returns an advised cm handle'
133 mockModuleOperationsUtils.getAdvisedCmHandleIds() >> createCmHandleIds(1)
134 and: 'can lock is : #canLock'
135 mockWorkQueueLock.tryLock() >> canLock
136 when: 'attempt to populate the work queue'
137 objectUnderTest.populateWorkQueueIfNeeded()
138 then: 'the queue remains empty is #expectQueueRemainsEmpty'
139 assert moduleSyncWorkQueue.isEmpty() == expectQueueRemainsEmpty
140 where: 'the following lock states are applied'
141 canLock | expectQueueRemainsEmpty
146 def 'Sleeper gets interrupted.'() {
147 given: 'sleeper gets interrupted'
148 spiedSleeper.haveALittleRest(_) >> { throw new InterruptedException() }
149 when: 'the watchdog attempts to sleep to save cpu cycles'
150 objectUnderTest.preventBusyWait()
151 then: 'no exception is thrown'
155 def createCmHandleIds(numberOfCmHandles) {
156 return (numberOfCmHandles > 0) ? (1..numberOfCmHandles).collect { 'ch-'+it } : []