Generalized unittest stubs so frame can be used also in other apps
[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(rtfile string, port string) *testingSubmgrControl {
38         mainCtrl = &testingSubmgrControl{}
39         mainCtrl.RmrControl.Init("SUBMGRCTL", rtfile, port)
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) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
63         var subs *Subscription
64         i := 1
65         for ; i <= secs*2; i++ {
66                 subs = mc.c.registry.GetSubscription(e2SubsId)
67                 if subs == nil {
68                         return true
69                 }
70                 time.Sleep(500 * time.Millisecond)
71         }
72         if subs != nil {
73                 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, subs.String())
74         } else {
75                 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A)", secs)
76         }
77         return false
78 }
79
80 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
81         var trans TransactionIf
82         i := 1
83         for ; i <= secs*2; i++ {
84                 subs := mc.c.registry.GetSubscription(e2SubsId)
85                 if subs == nil {
86                         return true
87                 }
88                 trans = subs.GetTransaction()
89                 if trans == nil {
90                         return true
91                 }
92                 time.Sleep(500 * time.Millisecond)
93         }
94         if trans != nil {
95                 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, trans.String())
96         } else {
97                 mc.TestError(t, "(submgr) no clean within %d secs: trans(N/A)", secs)
98         }
99         return false
100 }
101
102 func (mc *testingSubmgrControl) get_subid(t *testing.T) uint32 {
103         mc.c.registry.mutex.Lock()
104         defer mc.c.registry.mutex.Unlock()
105         return mc.c.registry.subIds[0]
106 }
107
108 func (mc *testingSubmgrControl) wait_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
109         i := 1
110         for ; i <= secs*2; i++ {
111                 mc.c.registry.mutex.Lock()
112                 currSubId := mc.c.registry.subIds[0]
113                 mc.c.registry.mutex.Unlock()
114                 if currSubId != origSubId {
115                         return currSubId, true
116                 }
117                 time.Sleep(500 * time.Millisecond)
118         }
119         mc.TestError(t, "(submgr) no subId change within %d secs", secs)
120         return 0, false
121 }
122
123 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
124         return mc.c.CntRecvMsg
125 }
126
127 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
128         i := 1
129         for ; i <= secs*2; i++ {
130                 curr := mc.c.CntRecvMsg
131                 if curr != orig {
132                         return curr, true
133                 }
134                 time.Sleep(500 * time.Millisecond)
135         }
136         mc.TestError(t, "(submgr) no msg counter change within %d secs", secs)
137         return 0, false
138 }