Unittest stability improvements

Signed-off-by: Markku Virtanen <markku.virtanen@nokia.com>
Change-Id: Ice47361aa0d8838aae6d5c392b2b70ca534d0610
diff --git a/pkg/control/control.go b/pkg/control/control.go
index 4df7676..5ab86c0 100755
--- a/pkg/control/control.go
+++ b/pkg/control/control.go
@@ -544,7 +544,7 @@
 
 	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 @@
 
 	fmt.Println("CRESTSubscriptionRequest")
 
+	if p == nil {
+		return
+	}
+
 	if p.SubscriptionID != "" {
 		fmt.Println("  SubscriptionID = ", p.SubscriptionID)
 	} else {
diff --git a/pkg/control/restendpoint.go b/pkg/control/restendpoint.go
index 3b4f8f5..2e64f05 100644
--- a/pkg/control/restendpoint.go
+++ b/pkg/control/restendpoint.go
@@ -40,7 +40,12 @@
 	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),
diff --git a/pkg/control/ut_messaging_test.go b/pkg/control/ut_messaging_test.go
index 63ffaaf..dff639e 100644
--- a/pkg/control/ut_messaging_test.go
+++ b/pkg/control/ut_messaging_test.go
@@ -2898,12 +2898,15 @@
 	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 @@
 	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 @@
 	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 @@
 
 	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 @@
 	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 @@
 	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 @@
 	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"})