Performance Improvement: Use hazelcast blocking queue
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / sync / ModuleSyncWatchdogSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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.api.inventory.sync
23
24 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
25
26 import java.util.concurrent.ArrayBlockingQueue
27 import java.util.concurrent.BlockingQueue
28 import org.onap.cps.spi.model.DataNode
29 import spock.lang.Specification
30
31 class ModuleSyncWatchdogSpec extends Specification {
32
33     def mockSyncUtils = Mock(SyncUtils)
34
35     def static testQueueCapacity = 50 + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE
36
37     BlockingQueue<DataNode> moduleSyncWorkQueue = new ArrayBlockingQueue(testQueueCapacity)
38
39     def moduleSyncStartedOnCmHandles = [:]
40
41     def mockModuleSyncTasks = Mock(ModuleSyncTasks)
42
43     def objectUnderTest = new ModuleSyncWatchdog(mockSyncUtils, moduleSyncWorkQueue , moduleSyncStartedOnCmHandles, mockModuleSyncTasks)
44
45     def 'Module sync #scenario , #numberOfAdvisedCmHandles advised cm handles.'() {
46         given: 'sync utilities returns #numberOfAdvisedCmHandles advised cm handles'
47             mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(numberOfAdvisedCmHandles)
48         when: ' module sync is started'
49             objectUnderTest.moduleSyncAdvisedCmHandles()
50         then: 'it performs #expectedNumberOfTaskExecutions tasks'
51             expectedNumberOfTaskExecutions * mockModuleSyncTasks.performModuleSync(_)
52         where:
53             scenario              |  numberOfAdvisedCmHandles                                         || expectedNumberOfTaskExecutions
54             'less then 1 batch'   | 1                                                                 || 1
55             'exactly 1 batch'     | ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE                         || 1
56             '2 batches'           | 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE                     || 2
57             'queue capacity'      | testQueueCapacity                                                 || 3
58             'over queue capacity' | testQueueCapacity + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 3
59     }
60
61     def 'Reset failed cm handles.'() {
62         given: 'sync utilities returns failed cm handles'
63             def failedCmHandles = [new YangModelCmHandle()]
64             mockSyncUtils.getModuleSyncFailedCmHandles() >> failedCmHandles
65         when: ' reset failed cm handles is started'
66             objectUnderTest.resetPreviouslyFailedCmHandles()
67         then: 'it is delegated to the module sync task (service)'
68             1 * mockModuleSyncTasks.resetFailedCmHandles(failedCmHandles)
69     }
70
71     def createDataNodes(numberOfDataNodes) {
72         def dataNodes = []
73         (1..numberOfDataNodes).each {dataNodes.add(new DataNode())}
74         return dataNodes
75     }
76 }