Restructured test files. stubs locates in own files etc.
[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_subs_clean(t *testing.T, e2SubsId int, secs int) bool {
47         var subs *Subscription
48         i := 1
49         for ; i <= secs*2; i++ {
50                 subs = mc.c.registry.GetSubscription(uint16(e2SubsId))
51                 if subs == nil {
52                         return true
53                 }
54                 time.Sleep(500 * time.Millisecond)
55         }
56         if subs != nil {
57                 testError(t, "(general) no clean within %d secs: %s", secs, subs.String())
58         } else {
59                 testError(t, "(general) no clean within %d secs: subs(N/A)", secs)
60         }
61         return false
62 }
63
64 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId int, secs int) bool {
65         var trans *Transaction
66         i := 1
67         for ; i <= secs*2; i++ {
68                 subs := mc.c.registry.GetSubscription(uint16(e2SubsId))
69                 if subs == nil {
70                         return true
71                 }
72                 trans = subs.GetTransaction()
73                 if trans == nil {
74                         return true
75                 }
76                 time.Sleep(500 * time.Millisecond)
77         }
78         if trans != nil {
79                 testError(t, "(general) no clean within %d secs: %s", secs, trans.String())
80         } else {
81                 testError(t, "(general) no clean within %d secs: trans(N/A)", secs)
82         }
83         return false
84 }
85
86 func (mc *testingSubmgrControl) get_subid(t *testing.T) uint16 {
87         mc.c.registry.mutex.Lock()
88         defer mc.c.registry.mutex.Unlock()
89         return mc.c.registry.subIds[0]
90 }
91
92 func (mc *testingSubmgrControl) wait_subid_change(t *testing.T, origSubId uint16, secs int) (uint16, bool) {
93         i := 1
94         for ; i <= secs*2; i++ {
95                 mc.c.registry.mutex.Lock()
96                 currSubId := mc.c.registry.subIds[0]
97                 mc.c.registry.mutex.Unlock()
98                 if currSubId != origSubId {
99                         return currSubId, true
100                 }
101                 time.Sleep(500 * time.Millisecond)
102         }
103         testError(t, "(general) no subId change within %d secs", secs)
104         return 0, false
105 }
106
107 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
108         return mc.c.msgCounter
109 }
110
111 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
112         i := 1
113         for ; i <= secs*2; i++ {
114                 curr := mc.c.msgCounter
115                 if curr != orig {
116                         return curr, true
117                 }
118                 time.Sleep(500 * time.Millisecond)
119         }
120         testError(t, "(general) no msg counter change within %d secs", secs)
121         return 0, false
122 }