3064a78ff9644964cfa8a2923d333fbde627be50
[cps.git] /
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.impl.inventory.sync
23
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
28
29 import java.util.concurrent.ArrayBlockingQueue
30 import java.util.concurrent.locks.Lock
31
32 class ModuleSyncWatchdogSpec extends Specification {
33
34     def mockSyncUtils = Mock(ModuleOperationsUtils)
35
36     def static testQueueCapacity = 50 + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE
37
38     def moduleSyncWorkQueue = new ArrayBlockingQueue(testQueueCapacity)
39
40     def mockModuleSyncStartedOnCmHandles = Mock(IMap<String, Object>)
41
42     def mockModuleSyncTasks = Mock(ModuleSyncTasks)
43
44     def spiedAsyncTaskExecutor = Spy(AsyncTaskExecutor)
45
46     def mockWorkQueueLock = Mock(Lock)
47
48     def objectUnderTest = new ModuleSyncWatchdog(mockSyncUtils, moduleSyncWorkQueue , mockModuleSyncStartedOnCmHandles, mockModuleSyncTasks, spiedAsyncTaskExecutor, mockWorkQueueLock)
49
50     void setup() {
51         spiedAsyncTaskExecutor.setupThreadPool()
52         mockWorkQueueLock.tryLock() >> true
53     }
54
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
71     }
72
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(*_)
82     }
83
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(*_)
95     }
96
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(*_)
108     }
109
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)
118     }
119
120     def createDataNodes(numberOfDataNodes) {
121         def dataNodes = []
122         (1..numberOfDataNodes).each {dataNodes.add(new DataNode())}
123         return dataNodes
124     }
125 }