X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fcontrol%2Fut_stub_rtmgr_test.go;h=91a8fd878d84447523c7004c1308827053a84855;hb=HEAD;hp=bdca2931c27bf4a38df0b626ed65f49e08d0d561;hpb=fa0156680b0bd8f7300f49c65d2ee7bedaaa0e44;p=ric-plt%2Fsubmgr.git diff --git a/pkg/control/ut_stub_rtmgr_test.go b/pkg/control/ut_stub_rtmgr_test.go index bdca293..91a8fd8 100644 --- a/pkg/control/ut_stub_rtmgr_test.go +++ b/pkg/control/ut_stub_rtmgr_test.go @@ -21,36 +21,39 @@ package control import ( "encoding/json" - "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_models" - "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" "net/http" "sync" "testing" "time" + + "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_models" + "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub" ) //----------------------------------------------------------------------------- // //----------------------------------------------------------------------------- -type httpEventWaiter struct { +type HttpEventWaiter struct { + teststub.TestWrapper resultChan chan bool nextActionOk bool + sleep int } -func (msg *httpEventWaiter) SetResult(res bool) { +func (msg *HttpEventWaiter) SetResult(res bool) { msg.resultChan <- res } -func (msg *httpEventWaiter) WaitResult(t *testing.T) bool { +func (msg *HttpEventWaiter) WaitResult(t *testing.T) bool { select { case result := <-msg.resultChan: return result case <-time.After(15 * time.Second): - testError(t, "Waiter not received result status from case within 15 secs") + msg.TestError(t, "Waiter not received result status from case within 15 secs") return false } - testError(t, "Waiter error in default branch") + msg.TestError(t, "Waiter error in default branch") return false } @@ -59,51 +62,86 @@ func (msg *httpEventWaiter) WaitResult(t *testing.T) bool { //----------------------------------------------------------------------------- type testingHttpRtmgrStub struct { sync.Mutex - desc string + teststub.TestWrapper port string - eventWaiter *httpEventWaiter + eventWaiter *HttpEventWaiter +} + +func (tc *testingHttpRtmgrStub) NextEvent(eventWaiter *HttpEventWaiter) { + tc.Lock() + defer tc.Unlock() + tc.eventWaiter = eventWaiter } -func (hc *testingHttpRtmgrStub) NextEvent(eventWaiter *httpEventWaiter) { - hc.Lock() - defer hc.Unlock() - hc.eventWaiter = eventWaiter +func (tc *testingHttpRtmgrStub) AllocNextEvent(nextAction bool) *HttpEventWaiter { + eventWaiter := &HttpEventWaiter{ + resultChan: make(chan bool), + nextActionOk: nextAction, + } + eventWaiter.TestWrapper.Init("localhost:" + tc.port) + tc.NextEvent(eventWaiter) + return eventWaiter } -func (hc *testingHttpRtmgrStub) AllocNextEvent(nextAction bool) *httpEventWaiter { - eventWaiter := &httpEventWaiter{ +func (tc *testingHttpRtmgrStub) AllocNextSleep(sleep int, nextAction bool) *HttpEventWaiter { + eventWaiter := &HttpEventWaiter{ resultChan: make(chan bool), nextActionOk: nextAction, + sleep: sleep, } - hc.NextEvent(eventWaiter) + eventWaiter.TestWrapper.Init("localhost:" + tc.port) + tc.NextEvent(eventWaiter) return eventWaiter } -func (hc *testingHttpRtmgrStub) http_handler(w http.ResponseWriter, r *http.Request) { +func (tc *testingHttpRtmgrStub) http_handler(w http.ResponseWriter, r *http.Request) { - hc.Lock() - defer hc.Unlock() + tc.Lock() + defer tc.Unlock() + var id int32 = -1 - var req rtmgr_models.XappSubscriptionData - err := json.NewDecoder(r.Body).Decode(&req) - if err != nil { - xapp.Logger.Error("%s", err.Error()) + if r.Method == http.MethodPost || r.Method == http.MethodDelete { + var req rtmgr_models.XappSubscriptionData + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + tc.Error("%s", err.Error()) + } + tc.Debug("handling SubscriptionID=%d Address=%s Port=%d", *req.SubscriptionID, *req.Address, *req.Port) + id = *req.SubscriptionID + } + if r.Method == http.MethodPut { + var req rtmgr_models.XappList + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + tc.Error("%s", err.Error()) + } + tc.Debug("handling put") } - xapp.Logger.Info("(%s) handling Address=%s Port=%d SubscriptionID=%d", hc.desc, *req.Address, *req.Port, *req.SubscriptionID) var code int = 0 switch r.Method { case http.MethodPost: code = 201 - if hc.eventWaiter != nil { - if hc.eventWaiter.nextActionOk == false { + if tc.eventWaiter != nil { + if tc.eventWaiter.nextActionOk == false { code = 400 } + if tc.eventWaiter.sleep != 0 { + <-time.After(time.Duration(tc.eventWaiter.sleep) * time.Millisecond) + tc.Debug("sleeping done, %v", id) + } } case http.MethodDelete: code = 200 - if hc.eventWaiter != nil { - if hc.eventWaiter.nextActionOk == false { + if tc.eventWaiter != nil { + if tc.eventWaiter.nextActionOk == false { + code = 400 + } + } + case http.MethodPut: + code = 201 + if tc.eventWaiter != nil { + if tc.eventWaiter.nextActionOk == false { code = 400 } } @@ -111,24 +149,24 @@ func (hc *testingHttpRtmgrStub) http_handler(w http.ResponseWriter, r *http.Requ code = 200 } - waiter := hc.eventWaiter - hc.eventWaiter = nil + waiter := tc.eventWaiter + tc.eventWaiter = nil if waiter != nil { waiter.SetResult(true) } - xapp.Logger.Info("(%s) Method=%s Reply with code %d", hc.desc, r.Method, code) + tc.Debug("Method=%s Reply with code %d", r.Method, code) w.WriteHeader(code) } -func (hc *testingHttpRtmgrStub) run() { - http.HandleFunc("/", hc.http_handler) - http.ListenAndServe("localhost:"+hc.port, nil) +func (tc *testingHttpRtmgrStub) run() { + http.HandleFunc("/", tc.http_handler) + http.ListenAndServe("localhost:"+tc.port, nil) } func createNewHttpRtmgrStub(desc string, port string) *testingHttpRtmgrStub { - hc := &testingHttpRtmgrStub{} - hc.desc = desc - hc.port = port - return hc + tc := &testingHttpRtmgrStub{} + tc.port = port + tc.TestWrapper.Init(desc) + return tc }