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.api.inventory.sync
24 import com.hazelcast.map.IMap
25 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncTasks
26 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncWatchdog
27 import org.onap.cps.ncmp.api.impl.inventory.sync.SyncUtils
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
29 import org.onap.cps.ncmp.api.impl.inventory.sync.executor.AsyncTaskExecutor
30 import java.util.concurrent.ArrayBlockingQueue
31 import org.onap.cps.spi.model.DataNode
32 import spock.lang.Specification
34 class ModuleSyncWatchdogSpec extends Specification {
36 def mockSyncUtils = Mock(SyncUtils)
38 def static testQueueCapacity = 50 + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE
40 def moduleSyncWorkQueue = new ArrayBlockingQueue(testQueueCapacity)
42 def mockModuleSyncStartedOnCmHandles = Mock(IMap<String, Object>)
44 def mockModuleSyncTasks = Mock(ModuleSyncTasks)
46 def spiedAsyncTaskExecutor = Spy(AsyncTaskExecutor)
48 def objectUnderTest = new ModuleSyncWatchdog(mockSyncUtils, moduleSyncWorkQueue , mockModuleSyncStartedOnCmHandles, mockModuleSyncTasks, spiedAsyncTaskExecutor)
51 spiedAsyncTaskExecutor.setupThreadPool()
54 def 'Module sync advised cm handles with #scenario.'() {
55 given: 'sync utilities returns #numberOfAdvisedCmHandles advised cm handles'
56 mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(numberOfAdvisedCmHandles)
57 and: 'the executor has enough available threads'
58 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 3
59 when: ' module sync is started'
60 objectUnderTest.moduleSyncAdvisedCmHandles()
61 then: 'it performs #expectedNumberOfTaskExecutions tasks'
62 expectedNumberOfTaskExecutions * spiedAsyncTaskExecutor.executeTask(*_)
63 where: 'the following parameter are used'
64 scenario | numberOfAdvisedCmHandles || expectedNumberOfTaskExecutions
65 'less then 1 batch' | 1 || 1
66 'exactly 1 batch' | ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 1
67 '2 batches' | 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 2
68 'queue capacity' | testQueueCapacity || 3
69 'over queue capacity' | testQueueCapacity + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 3
72 def 'Module sync advised cm handles starts with no available threads.'() {
73 given: 'sync utilities returns a advise cm handles'
74 mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(1)
75 and: 'the executor first has no threads but has one thread on the second attempt'
76 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >>> [ 0, 1 ]
77 when: ' module sync is started'
78 objectUnderTest.moduleSyncAdvisedCmHandles()
79 then: 'it performs one task'
80 1 * spiedAsyncTaskExecutor.executeTask(*_)
83 def 'Module sync advised cm handles already handled.'() {
84 given: 'sync utilities returns a advise cm handles'
85 mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(1)
86 and: 'the executor has a thread available'
87 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 1
88 and: 'the semaphore cache indicates the cm handle is already being processed'
89 mockModuleSyncStartedOnCmHandles.putIfAbsent(*_) >> 'Started'
90 when: ' module sync is started'
91 objectUnderTest.moduleSyncAdvisedCmHandles()
92 then: 'it does NOT execute a task to process the (empty) batch'
93 0 * spiedAsyncTaskExecutor.executeTask(*_)
96 def 'Module sync with previous cm handle(s) left in work queue.'() {
97 given: 'there is still a cm handle in the queue'
98 moduleSyncWorkQueue.offer(new DataNode())
99 and: 'sync utilities returns many advise cm handles'
100 mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(500)
101 and: 'the executor has plenty threads available'
102 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 10
103 when: ' module sync is started'
104 objectUnderTest.moduleSyncAdvisedCmHandles()
105 then: 'it does executes only one task to process the remaining handle in the queue'
106 1 * spiedAsyncTaskExecutor.executeTask(*_)
109 def 'Reset failed cm handles.'() {
110 given: 'sync utilities returns failed cm handles'
111 def failedCmHandles = [new YangModelCmHandle()]
112 mockSyncUtils.getModuleSyncFailedCmHandles() >> failedCmHandles
113 when: 'reset failed cm handles is started'
114 objectUnderTest.resetPreviouslyFailedCmHandles()
115 then: 'it is delegated to the module sync task (service)'
116 1 * mockModuleSyncTasks.resetFailedCmHandles(failedCmHandles)
119 def createDataNodes(numberOfDataNodes) {
121 (1..numberOfDataNodes).each {dataNodes.add(new DataNode())}