Unittest stability improvements 46/6546/3
authorMarkku Virtanen <markku.virtanen@nokia.com>
Fri, 30 Jul 2021 12:04:00 +0000 (12:04 +0000)
committerAnssi Mannila <anssi.mannila@nokia.com>
Mon, 2 Aug 2021 09:07:01 +0000 (09:07 +0000)
Signed-off-by: Markku Virtanen <markku.virtanen@nokia.com>
Change-Id: Ice47361aa0d8838aae6d5c392b2b70ca534d0610

pkg/control/control.go
pkg/control/restendpoint.go
pkg/control/ut_messaging_test.go
pkg/teststube2ap/stubE2.go

index 4df7676..5ab86c0 100755 (executable)
@@ -544,7 +544,7 @@ func (c *Control) SubscriptionDeleteHandlerCB(restSubId string) error {
 
        xAppRmrEndPoint := restSubscription.xAppRmrEndPoint
        go func() {
-               xapp.Logger.Info("Deleteting instances = %v", restSubscription.InstanceIds)
+               xapp.Logger.Info("Deleteting handler: processing instances = %v", restSubscription.InstanceIds)
                for _, instanceId := range restSubscription.InstanceIds {
                        xAppEventInstanceID, err := c.SubscriptionDeleteHandler(&restSubId, &xAppRmrEndPoint, &restSubscription.Meid, instanceId)
 
@@ -1276,6 +1276,10 @@ func (c *Control) PrintRESTSubscriptionRequest(p *models.SubscriptionParams) {
 
        fmt.Println("CRESTSubscriptionRequest")
 
+       if p == nil {
+               return
+       }
+
        if p.SubscriptionID != "" {
                fmt.Println("  SubscriptionID = ", p.SubscriptionID)
        } else {
index 3b4f8f5..2e64f05 100644 (file)
@@ -40,7 +40,12 @@ func ConstructEndpointAddresses(clientEndpoint models.SubscriptionParamsClientEn
        var xAppHTTPEndPoint string
        var xAppRMREndPoint string
 
-       if host == "" || (HTTP_port == 0 && RMR_port == 0) {
+       if host == "" {
+               err := fmt.Errorf("ClientEndpoint provided without HOST name")
+               return "", "", err
+       }
+
+       if HTTP_port == 0 && RMR_port == 0 {
                err := fmt.Errorf("ClientEndpoint provided without PORT numbers")
                return "INVALID_HTTP_ADDRESS:" + host + (string)(*clientEndpoint.HTTPPort),
                        "INVALID_RMR_ADDRESS:" + host + (string)(*clientEndpoint.RMRPort),
index 63ffaaf..dff639e 100644 (file)
@@ -2898,12 +2898,15 @@ func TestRESTSubReqRetransmissionV4(t *testing.T) {
        params2.SetSubscriptionID(&restSubId)
 
        xapp.Subscription.SetResponseCB(xappConn1.SubscriptionRespHandler)
-       xappConn1.WaitRESTNotificationForAnySubscriptionId(t)
+       xappConn1.ExpectAnyNotification(t)
        // Resend the original request with an additional e2 subscription (detail), this time with restsubsid
        restSubId_resend := xappConn1.SendRESTSubsReq(t, params2)
+       e2SubsId1 := xappConn1.WaitAnyRESTNotification(t)
+       assert.Equal(t, e2SubsId, e2SubsId1)
 
        crereq2, cremsg2 := e2termConn1.RecvSubsReq(t)
 
+       xappConn1.DecrementRequestCount()
        xappConn1.ExpectRESTNotification(t, restSubId_resend)
        e2termConn1.SendSubsResp(t, crereq2, cremsg2)
        e2SubsId2 := xappConn1.WaitRESTNotification(t, restSubId_resend)
@@ -2914,11 +2917,14 @@ func TestRESTSubReqRetransmissionV4(t *testing.T) {
        xapp.Subscription.SetResponseCB(xappConn1.SubscriptionRespHandler)
        params = xappConn1.GetRESTSubsReqReportParams(subReqCount)
        params.SetSubscriptionID(&restSubId)
-       xappConn1.WaitRESTNotificationForAnySubscriptionId(t)
+       xappConn1.ExpectAnyNotification(t)
        // Resend the original request again with only one e2 subscription (detail), this time with restsubsid
        restSubId_resend2 := xappConn1.SendRESTSubsReq(t, params)
        assert.Equal(t, restSubId_resend, restSubId_resend2)
 
+       e2SubsId1 = xappConn1.WaitAnyRESTNotification(t)
+       assert.Equal(t, e2SubsId, e2SubsId1)
+
        // Delete both e2 subscriptions
        xappConn1.SendRESTSubsDelReq(t, &restSubId)
        e2SubsIds := []uint32{e2SubsId, e2SubsId2}
@@ -3013,10 +3019,15 @@ func TestRESTSubReqRetransmissionV5(t *testing.T) {
        params2.SetSubscriptionID(&restSubId)
 
        xapp.Subscription.SetResponseCB(xappConn1.SubscriptionRespHandler)
-       xappConn1.WaitRESTNotificationForAnySubscriptionId(t)
+       xappConn1.ExpectAnyNotification(t)
        // Resend the original request with an additional e2 subscription (detail), this time with restsubsid
        restSubId_resend := xappConn1.SendRESTSubsReq(t, params2)
 
+       e2SubsId1 := xappConn1.WaitAnyRESTNotification(t)
+       assert.Equal(t, e2SubsId, e2SubsId1)
+       // The first E2 subscription returns immediately, manually decrement expected request count for the remaining request handling
+       xappConn1.DecrementRequestCount()
+
        crereq2, cremsg2 := e2termConn1.RecvSubsReq(t)
 
        xappConn1.ExpectRESTNotification(t, restSubId_resend)
@@ -3028,12 +3039,15 @@ func TestRESTSubReqRetransmissionV5(t *testing.T) {
 
        xapp.Subscription.SetResponseCB(xappConn1.SubscriptionRespHandler)
        params = xappConn1.GetRESTSubsReqReportParams(subReqCount)
-       xappConn1.WaitRESTNotificationForAnySubscriptionId(t)
+       xappConn1.ExpectAnyNotification(t)
        // Resend the original request again with only one e2 subscription (detail), WITHOUT restsubsid
        // md5sum shall find the original request
        restSubId_resend2 := xappConn1.SendRESTSubsReq(t, params)
        assert.Equal(t, restSubId_resend, restSubId_resend2)
 
+       e2SubsId1 = xappConn1.WaitAnyRESTNotification(t)
+       assert.Equal(t, e2SubsId, e2SubsId1)
+
        // Delete both e2 subscriptions
        xappConn1.SendRESTSubsDelReq(t, &restSubId)
        e2SubsIds := []uint32{e2SubsId, e2SubsId2}
@@ -3137,10 +3151,13 @@ func TestRESTSubReqRetransmissionV6(t *testing.T) {
        params2.SetSubscriptionID(&restSubId)
 
        xapp.Subscription.SetResponseCB(xappConn1.SubscriptionRespHandler)
-       xappConn1.WaitRESTNotificationForAnySubscriptionId(t)
+       xappConn1.ExpectAnyNotification(t)
        // Resend the original request with an additional e2 subscription (detail), this time with restsubsid
        restSubId_resend := xappConn1.SendRESTSubsReq(t, params2)
 
+       e2SubsId1 := xappConn1.WaitAnyRESTNotification(t)
+       assert.Equal(t, e2SubsId, e2SubsId1)
+
        crereq2, cremsg2 := e2termConn1.RecvSubsReq(t)
 
        xappConn1.ExpectRESTNotification(t, restSubId_resend)
@@ -3938,12 +3955,12 @@ func TestRESTSubReqAndSubDelOkSameAction(t *testing.T) {
        params.SetMeid("RAN_NAME_1")
 
        xapp.Subscription.SetResponseCB(xappConn2.SubscriptionRespHandler)
-       xappConn2.WaitRESTNotificationForAnySubscriptionId(t)
+       xappConn2.ExpectAnyNotification(t)
        waiter := rtmgrHttp.AllocNextSleep(10, true)
        restSubId2 := xappConn2.SendRESTSubsReq(t, params)
        waiter.WaitResult(t)
        xapp.Logger.Info("Send REST subscriber request for subscriberId : %v", restSubId2)
-       e2SubsId2 := <-xappConn2.RESTNotification
+       e2SubsId2 := xappConn2.WaitAnyRESTNotification(t)
        xapp.Logger.Info("REST notification received e2SubsId=%v", e2SubsId2)
 
        queryXappSubscription(t, int64(e2SubsId1), "RAN_NAME_1", []string{"localhost:13560", "localhost:13660"})
@@ -4918,10 +4935,10 @@ func TestRESTSubReqAndSubDelOkSameActionWithRestartsInMiddle(t *testing.T) {
        params = xappConn2.GetRESTSubsReqReportParams(subReqCount)
        params.SetMeid("RAN_NAME_1")
        xapp.Subscription.SetResponseCB(xappConn2.SubscriptionRespHandler)
-       xappConn2.WaitRESTNotificationForAnySubscriptionId(t)
+       xappConn2.ExpectAnyNotification(t)
        restSubId2 := xappConn2.SendRESTSubsReq(t, params)
        xapp.Logger.Info("Send REST subscriber request for subscriberId : %v", restSubId2)
-       e2SubsId2 := <-xappConn2.RESTNotification
+       e2SubsId2 := xappConn2.WaitAnyRESTNotification(t)
        xapp.Logger.Info("REST notification received e2SubsId=%v", e2SubsId2)
 
        queryXappSubscription(t, int64(e2SubsId1), "RAN_NAME_1", []string{"localhost:13560", "localhost:13660"})
index f20afed..72baf27 100644 (file)
@@ -21,6 +21,7 @@ package teststube2ap
 
 import (
        "fmt"
+       "runtime/debug"
        "strconv"
        "testing"
        "time"
@@ -709,7 +710,7 @@ func (tc *E2Stub) expectNotification(t *testing.T, restSubsId string, expectErro
                        if tc.requestCount == 0 {
                                tc.TestError(t, "### REST notification count unexpectedly ZERO for %s (%v)", restSubsId, tc)
                        } else if e2Ids.RestSubsId != restSubsId {
-                               tc.TestError(t, "### Unexpected REST notifications received |%s:%s| (%v)", e2Ids.RestSubsId, restSubsId, tc)
+                               tc.TestError(t, "### Unexpected REST notifications received, expected %s bunt got %s instead| (%v)", e2Ids.RestSubsId, restSubsId, tc)
                        } else if e2Ids.ErrorCause == "" && expectError == "allFail" {
                                tc.TestError(t, "### Unexpected ok cause received from REST notifications |%s:%s| (%v)", e2Ids.RestSubsId, restSubsId, tc)
                        } else if e2Ids.ErrorCause != "" && expectError == "allOk" {
@@ -726,6 +727,9 @@ func (tc *E2Stub) expectNotification(t *testing.T, restSubsId string, expectErro
                                }
                                tc.Info("### REST Notification received Notif for %s : %v", e2Ids.RestSubsId, e2Ids.E2SubsId)
                                tc.ListedRESTNotifications <- e2Ids
+                               if len(tc.ListedRESTNotifications) > 1 {
+                                       panic("expectNotification - ListedRESTNotifications stacking up")
+                               }
                        }
                case <-time.After(15 * time.Second):
                        err := fmt.Errorf("### Timeout 15s expired while expecting REST notification for subsId: %v", restSubsId)
@@ -735,38 +739,57 @@ func (tc *E2Stub) expectNotification(t *testing.T, restSubsId string, expectErro
 }
 
 func (tc *E2Stub) WaitRESTNotification(t *testing.T, restSubsId string) uint32 {
+
+       stack := string(debug.Stack())
+
        select {
        case e2SubsId := <-tc.ListedRESTNotifications:
                if e2SubsId.RestSubsId == restSubsId {
                        tc.Info("### Expected REST notifications received %s, e2SubsId %v for endpoint=%s, (%v)", e2SubsId.RestSubsId, e2SubsId.E2SubsId, tc.clientEndpoint, tc)
                        return e2SubsId.E2SubsId
                } else {
-                       tc.TestError(t, "### Unexpected REST notifications received %s, expected %s for endpoint=%s, (%v)", e2SubsId.RestSubsId, restSubsId, tc.clientEndpoint, tc)
+                       tc.TestError(t, "### Unexpected REST notification %s received, expected %s for endpoint=%s, (%v)", e2SubsId.RestSubsId, restSubsId, tc.clientEndpoint, tc)
+                       xapp.Logger.Info("CALL STACK:\n %s", stack)
                        return 0
                }
        case <-time.After(15 * time.Second):
                err := fmt.Errorf("### Timeout 15s expired while waiting REST notification for subsId: %v", restSubsId)
                tc.TestError(t, "%s", err.Error())
+               xapp.Logger.Info("CALL STACK:\n %s", stack)
                panic("WaitRESTNotification - timeout error")
        }
-       return 0
 }
 
-func (tc *E2Stub) WaitRESTNotificationForAnySubscriptionId(t *testing.T) {
+// Note, this function should be followed by a handling of <-xappConn1.RESTNotification.
+func (tc *E2Stub) ExpectAnyNotification(t *testing.T) {
        go func() {
-               tc.Info("### REST notifications received for endpoint=%s, (%v)", tc.clientEndpoint, tc)
+               tc.Info("### Started waiting ANY REST notifications received for endpoint=%s, (%v)", tc.clientEndpoint, tc)
                select {
                case e2SubsId := <-tc.CallBackNotification:
-                       tc.Info("### REST notifications received e2SubsId %v for endpoint=%s, (%v)", e2SubsId, tc.clientEndpoint, tc)
+                       tc.Info("### ANY REST notifications received e2SubsId %v for endpoint=%s, (%v) via CallBackNotification", e2SubsId, tc.clientEndpoint, tc)
                        tc.RESTNotification <- (uint32)(e2SubsId)
                case <-time.After(15 * time.Second):
-                       err := fmt.Errorf("### Timeout 15s expired while waiting REST notification")
+                       err := fmt.Errorf("### Timeout 15s expired while waiting ANY REST notification")
                        tc.TestError(t, "%s", err.Error())
                        tc.RESTNotification <- 0
                }
        }()
 }
 
+func (tc *E2Stub) WaitAnyRESTNotification(t *testing.T) uint32 {
+
+       select {
+       case e2SubsId := <-tc.RESTNotification:
+               tc.Info("### Expected ANY REST notification received - e2SubsId %v for endpoint=%s, (%v)", e2SubsId, tc.clientEndpoint, tc)
+               return e2SubsId
+
+       case <-time.After(15 * time.Second):
+               err := fmt.Errorf("### Timeout 15s expired while waiting ANY REST notification")
+               tc.TestError(t, "%s", err.Error())
+               panic("WaitRESTNotification - timeout error")
+       }
+}
+
 func (tc *E2Stub) ListedRestNotifHandler(resp *clientmodel.SubscriptionResponse) {
 
        if len(tc.restSubsIdList) == 0 {
@@ -787,7 +810,7 @@ func (tc *E2Stub) ListedRestNotifHandler(resp *clientmodel.SubscriptionResponse)
                                }
 
                                if len(tc.restSubsIdList) == 0 {
-                                       //tc.Info("All listed REST notifications received for endpoint=%s", tc.clientEndpoint)
+                                       tc.Info("All listed REST notifications received for endpoint=%s", tc.clientEndpoint)
                                }
 
                                return
@@ -1039,6 +1062,8 @@ func (p *RESTSubsReqParams) SetSubscriptionID(SubscriptionID *string) {
 //-----------------------------------------------------------------------------
 func (tc *E2Stub) SendRESTSubsDelReq(t *testing.T, subscriptionID *string) {
 
+       tc.Info("======== Posting REST DELETE subscription(s) to Submgr ======")
+
        if *subscriptionID == "" {
                tc.Error("REST error in deleting subscriptions. Empty SubscriptionID = %s", *subscriptionID)
        }
@@ -1062,6 +1087,17 @@ func (tc *E2Stub) GetRESTSubsReqPolicyParams(subReqCount int) *RESTSubsReqParams
        return &policyParams
 }
 
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+func (tc *E2Stub) DecrementRequestCount() {
+       if tc.requestCount > 0 {
+               tc.requestCount -= 1
+       } else {
+               tc.Error("FAILED to decrement request count, count already ZERO")
+       }
+}
+
 //-----------------------------------------------------------------------------
 //
 //-----------------------------------------------------------------------------