2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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.
17 ==================================================================================
24 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
25 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
26 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
34 type testingSubmgrControl struct {
39 func createSubmgrControl(srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc) *testingSubmgrControl {
40 mainCtrl = &testingSubmgrControl{}
41 mainCtrl.RmrControl.Init("SUBMGRCTL", srcId, rtgSvc)
42 mainCtrl.c = NewControl()
43 xapp.Logger.Debug("Replacing real db with test db")
44 mainCtrl.c.db = CreateMock() // This overrides real database for testing
45 xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
46 go xapp.RunWithParams(mainCtrl.c, false)
48 mainCtrl.c.ReadyCB(nil)
52 func (mc *testingSubmgrControl) SimulateRestart(t *testing.T) {
53 mc.TestLog(t, "Simulating submgr restart")
54 mainCtrl.c.registry.subIds = nil
55 // Initialize subIds slice and subscription map
56 mainCtrl.c.registry.Initialize()
57 // Read subIds and subscriptions from database
58 subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
60 mc.TestError(t, "%v", err)
62 mainCtrl.c.registry.register = nil
63 mainCtrl.c.registry.subIds = subIds
64 mainCtrl.c.registry.register = register
66 fmt.Println("register:")
67 for subId, subs := range register {
68 fmt.Println(" subId", subId)
69 fmt.Println(" subs.SubRespRcvd", subs.SubRespRcvd)
70 fmt.Printf(" subs %v\n", subs)
73 fmt.Println("mainCtrl.c.registry.register:")
74 for subId, subs := range mainCtrl.c.registry.register {
75 fmt.Println(" subId", subId)
76 fmt.Println(" subs.SubRespRcvd", subs.SubRespRcvd)
77 fmt.Printf(" subs %v\n", subs)
80 go mainCtrl.c.HandleUncompletedSubscriptions(mainCtrl.c.registry.register)
83 func (mc *testingSubmgrControl) SetResetTestFlag(t *testing.T, status bool) {
84 mc.TestLog(t, "ResetTestFlag set to %v", status)
85 mainCtrl.c.ResetTestFlag = status
88 func (mc *testingSubmgrControl) removeExistingSubscriptions(t *testing.T) {
90 mc.TestLog(t, "Removing existing subscriptions")
91 mainCtrl.c.RemoveAllSubscriptionsFromSdl()
92 mainCtrl.c.registry.subIds = nil
93 // Initialize subIds slice and subscription map
94 mainCtrl.c.registry.Initialize()
97 func PringSubscriptionQueryResult(resp models.SubscriptionList) {
98 for _, item := range resp {
99 fmt.Printf("item.SubscriptionID %v\n", item.SubscriptionID)
100 fmt.Printf("item.Meid %v\n", item.Meid)
101 fmt.Printf("item.Endpoint %v\n", item.Endpoint)
105 func (mc *testingSubmgrControl) wait_registry_empty(t *testing.T, secs int) bool {
108 for ; i <= secs*2; i++ {
109 cnt = len(mc.c.registry.register)
113 time.Sleep(500 * time.Millisecond)
115 mc.TestError(t, "(submgr) no registry empty within %d secs: %d", secs, cnt)
119 func (mc *testingSubmgrControl) get_registry_next_subid(t *testing.T) uint32 {
120 mc.c.registry.mutex.Lock()
121 defer mc.c.registry.mutex.Unlock()
122 return mc.c.registry.subIds[0]
125 func (mc *testingSubmgrControl) wait_registry_next_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
127 for ; i <= secs*2; i++ {
128 mc.c.registry.mutex.Lock()
129 currSubId := mc.c.registry.subIds[0]
130 mc.c.registry.mutex.Unlock()
131 if currSubId != origSubId {
132 return currSubId, true
134 time.Sleep(500 * time.Millisecond)
136 mc.TestError(t, "(submgr) no subId change within %d secs", secs)
140 func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
141 var subs *Subscription
143 for ; i <= secs*2; i++ {
144 subs = mc.c.registry.GetSubscription(e2SubsId)
148 time.Sleep(500 * time.Millisecond)
151 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, subs.String())
153 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A)", secs)
158 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
159 var trans TransactionIf
161 for ; i <= secs*2; i++ {
162 subs := mc.c.registry.GetSubscription(e2SubsId)
166 trans = subs.GetTransaction()
170 time.Sleep(500 * time.Millisecond)
173 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, trans.String())
175 mc.TestError(t, "(submgr) no clean within %d secs: trans(N/A)", secs)
180 func (mc *testingSubmgrControl) get_subs_entrypoint_cnt(t *testing.T, origSubId uint32) int {
181 subs := mc.c.registry.GetSubscription(origSubId)
183 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt get", origSubId)
186 return subs.EpList.Size()
189 func (mc *testingSubmgrControl) wait_subs_entrypoint_cnt_change(t *testing.T, origSubId uint32, orig int, secs int) (int, bool) {
191 subs := mc.c.registry.GetSubscription(origSubId)
193 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt wait", origSubId)
198 for ; i <= secs*2; i++ {
199 curr := subs.EpList.Size()
203 time.Sleep(500 * time.Millisecond)
205 mc.TestError(t, "(submgr) no subs %d entrypoint cnt change within %d secs", origSubId, secs)
210 // Counter check for received message. Note might not be yet handled
212 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
213 return mc.c.CntRecvMsg
216 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
218 for ; i <= secs*2; i++ {
219 curr := mc.c.CntRecvMsg
223 time.Sleep(500 * time.Millisecond)
225 mc.TestError(t, "(submgr) no msg counter change within %d secs", secs)