16da422f8fec8b777a8738b263548fe8541cf4f7
[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/xapp-frame/pkg/xapp"
24         "testing"
25         "time"
26 )
27
28 //-----------------------------------------------------------------------------
29 //
30 //-----------------------------------------------------------------------------
31 type testingSubmgrControl struct {
32         testingRmrControl
33         c *Control
34 }
35
36 func createSubmgrControl(desc string, rtfile string, port string) *testingSubmgrControl {
37         mainCtrl = &testingSubmgrControl{}
38         mainCtrl.testingRmrControl.init(desc, rtfile, port)
39         mainCtrl.c = NewControl()
40         xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
41         go xapp.RunWithParams(mainCtrl.c, false)
42         mainCtrl.WaitCB()
43         return mainCtrl
44 }
45
46 func (mc *testingSubmgrControl) wait_registry_empty(t *testing.T, secs int) bool {
47         cnt := int(0)
48         i := 1
49         for ; i <= secs*2; i++ {
50                 cnt = len(mc.c.registry.register)
51                 if cnt == 0 {
52                         return true
53                 }
54                 time.Sleep(500 * time.Millisecond)
55         }
56         testError(t, "(general) no registry empty within %d secs: %d", secs, cnt)
57         return false
58 }
59
60 func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
61         var subs *Subscription
62         i := 1
63         for ; i <= secs*2; i++ {
64                 subs = mc.c.registry.GetSubscription(e2SubsId)
65                 if subs == nil {
66                         return true
67                 }
68                 time.Sleep(500 * time.Millisecond)
69         }
70         if subs != nil {
71                 testError(t, "(general) no clean within %d secs: %s", secs, subs.String())
72         } else {
73                 testError(t, "(general) no clean within %d secs: subs(N/A)", secs)
74         }
75         return false
76 }
77
78 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
79         var trans TransactionIf
80         i := 1
81         for ; i <= secs*2; i++ {
82                 subs := mc.c.registry.GetSubscription(e2SubsId)
83                 if subs == nil {
84                         return true
85                 }
86                 trans = subs.GetTransaction()
87                 if trans == nil {
88                         return true
89                 }
90                 time.Sleep(500 * time.Millisecond)
91         }
92         if trans != nil {
93                 testError(t, "(general) no clean within %d secs: %s", secs, trans.String())
94         } else {
95                 testError(t, "(general) no clean within %d secs: trans(N/A)", secs)
96         }
97         return false
98 }
99
100 func (mc *testingSubmgrControl) get_subid(t *testing.T) uint32 {
101         mc.c.registry.mutex.Lock()
102         defer mc.c.registry.mutex.Unlock()
103         return mc.c.registry.subIds[0]
104 }
105
106 func (mc *testingSubmgrControl) wait_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
107         i := 1
108         for ; i <= secs*2; i++ {
109                 mc.c.registry.mutex.Lock()
110                 currSubId := mc.c.registry.subIds[0]
111                 mc.c.registry.mutex.Unlock()
112                 if currSubId != origSubId {
113                         return currSubId, true
114                 }
115                 time.Sleep(500 * time.Millisecond)
116         }
117         testError(t, "(general) no subId change within %d secs", secs)
118         return 0, false
119 }
120
121 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
122         return mc.c.msgCounter
123 }
124
125 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
126         i := 1
127         for ; i <= secs*2; i++ {
128                 curr := mc.c.msgCounter
129                 if curr != orig {
130                         return curr, true
131                 }
132                 time.Sleep(500 * time.Millisecond)
133         }
134         testError(t, "(general) no msg counter change within %d secs", secs)
135         return 0, false
136 }