85cbe8f99a1ff903230b1e5cceedb3c3fd712672
[ric-plt/submgr.git] / pkg / control / ut_ctrl_submgr_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
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 */
19
20 package control
21
22 import (
23         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
24         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
25         "testing"
26         "time"
27 )
28
29 //-----------------------------------------------------------------------------
30 //
31 //-----------------------------------------------------------------------------
32 type testingSubmgrControl struct {
33         teststub.RmrControl
34         c *Control
35 }
36
37 func createSubmgrControl(srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc) *testingSubmgrControl {
38         mainCtrl = &testingSubmgrControl{}
39         mainCtrl.RmrControl.Init("SUBMGRCTL", srcId, rtgSvc)
40         mainCtrl.c = NewControl()
41         xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
42         go xapp.RunWithParams(mainCtrl.c, false)
43         mainCtrl.WaitCB()
44         mainCtrl.c.ReadyCB(nil)
45         return mainCtrl
46 }
47
48 func (mc *testingSubmgrControl) wait_registry_empty(t *testing.T, secs int) bool {
49         cnt := int(0)
50         i := 1
51         for ; i <= secs*2; i++ {
52                 cnt = len(mc.c.registry.register)
53                 if cnt == 0 {
54                         return true
55                 }
56                 time.Sleep(500 * time.Millisecond)
57         }
58         mc.TestError(t, "(submgr) no registry empty within %d secs: %d", secs, cnt)
59         return false
60 }
61
62 func (mc *testingSubmgrControl) get_registry_next_subid(t *testing.T) uint32 {
63         mc.c.registry.mutex.Lock()
64         defer mc.c.registry.mutex.Unlock()
65         return mc.c.registry.subIds[0]
66 }
67
68 func (mc *testingSubmgrControl) wait_registry_next_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
69         i := 1
70         for ; i <= secs*2; i++ {
71                 mc.c.registry.mutex.Lock()
72                 currSubId := mc.c.registry.subIds[0]
73                 mc.c.registry.mutex.Unlock()
74                 if currSubId != origSubId {
75                         return currSubId, true
76                 }
77                 time.Sleep(500 * time.Millisecond)
78         }
79         mc.TestError(t, "(submgr) no subId change within %d secs", secs)
80         return 0, false
81 }
82
83 func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
84         var subs *Subscription
85         i := 1
86         for ; i <= secs*2; i++ {
87                 subs = mc.c.registry.GetSubscription(e2SubsId)
88                 if subs == nil {
89                         return true
90                 }
91                 time.Sleep(500 * time.Millisecond)
92         }
93         if subs != nil {
94                 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, subs.String())
95         } else {
96                 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A)", secs)
97         }
98         return false
99 }
100
101 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
102         var trans TransactionIf
103         i := 1
104         for ; i <= secs*2; i++ {
105                 subs := mc.c.registry.GetSubscription(e2SubsId)
106                 if subs == nil {
107                         return true
108                 }
109                 trans = subs.GetTransaction()
110                 if trans == nil {
111                         return true
112                 }
113                 time.Sleep(500 * time.Millisecond)
114         }
115         if trans != nil {
116                 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, trans.String())
117         } else {
118                 mc.TestError(t, "(submgr) no clean within %d secs: trans(N/A)", secs)
119         }
120         return false
121 }
122
123 func (mc *testingSubmgrControl) get_subs_entrypoint_cnt(t *testing.T, origSubId uint32) int {
124         subs := mc.c.registry.GetSubscription(origSubId)
125         if subs == nil {
126                 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt get", origSubId)
127                 return -1
128         }
129         return subs.EpList.Size()
130 }
131
132 func (mc *testingSubmgrControl) wait_subs_entrypoint_cnt_change(t *testing.T, origSubId uint32, orig int, secs int) (int, bool) {
133
134         subs := mc.c.registry.GetSubscription(origSubId)
135         if subs == nil {
136                 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt wait", origSubId)
137                 return -1, true
138         }
139
140         i := 1
141         for ; i <= secs*2; i++ {
142                 curr := subs.EpList.Size()
143                 if curr != orig {
144                         return curr, true
145                 }
146                 time.Sleep(500 * time.Millisecond)
147         }
148         mc.TestError(t, "(submgr) no subs %d entrypoint cnt change within %d secs", origSubId, secs)
149         return 0, false
150 }
151
152 //
153 // Counter check for received message. Note might not be yet handled
154 //
155 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
156         return mc.c.CntRecvMsg
157 }
158
159 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
160         i := 1
161         for ; i <= secs*2; i++ {
162                 curr := mc.c.CntRecvMsg
163                 if curr != orig {
164                         return curr, true
165                 }
166                 time.Sleep(500 * time.Millisecond)
167         }
168         mc.TestError(t, "(submgr) no msg counter change within %d secs", secs)
169         return 0, false
170 }