Unit test improvement
[ric-plt/submgr.git] / pkg / control / ut_messaging_test.go
index 5cc7458..2a3ad96 100644 (file)
@@ -39,6 +39,7 @@ func TestSuiteSetup(t *testing.T) {
        // like this:git log
        // SetPackerIf(e2ap_wrapper.NewAsn1E2APPacker())
 
+       mainCtrl.InitAllCounterMap()
        SetPackerIf(e2ap_wrapper.NewUtAsn1E2APPacker())
        mainCtrl.c.restDuplicateCtrl.Init()
 
@@ -95,6 +96,8 @@ func TestRESTSubReqAfterE2ConnBreak(t *testing.T) {
        mainCtrl.SetE2State(t, "RAN_NAME_1_CONNECTED")
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
+       //os.Exit(0)
 }
 
 //-----------------------------------------------------------------------------
@@ -173,6 +176,7 @@ func TestRESTSubReqE2ConnBreak(t *testing.T) {
        // Wait that subs is cleaned
        waitSubsCleanup(t, e2SubsId, 10)
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -249,6 +253,7 @@ func TestRESTSubscriptionDeleteAfterE2ConnectionBreak(t *testing.T) {
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -312,6 +317,7 @@ func TestRESTOtherE2ConnectionChanges(t *testing.T) {
        // Wait that subs is cleaned
        waitSubsCleanup(t, e2SubsId, 10)
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -361,6 +367,7 @@ func TestRESTSubReqAndDeleteOkWithE2apUtWrapper(t *testing.T) {
        deleteSubscription(t, xappConn1, e2termConn1, &restSubId)
 
        waitSubsCleanup(t, e2SubsId, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -407,6 +414,7 @@ func TestRESTSubReqAndE1apDeleteReqPackingError(t *testing.T) {
        defer e2ap_wrapper.AllowE2apToProcess(e2ap_wrapper.SUB_DEL_REQ, true)
 
        waitSubsCleanup(t, e2SubsId, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -469,6 +477,7 @@ func TestRESTSubReqAndE1apDeleteRespUnpackingError(t *testing.T) {
        defer e2ap_wrapper.AllowE2apToProcess(e2ap_wrapper.SUB_DEL_RESP, true)
 
        waitSubsCleanup(t, e2SubsId, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -726,6 +735,7 @@ func TestSubMergeDelAndRouteUpdateNok(t *testing.T) {
        // Init counter check
        mainCtrl.CounterValuesToBeVeriefied(t, CountersToBeAdded{
                Counter{cSubReqFromXapp, 2},
+               Counter{cMergedSubscriptions, 1},
                Counter{cSubReqToE2, 1},
                Counter{cSubRespFromE2, 1},
                Counter{cSubRespToXapp, 2},
@@ -734,6 +744,7 @@ func TestSubMergeDelAndRouteUpdateNok(t *testing.T) {
                Counter{cSubDelReqToE2, 1},
                Counter{cSubDelRespFromE2, 1},
                Counter{cSubDelRespToXapp, 2},
+               Counter{cUnmergedSubscriptions, 1},
        })
 
        //Req1
@@ -1260,6 +1271,7 @@ func TestSubReqRetryInSubmgr(t *testing.T) {
        mainCtrl.CounterValuesToBeVeriefied(t, CountersToBeAdded{
                Counter{cSubReqFromXapp, 1},
                Counter{cSubReqToE2, 1},
+               Counter{cSubReqTimerExpiry, 1},
                Counter{cSubReReqToE2, 1},
                Counter{cSubRespFromE2, 1},
                Counter{cSubRespToXapp, 1},
@@ -2363,9 +2375,6 @@ func TestSubReqRetransmissionWithSameSubIdDiffXid(t *testing.T) {
 func TestSubReqNokAndSubDelOkWithRestartInMiddle(t *testing.T) {
        CaseBegin("TestSubReqNokAndSubDelOkWithRestartInMiddle")
 
-       // Remove possible existing subscrition
-       mainCtrl.removeExistingSubscriptions(t)
-
        mainCtrl.SetResetTestFlag(t, true) // subs.DoNotWaitSubResp will be set TRUE for the subscription
        xappConn1.SendSubsReq(t, nil, nil)
        e2termConn1.RecvSubsReq(t)
@@ -2447,6 +2456,10 @@ func TestSubReqAndSubDelOkWithRestartInMiddle(t *testing.T) {
        mainCtrl.SimulateRestart(t)
        xapp.Logger.Debug("mainCtrl.SimulateRestart done")
 
+       // ReadE2Subscriptions() for testing is running in own go routine (go mainCtrl.c.ReadE2Subscriptions())
+       // That needs to be completed before successful subscription query is possible
+       <-time.After(time.Second * 1)
+
        // Check that subscription is restored correctly after restart
        resp, _ = xapp.Subscription.QuerySubscriptions()
        assert.Equal(t, resp[0].SubscriptionID, int64(e2SubsId))
@@ -2541,7 +2554,7 @@ func TestSubReqAndSubDelOkSameActionWithRestartsInMiddle(t *testing.T) {
        e2SubsId2 := xappConn2.RecvSubsResp(t, cretrans2)
 
        // Check subscription
-       resp, _ := xapp.Subscription.QuerySubscriptions() ////////////////////////////////
+       resp, _ := xapp.Subscription.QuerySubscriptions()
        assert.Equal(t, resp[0].SubscriptionID, int64(e2SubsId1))
        assert.Equal(t, resp[0].Meid, "RAN_NAME_1")
        assert.Equal(t, resp[0].ClientEndpoint, []string{"localhost:13560", "localhost:13660"})
@@ -2549,6 +2562,10 @@ func TestSubReqAndSubDelOkSameActionWithRestartsInMiddle(t *testing.T) {
        mainCtrl.SimulateRestart(t)
        xapp.Logger.Debug("mainCtrl.SimulateRestart done")
 
+       // ReadE2Subscriptions() for testing is running in own go routine (go mainCtrl.c.ReadE2Subscriptions())
+       // That needs to be completed before successful subscription query is possible
+       <-time.After(time.Second * 1)
+
        // Check that subscription is restored correctly after restart
        resp, _ = xapp.Subscription.QuerySubscriptions()
        assert.Equal(t, resp[0].SubscriptionID, int64(e2SubsId1))
@@ -2564,6 +2581,10 @@ func TestSubReqAndSubDelOkSameActionWithRestartsInMiddle(t *testing.T) {
        mainCtrl.SimulateRestart(t)
        xapp.Logger.Debug("mainCtrl.SimulateRestart done")
 
+       // ReadE2Subscriptions() for testing is running in own go routine (go mainCtrl.c.ReadE2Subscriptions())
+       // Submgr need be ready before successful subscription deletion is possible
+       <-time.After(time.Second * 1)
+
        //Del2
        deltrans2 := xappConn2.SendSubsDelReq(t, nil, e2SubsId2)
        delreq2, delmsg2 := e2termConn1.RecvSubsDelReq(t)
@@ -2687,6 +2708,7 @@ func TestRESTSubReqAndRouteNok(t *testing.T) {
        mainCtrl.wait_subs_clean(t, newSubsId, 10)
        waitSubsCleanup(t, e2SubsId, 10)
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubReqAndRouteUpdateNok(t *testing.T) {
@@ -2701,10 +2723,10 @@ func TestRESTSubReqAndRouteUpdateNok(t *testing.T) {
                Counter{cRestSubNotifToXapp, 1},
                Counter{cRestSubFailNotifToXapp, 1},
                Counter{cRouteCreateUpdateFail, 1},
-               Counter{cRestSubDelReqFromXapp, 1},
+               Counter{cRestSubDelReqFromXapp, 2},
                Counter{cSubDelReqToE2, 1},
                Counter{cSubDelRespFromE2, 1},
-               Counter{cRestSubDelRespToXapp, 1},
+               Counter{cRestSubDelRespToXapp, 2},
        })
 
        var params *teststube2ap.RESTSubsReqParams = nil
@@ -2723,18 +2745,19 @@ func TestRESTSubReqAndRouteUpdateNok(t *testing.T) {
        xapp.Logger.Debug("Send REST subscriber request for second subscriber : %v", restSubId2)
        xappConn2.ExpectRESTNotificationNok(t, restSubId2, "allFail")
        waiter.WaitResult(t)
-       // e2SubsId2 := xappConn2.WaitRESTNotification(t, restSubId2) - TOD: missing delete
        xappConn2.WaitRESTNotification(t, restSubId2)
 
        queryXappSubscription(t, int64(e2SubsId), "RAN_NAME_1", []string{"localhost:13560"})
 
        deleteSubscription(t, xappConn1, e2termConn1, &restSubId)
+       xappConn2.SendRESTSubsDelReq(t, &restSubId2)
 
        mainCtrl.wait_subs_clean(t, newSubsId, 10)
        //Wait that subs is cleaned
        waitSubsCleanup(t, e2SubsId, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubDelReqAndRouteDeleteNok(t *testing.T) {
@@ -2768,8 +2791,8 @@ func TestRESTSubDelReqAndRouteDeleteNok(t *testing.T) {
        waiter.WaitResult(t)
 
        waitSubsCleanup(t, e2SubsId, 10)
-
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubMergeDelAndRouteUpdateNok(t *testing.T) {
@@ -2777,6 +2800,7 @@ func TestRESTSubMergeDelAndRouteUpdateNok(t *testing.T) {
 
        mainCtrl.CounterValuesToBeVeriefied(t, CountersToBeAdded{
                Counter{cRestSubReqFromXapp, 2},
+               Counter{cMergedSubscriptions, 1},
                Counter{cRestSubRespToXapp, 2},
                Counter{cSubReqToE2, 1},
                Counter{cSubRespFromE2, 1},
@@ -2786,6 +2810,7 @@ func TestRESTSubMergeDelAndRouteUpdateNok(t *testing.T) {
                Counter{cSubDelReqToE2, 1},
                Counter{cSubDelRespFromE2, 1},
                Counter{cRestSubDelRespToXapp, 2},
+               Counter{cUnmergedSubscriptions, 1},
        })
 
        var params *teststube2ap.RESTSubsReqParams = nil
@@ -2810,6 +2835,7 @@ func TestRESTSubMergeDelAndRouteUpdateNok(t *testing.T) {
 
        waitSubsCleanup(t, e2SubsId2, 10)
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -2865,33 +2891,32 @@ func TestRESTSubReqRetransmission(t *testing.T) {
        // Subs Create
        const subReqCount int = 1
 
-       // In order to force both XAPP's to create their own subscriptions, force rtmgr to block a while so that 2nd create
-       // gets into execution before the rtmgrg responds for the first one.
-       waiter := rtmgrHttp.AllocNextSleep(10, true)
        params := xappConn1.GetRESTSubsReqReportParams(subReqCount)
-       restSubId1 := xappConn1.SendRESTSubsReq(t, params)
-       xappConn2.SendRESTSubsReq(t, params)
+       restSubId := xappConn1.SendRESTSubsReq(t, params)
 
-       waiter.WaitResult(t)
+       xappConn1.SendRESTSubsReq(t, params)
+       <-time.After(time.Second * 1)
 
-       xappConn1.WaitListedRestNotifications(t, []string{restSubId1})
+       xappConn1.WaitListedRestNotifications(t, []string{restSubId})
 
        // Depending one goroutine scheduling order, we cannot say for sure which xapp reaches e2term first. Thus
-       // the order is not significant he6re.
+       // the order is not significant here.
        crereq, cremsg := e2termConn1.RecvSubsReq(t)
        e2termConn1.SendSubsResp(t, crereq, cremsg)
 
-       e2SubsIdA := <-xappConn1.ListedRESTNotifications
-       xapp.Logger.Debug("TEST: 1.st XAPP notification received e2SubsId=%v", e2SubsIdA)
+       e2SubsId := <-xappConn1.ListedRESTNotifications
+
+       xapp.Logger.Debug("TEST: XAPP notification received e2SubsId=%v", e2SubsId)
 
        // Del1
-       xappConn1.SendRESTSubsDelReq(t, &restSubId1)
+       xappConn1.SendRESTSubsDelReq(t, &restSubId)
        delreq1, delmsg1 := e2termConn1.RecvSubsDelReq(t)
        e2termConn1.SendSubsDelResp(t, delreq1, delmsg1)
 
-       mainCtrl.wait_multi_subs_clean(t, []uint32{e2SubsIdA.E2SubsId}, 10)
+       mainCtrl.wait_multi_subs_clean(t, []uint32{e2SubsId.E2SubsId}, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -2941,6 +2966,7 @@ func TestRESTSubReqRetransmissionV2(t *testing.T) {
 
        mainCtrl.CounterValuesToBeVeriefied(t, CountersToBeAdded{
                Counter{cRestSubReqFromXapp, 3},
+               Counter{cDuplicateE2SubReq, 2},
                Counter{cRestSubRespToXapp, 3},
                Counter{cSubReqToE2, 1},
                Counter{cSubRespFromE2, 1},
@@ -2957,26 +2983,27 @@ func TestRESTSubReqRetransmissionV2(t *testing.T) {
 
        queryXappSubscription(t, int64(e2SubsId), "RAN_NAME_1", []string{"localhost:13560"})
 
+       mainCtrl.WaitOngoingRequestMapEmpty()
+
        //1.st resend
        restSubId_resend := xappConn1.SendRESTSubsReq(t, params)
 
        assert.Equal(t, restSubId_resend, restSubId)
 
-       <-time.After(100 * time.Millisecond)
+       mainCtrl.WaitOngoingRequestMapEmpty()
 
        //2.nd resend
        restSubId_resend2 := xappConn1.SendRESTSubsReq(t, params)
 
        assert.Equal(t, restSubId_resend2, restSubId)
 
-       <-time.After(100 * time.Millisecond)
+       mainCtrl.WaitOngoingRequestMapEmpty()
 
        deleteSubscription(t, xappConn1, e2termConn1, &restSubId)
 
        waitSubsCleanup(t, e2SubsId, 10)
-
-       //Wait that subs is cleaned
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -3025,6 +3052,7 @@ func TestRESTSubReqRetransmissionV3(t *testing.T) {
 
        mainCtrl.CounterValuesToBeVeriefied(t, CountersToBeAdded{
                Counter{cRestSubReqFromXapp, 3},
+               Counter{cDuplicateE2SubReq, 2},
                Counter{cRestSubRespToXapp, 3},
                Counter{cSubReqToE2, 1},
                Counter{cSubRespFromE2, 1},
@@ -3041,7 +3069,7 @@ func TestRESTSubReqRetransmissionV3(t *testing.T) {
 
        queryXappSubscription(t, int64(e2SubsId), "RAN_NAME_1", []string{"localhost:13560"})
 
-       <-time.After(100 * time.Millisecond)
+       mainCtrl.WaitOngoingRequestMapEmpty()
 
        //1.st resend with subscription ID
        params.SetSubscriptionID(&restSubId)
@@ -3049,7 +3077,7 @@ func TestRESTSubReqRetransmissionV3(t *testing.T) {
 
        assert.Equal(t, restSubId_resend, restSubId)
 
-       <-time.After(100 * time.Millisecond)
+       mainCtrl.WaitOngoingRequestMapEmpty()
 
        //2.nd resend without subscription ID (faking app restart)
        params = xappConn1.GetRESTSubsReqReportParams(subReqCount)
@@ -3057,14 +3085,13 @@ func TestRESTSubReqRetransmissionV3(t *testing.T) {
 
        assert.Equal(t, restSubId_resend2, restSubId)
 
-       <-time.After(100 * time.Millisecond)
+       mainCtrl.WaitOngoingRequestMapEmpty()
 
        deleteSubscription(t, xappConn1, e2termConn1, &restSubId)
 
        waitSubsCleanup(t, e2SubsId, 10)
-
-       //Wait that subs is cleaned
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -3129,6 +3156,7 @@ func TestRESTSubReqRetransmissionV4(t *testing.T) {
 
        mainCtrl.CounterValuesToBeVeriefied(t, CountersToBeAdded{
                Counter{cRestSubReqFromXapp, 3},
+               Counter{cDuplicateE2SubReq, 2},
                Counter{cRestSubRespToXapp, 3},
                Counter{cSubReqToE2, 2},
                Counter{cSubRespFromE2, 2},
@@ -3143,7 +3171,7 @@ func TestRESTSubReqRetransmissionV4(t *testing.T) {
 
        restSubId, e2SubsId := createSubscription(t, xappConn1, e2termConn1, params)
 
-       <-time.After(100 * time.Millisecond)
+       mainCtrl.WaitOngoingRequestMapEmpty()
 
        // Send modified  requst, this time with e2 subscriptions.
        params2 := xappConn1.GetRESTSubsReqReportParams(subReqCount + 1)
@@ -3164,7 +3192,7 @@ func TestRESTSubReqRetransmissionV4(t *testing.T) {
        e2SubsId2 := xappConn1.WaitRESTNotification(t, restSubId_resend)
        assert.NotEqual(t, e2SubsId2, 0)
 
-       <-time.After(100 * time.Millisecond)
+       mainCtrl.WaitOngoingRequestMapEmpty()
 
        xapp.Subscription.SetResponseCB(xappConn1.SubscriptionRespHandler)
        params = xappConn1.GetRESTSubsReqReportParams(subReqCount)
@@ -3177,15 +3205,16 @@ func TestRESTSubReqRetransmissionV4(t *testing.T) {
        e2SubsId1 = xappConn1.WaitAnyRESTNotification(t)
        assert.Equal(t, e2SubsId, e2SubsId1)
 
+       mainCtrl.WaitOngoingRequestMapEmpty()
+
        // Delete both e2 subscriptions
        xappConn1.SendRESTSubsDelReq(t, &restSubId)
        e2SubsIds := []uint32{e2SubsId, e2SubsId2}
        sendAndReceiveMultipleE2DelReqs(t, e2SubsIds, e2termConn1)
 
        waitSubsCleanup(t, e2SubsId, 10)
-
-       //Wait that subs is cleaned
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -3250,6 +3279,7 @@ func TestRESTSubReqRetransmissionV5(t *testing.T) {
 
        mainCtrl.CounterValuesToBeVeriefied(t, CountersToBeAdded{
                Counter{cRestSubReqFromXapp, 3},
+               Counter{cDuplicateE2SubReq, 2},
                Counter{cRestSubRespToXapp, 3},
                Counter{cSubReqToE2, 2},
                Counter{cSubRespFromE2, 2},
@@ -3264,7 +3294,7 @@ func TestRESTSubReqRetransmissionV5(t *testing.T) {
 
        restSubId, e2SubsId := createSubscription(t, xappConn1, e2termConn1, params)
 
-       <-time.After(100 * time.Millisecond)
+       mainCtrl.WaitOngoingRequestMapEmpty()
 
        // Send modified  requst, this time with e2 subscriptions.
        params2 := xappConn1.GetRESTSubsReqReportParams(subReqCount + 1)
@@ -3287,7 +3317,7 @@ func TestRESTSubReqRetransmissionV5(t *testing.T) {
        e2SubsId2 := xappConn1.WaitRESTNotification(t, restSubId_resend)
        assert.NotEqual(t, e2SubsId2, 0)
 
-       <-time.After(100 * time.Millisecond)
+       mainCtrl.WaitOngoingRequestMapEmpty()
 
        xapp.Subscription.SetResponseCB(xappConn1.SubscriptionRespHandler)
        params = xappConn1.GetRESTSubsReqReportParams(subReqCount)
@@ -3300,15 +3330,16 @@ func TestRESTSubReqRetransmissionV5(t *testing.T) {
        e2SubsId1 = xappConn1.WaitAnyRESTNotification(t)
        assert.Equal(t, e2SubsId, e2SubsId1)
 
+       mainCtrl.WaitOngoingRequestMapEmpty()
+
        // Delete both e2 subscriptions
        xappConn1.SendRESTSubsDelReq(t, &restSubId)
        e2SubsIds := []uint32{e2SubsId, e2SubsId2}
        sendAndReceiveMultipleE2DelReqs(t, e2SubsIds, e2termConn1)
 
        waitSubsCleanup(t, e2SubsId, 10)
-
-       //Wait that subs is cleaned
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -3382,6 +3413,7 @@ func TestRESTSubReqRetransmissionV6(t *testing.T) {
 
        mainCtrl.CounterValuesToBeVeriefied(t, CountersToBeAdded{
                Counter{cRestSubReqFromXapp, 3},
+               Counter{cDuplicateE2SubReq, 1},
                Counter{cRestSubRespToXapp, 3},
                Counter{cSubReqToE2, 3},
                Counter{cSubRespFromE2, 3},
@@ -3396,7 +3428,7 @@ func TestRESTSubReqRetransmissionV6(t *testing.T) {
 
        restSubId, e2SubsId := createSubscription(t, xappConn1, e2termConn1, params)
 
-       <-time.After(100 * time.Millisecond)
+       mainCtrl.WaitOngoingRequestMapEmpty()
 
        // Send modified  requst, this time with e2 subscriptions.
        params2 := xappConn1.GetRESTSubsReqReportParams(subReqCount + 1)
@@ -3417,7 +3449,7 @@ func TestRESTSubReqRetransmissionV6(t *testing.T) {
        e2SubsId2 := xappConn1.WaitRESTNotification(t, restSubId_resend)
        assert.NotEqual(t, e2SubsId2, 0)
 
-       <-time.After(100 * time.Millisecond)
+       mainCtrl.WaitOngoingRequestMapEmpty()
 
        // Delete both e2 subscriptions
        xappConn1.SendRESTSubsDelReq(t, &restSubId)
@@ -3431,14 +3463,13 @@ func TestRESTSubReqRetransmissionV6(t *testing.T) {
        // fresh create.
        restSubId, e2SubsId = createSubscription(t, xappConn1, e2termConn1, params)
 
-       <-time.After(100 * time.Millisecond)
+       mainCtrl.WaitOngoingRequestMapEmpty()
 
        deleteSubscription(t, xappConn1, e2termConn1, &restSubId)
 
        waitSubsCleanup(t, e2SubsId, 10)
-
-       //Wait that subs is cleaned
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubDelReqRetransmission(t *testing.T) {
@@ -3453,7 +3484,7 @@ func TestRESTSubDelReqRetransmission(t *testing.T) {
                Counter{cRestSubDelReqFromXapp, 2},
                Counter{cSubDelReqToE2, 1},
                Counter{cSubDelRespFromE2, 1},
-               Counter{cRestSubDelRespToXapp, 1},
+               Counter{cRestSubDelRespToXapp, 2},
        })
 
        var params *teststube2ap.RESTSubsReqParams = nil
@@ -3474,8 +3505,8 @@ func TestRESTSubDelReqRetransmission(t *testing.T) {
        e2termConn1.SendSubsDelResp(t, delreq, delmsg)
 
        waitSubsCleanup(t, e2SubsId, 10)
-
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -3517,6 +3548,7 @@ func TestRESTSubReqDelReq(t *testing.T) {
                Counter{cSubRespFromE2, 1},
                Counter{cRestSubNotifToXapp, 1},
                Counter{cRestSubDelReqFromXapp, 2},
+               Counter{cRestSubDelFailToXapp, 1},
                Counter{cSubDelReqToE2, 1},
                Counter{cSubDelRespFromE2, 1},
                Counter{cRestSubDelRespToXapp, 1},
@@ -3546,7 +3578,7 @@ func TestRESTSubReqDelReq(t *testing.T) {
        // Wait that subs is cleaned
        waitSubsCleanup(t, e2SubsId, 10)
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubDelReqCollision(t *testing.T) {
@@ -3604,7 +3636,7 @@ func TestRESTSubReqAndSubDelOkTwoParallel(t *testing.T) {
        waitSubsCleanup(t, e2SubsIdB.E2SubsId, 10)
 
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSameSubsDiffRan(t *testing.T) {
@@ -3641,7 +3673,7 @@ func TestRESTSameSubsDiffRan(t *testing.T) {
        waitSubsCleanup(t, e2SubsId2, 10)
 
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubReqRetryInSubmgr(t *testing.T) {
@@ -3652,6 +3684,7 @@ func TestRESTSubReqRetryInSubmgr(t *testing.T) {
                Counter{cRestSubReqFromXapp, 1},
                Counter{cRestSubRespToXapp, 1},
                Counter{cSubReqToE2, 1},
+               Counter{cSubReqTimerExpiry, 1},
                Counter{cSubReReqToE2, 1},
                Counter{cSubRespFromE2, 1},
                Counter{cRestSubNotifToXapp, 1},
@@ -3685,7 +3718,7 @@ func TestRESTSubReqRetryInSubmgr(t *testing.T) {
        waitSubsCleanup(t, e2SubsId, 10)
 
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -3737,8 +3770,11 @@ func TestRESTSubReqRetryNoRespSubDelRespInSubmgr(t *testing.T) {
                Counter{cSubReqToE2, 1},
                Counter{cSubReReqToE2, 1},
                Counter{cSubReqTimerExpiry, 2},
+               Counter{cRestSubFailNotifToXapp, 1},
                Counter{cSubDelReqToE2, 1},
                Counter{cSubDelRespFromE2, 1},
+               Counter{cRestSubDelReqFromXapp, 1},
+               Counter{cRestSubDelRespToXapp, 1},
        })
 
        params := xappConn1.GetRESTSubsReqReportParams(subReqCount)
@@ -3756,10 +3792,12 @@ func TestRESTSubReqRetryNoRespSubDelRespInSubmgr(t *testing.T) {
        e2termConn1.SendSubsDelResp(t, delreq, delmsg)
        xappConn1.WaitRESTNotification(t, restSubId)
 
+       xappConn1.SendRESTSubsDelReq(t, &restSubId)
+
        // Wait that subs is cleaned
        waitSubsCleanup(t, delreq.RequestId.InstanceId, 10)
-
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestREST2eTermNotRespondingToSubReq(t *testing.T) {
@@ -3772,8 +3810,12 @@ func TestREST2eTermNotRespondingToSubReq(t *testing.T) {
                Counter{cSubReqToE2, 1},
                Counter{cSubReReqToE2, 1},
                Counter{cSubReqTimerExpiry, 2},
+               Counter{cSubDelReReqToE2, 1},
+               Counter{cRestSubFailNotifToXapp, 1},
                Counter{cSubDelReqToE2, 1},
                Counter{cSubDelReqTimerExpiry, 2},
+               Counter{cRestSubDelReqFromXapp, 1},
+               Counter{cRestSubDelRespToXapp, 1},
        })
 
        params := xappConn1.GetRESTSubsReqReportParams(subReqCount)
@@ -3795,10 +3837,11 @@ func TestREST2eTermNotRespondingToSubReq(t *testing.T) {
 
        e2SubsId := xappConn1.WaitRESTNotification(t, restSubId)
 
-       waitSubsCleanup(t, e2SubsId, 10)
+       xappConn1.SendRESTSubsDelReq(t, &restSubId)
 
+       waitSubsCleanup(t, e2SubsId, 10)
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -3849,9 +3892,12 @@ func TestRESTSubReqTwoRetriesNoRespAtAllInSubmgr(t *testing.T) {
                Counter{cSubReqToE2, 1},
                Counter{cSubReReqToE2, 1},
                Counter{cSubReqTimerExpiry, 2},
+               Counter{cRestSubFailNotifToXapp, 1},
                Counter{cSubDelReqToE2, 1},
                Counter{cSubDelReReqToE2, 1},
                Counter{cSubDelReqTimerExpiry, 2},
+               Counter{cRestSubDelReqFromXapp, 1},
+               Counter{cRestSubDelRespToXapp, 1},
        })
 
        params := xappConn1.GetRESTSubsReqReportParams(subReqCount)
@@ -3873,9 +3919,11 @@ func TestRESTSubReqTwoRetriesNoRespAtAllInSubmgr(t *testing.T) {
 
        e2SubsId := xappConn1.WaitRESTNotification(t, restSubId)
 
-       waitSubsCleanup(t, e2SubsId, 10)
+       xappConn1.SendRESTSubsDelReq(t, &restSubId)
 
+       waitSubsCleanup(t, e2SubsId, 10)
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -3915,6 +3963,7 @@ func TestRESTSubReqSubFailRespInSubmgr(t *testing.T) {
                Counter{cSubFailFromE2, 1},
                Counter{cRestSubFailNotifToXapp, 1},
                Counter{cRestSubDelReqFromXapp, 1},
+               Counter{cRestSubDelRespToXapp, 1},
        })
 
        const subReqCount int = 1
@@ -3942,7 +3991,7 @@ func TestRESTSubReqSubFailRespInSubmgr(t *testing.T) {
        waitSubsCleanup(t, e2SubsId, 10)
 
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -3983,6 +4032,7 @@ func TestRESTSubDelReqRetryInSubmgr(t *testing.T) {
                Counter{cRestSubNotifToXapp, 1},
                Counter{cRestSubDelReqFromXapp, 1},
                Counter{cSubDelReqToE2, 1},
+               Counter{cSubDelReqTimerExpiry, 1},
                Counter{cSubDelReReqToE2, 1},
                Counter{cSubDelRespFromE2, 1},
                Counter{cRestSubDelRespToXapp, 1},
@@ -4005,6 +4055,7 @@ func TestRESTSubDelReqRetryInSubmgr(t *testing.T) {
        waitSubsCleanup(t, e2SubsId, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -4043,6 +4094,7 @@ func TestRESTSubDelReqTwoRetriesNoRespInSubmgr(t *testing.T) {
                Counter{cRestSubNotifToXapp, 1},
                Counter{cRestSubDelReqFromXapp, 1},
                Counter{cSubDelReqToE2, 1},
+               Counter{cSubDelReqTimerExpiry, 1},
                Counter{cSubDelReReqToE2, 1},
                Counter{cSubDelRespFromE2, 1},
                Counter{cRestSubDelRespToXapp, 1},
@@ -4066,6 +4118,7 @@ func TestRESTSubDelReqTwoRetriesNoRespInSubmgr(t *testing.T) {
        waitSubsCleanup(t, e2SubsId, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -4122,6 +4175,7 @@ func TestRESTSubDelReqSubDelFailRespInSubmgr(t *testing.T) {
        waitSubsCleanup(t, e2SubsId, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -4221,8 +4275,8 @@ func TestRESTSubReqAndSubDelOkSameAction(t *testing.T) {
 
        //Wait that subs is cleaned
        waitSubsCleanup(t, e2SubsId2, 10)
-
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -4315,8 +4369,8 @@ func TestRESTSubReqAndSubDelOkSameActionParallel(t *testing.T) {
        e2termConn1.SendSubsDelResp(t, delreq2, delmsg2)
 
        waitSubsCleanup(t, e2SubsId2, 10)
-
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -4339,7 +4393,7 @@ func TestRESTSubReqAndSubDelOkSameActionParallel(t *testing.T) {
 //     | RESTSubReq2                   |              |
 //     |------------------------------>|              |
 //     |             |                 |              |
-//     |               RESTSubDelResp2 |              |
+//     |               RESTSubResp2    |              |
 //     |<------------------------------|              |
 //     |             |                 | SubReq1      |
 //     |             |                 |------------->|
@@ -4375,15 +4429,18 @@ func TestRESTSubReqAndSubDelNoAnswerSameActionParallel(t *testing.T) {
 
        mainCtrl.CounterValuesToBeVeriefied(t, CountersToBeAdded{
                Counter{cRestSubReqFromXapp, 2},
+               Counter{cMergedSubscriptions, 1},
                Counter{cRestSubRespToXapp, 2},
                Counter{cSubReqToE2, 1},
+               Counter{cSubReqTimerExpiry, 2},
+               Counter{cSubReReqToE2, 1},
                Counter{cRestSubFailNotifToXapp, 2},
+               Counter{cUnmergedSubscriptions, 1},
                Counter{cRestSubDelReqFromXapp, 2},
                Counter{cSubDelReqToE2, 1},
                Counter{cSubDelRespFromE2, 1},
                Counter{cRestSubDelRespToXapp, 2},
        })
-
        const subReqCount int = 1
 
        // Req1
@@ -4421,8 +4478,8 @@ func TestRESTSubReqAndSubDelNoAnswerSameActionParallel(t *testing.T) {
 
        //Wait that subs is cleaned
        waitSubsCleanup(t, e2SubsIdA.E2SubsId, 10)
-
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -4475,10 +4532,12 @@ func TestRESTSubReqAndSubDelNokSameActionParallel(t *testing.T) {
 
        mainCtrl.CounterValuesToBeVeriefied(t, CountersToBeAdded{
                Counter{cRestSubReqFromXapp, 2},
+               Counter{cMergedSubscriptions, 1},
                Counter{cRestSubRespToXapp, 2},
                Counter{cSubReqToE2, 1},
                Counter{cSubFailFromE2, 1},
                Counter{cRestSubFailNotifToXapp, 2},
+               Counter{cUnmergedSubscriptions, 1},
                Counter{cRestSubDelReqFromXapp, 2},
                Counter{cRestSubDelRespToXapp, 2},
        })
@@ -4517,8 +4576,8 @@ func TestRESTSubReqAndSubDelNokSameActionParallel(t *testing.T) {
        //Wait that subs is cleaned
        waitSubsCleanup(t, e2SubsIdA.E2SubsId, 10)
        waitSubsCleanup(t, e2SubsIdB.E2SubsId, 10)
-
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubReqPolicyAndSubDelOk(t *testing.T) {
@@ -4554,6 +4613,7 @@ func TestRESTSubReqPolicyAndSubDelOk(t *testing.T) {
        // Wait that subs is cleaned
        waitSubsCleanup(t, e2SubsId, 10)
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -4648,6 +4708,7 @@ func TestRESTSubReqPolicyChangeAndSubDelOk(t *testing.T) {
        // Wait that subs is cleaned
        waitSubsCleanup(t, e2SubsId, 10)
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -4729,6 +4790,7 @@ func TestRESTSubReqPolicyChangeNOk(t *testing.T) {
        // Wait that subs is cleaned
        waitSubsCleanup(t, e2SubsId, 10)
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -4832,6 +4894,7 @@ func TestRESTSubReqAndSubDelOkTwoE2termParallel(t *testing.T) {
        waitSubsCleanup(t, e2SubsId2, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -4931,6 +4994,7 @@ func TestRESTSubReqInsertAndSubDelOk(t *testing.T) {
        // Wait that subs is cleaned
        waitSubsCleanup(t, e2SubsId, 10)
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -4944,6 +5008,8 @@ func TestRESTSubReqInsertAndSubDelOk(t *testing.T) {
 //     | RESTSubReq   |              |
 //     |------------->|              |
 //     |              |              |
+//     |  RESTSubResp |              |
+//     |<-------------|              |
 //     |              | SubReq       |
 //     |              |------------->|
 //     |              |              |
@@ -4959,6 +5025,15 @@ func TestRESTSubReqInsertAndSubDelOk(t *testing.T) {
 //     |              |   SubDelResp |
 //     |              |<-------------|
 //     |              |              |
+//     |    RESTNotif |              |
+//     |    unsuccess |              |
+//     |<-------------|              |
+//     |              |              |
+//     | RESTSubDelReq|              |
+//     |------------->|              |
+//     |              |              |
+//     |RESTSubDelResp|              |
+//     |<-------------|              |
 //
 //-----------------------------------------------------------------------------
 func TestRESTSubReqNokAndSubDelOkWithRestartInMiddle(t *testing.T) {
@@ -4971,13 +5046,12 @@ func TestRESTSubReqNokAndSubDelOkWithRestartInMiddle(t *testing.T) {
                Counter{cSubDelReqFromXapp, 1},
                Counter{cSubDelReqToE2, 1},
                Counter{cSubDelRespFromE2, 1},
+               Counter{cRestSubDelReqFromXapp, 1},
+               Counter{cRestSubDelRespToXapp, 1},
        })
 
        const subReqCount int = 1
 
-       // Remove possible existing subscription
-       mainCtrl.removeExistingSubscriptions(t)
-
        params := xappConn1.GetRESTSubsReqReportParams(subReqCount)
 
        //Req
@@ -4992,16 +5066,19 @@ func TestRESTSubReqNokAndSubDelOkWithRestartInMiddle(t *testing.T) {
        mainCtrl.SimulateRestart(t)
        xapp.Logger.Debug("mainCtrl.SimulateRestart done")
 
-       //Del
+       // Deleletion of uncompleted subscription
        delreq, delmsg := e2termConn1.RecvSubsDelReq(t)
        e2termConn1.SendSubsDelResp(t, delreq, delmsg)
 
+       //Del
+       xappConn1.SendRESTSubsDelReq(t, &restSubId)
+
        xappConn1.TestMsgChanEmpty(t)
-       xappConn2.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -5054,15 +5131,12 @@ func TestRESTSubReqAndSubDelOkWithRestartInMiddle(t *testing.T) {
                Counter{cRestSubNotifToXapp, 1},
                Counter{cRestSubDelReqFromXapp, 1},
                Counter{cSubDelReqToE2, 1},
+               Counter{cSubDelRespFromE2, 1},
                Counter{cRestSubDelRespToXapp, 1},
        })
 
-       // Remove possible existing subscription
-       mainCtrl.removeExistingSubscriptions(t)
-
-       var params *teststube2ap.RESTSubsReqParams = nil
-
        // Create subscription
+       var params *teststube2ap.RESTSubsReqParams = nil
        restSubId, e2SubsId := createSubscription(t, xappConn1, e2termConn1, params)
        xapp.Logger.Debug("Send REST subscriber request for subscriber : %v", restSubId)
 
@@ -5072,6 +5146,10 @@ func TestRESTSubReqAndSubDelOkWithRestartInMiddle(t *testing.T) {
        mainCtrl.SimulateRestart(t)
        xapp.Logger.Debug("mainCtrl.SimulateRestart done")
 
+       // ReadE2Subscriptions() for testing is running in own go routine (go mainCtrl.c.ReadE2Subscriptions())
+       // That needs to be completed before successful subscription query is possible
+       <-time.After(time.Second * 1)
+
        // Check subscription
        queryXappSubscription(t, int64(e2SubsId), "RAN_NAME_1", []string{"localhost:13560"})
 
@@ -5082,6 +5160,7 @@ func TestRESTSubReqAndSubDelOkWithRestartInMiddle(t *testing.T) {
        waitSubsCleanup(t, e2SubsId, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -5138,7 +5217,6 @@ func TestRESTSubReqAndSubDelOkWithRestartInMiddle(t *testing.T) {
 //     |             |                 |              |
 //
 //-----------------------------------------------------------------------------
-
 func TestRESTSubReqAndSubDelOkSameActionWithRestartsInMiddle(t *testing.T) {
        CaseBegin("TestRESTSubReqAndSubDelOkSameActionWithRestartsInMiddle")
 
@@ -5156,12 +5234,8 @@ func TestRESTSubReqAndSubDelOkSameActionWithRestartsInMiddle(t *testing.T) {
                Counter{cRestSubDelRespToXapp, 2},
        })
 
-       // Remove possible existing subscription
-       mainCtrl.removeExistingSubscriptions(t)
-
-       var params *teststube2ap.RESTSubsReqParams = nil
-
        // Create subscription 1
+       var params *teststube2ap.RESTSubsReqParams = nil
        restSubId1, e2SubsId1 := createSubscription(t, xappConn1, e2termConn1, params)
        xapp.Logger.Debug("Send REST subscriber request for subscriber 1 : %v", restSubId1)
 
@@ -5178,15 +5252,24 @@ func TestRESTSubReqAndSubDelOkSameActionWithRestartsInMiddle(t *testing.T) {
        queryXappSubscription(t, int64(e2SubsId1), "RAN_NAME_1", []string{"localhost:13560", "localhost:13660"})
 
        mainCtrl.SimulateRestart(t)
-       xapp.Logger.Debug("mainCtrl.SimulateRestart done")
+       xapp.Logger.Debug("mainCtrl.SimulateRestart done 1")
+
+       // ReadE2Subscriptions() for testing is running in own go routine (go mainCtrl.c.ReadE2Subscriptions())
+       // That needs to be completed before successful subscription delete is possible
+       <-time.After(time.Second * 1)
 
        // Delete subscription 1, and wait until it has removed the first endpoint
-       subepcnt := mainCtrl.get_subs_entrypoint_cnt(t, e2SubsId1)
        xappConn1.SendRESTSubsDelReq(t, &restSubId1)
-       mainCtrl.wait_subs_entrypoint_cnt_change(t, e2SubsId1, subepcnt, 10)
+       mainCtrl.WaitRESTSubscriptionDelete(restSubId1)
+       // Above wait does not work correctly anymore as this delay makes this test case work
 
        mainCtrl.SimulateRestart(t)
-       xapp.Logger.Debug("mainCtrl.SimulateRestart done")
+       xapp.Logger.Debug("mainCtrl.SimulateRestart done 2")
+
+       // ReadE2Subscriptions() for testing is running in own go routine (go mainCtrl.c.ReadE2Subscriptions())
+       // That needs to be completed before successful subscription query is possible
+       <-time.After(time.Second * 1)
+
        queryXappSubscription(t, int64(e2SubsId1), "RAN_NAME_1", []string{"localhost:13660"})
 
        // Delete subscription 2
@@ -5196,6 +5279,7 @@ func TestRESTSubReqAndSubDelOkSameActionWithRestartsInMiddle(t *testing.T) {
        waitSubsCleanup(t, e2SubsId2, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -5289,10 +5373,11 @@ func RESTReportSubReqAndSubDelOk(t *testing.T, subReqCount int, testIndex int) {
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 /*
-func TestRESTPolicySubReqAndSubDelOk(t *testing.T) {
+func TestRESTPolicySubReqAndSubDelOk(t *testing.T) {  was in comments already
        CaseBegin("TestRESTPolicySubReqAndSubDelOk")
 
        subReqCount := 2
@@ -5336,6 +5421,7 @@ func RESTPolicySubReqAndSubDelOk(t *testing.T, subReqCount int, testIndex int) {
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTTwoPolicySubReqAndSubDelOk(t *testing.T) {
@@ -5370,6 +5456,7 @@ func TestRESTTwoPolicySubReqAndSubDelOk(t *testing.T) {
        mainCtrl.wait_registry_empty(t, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 func TestRESTPolicySubReqAndSubDelOkFullAmount(t *testing.T) {
 
@@ -5401,6 +5488,7 @@ func TestRESTPolicySubReqAndSubDelOkFullAmount(t *testing.T) {
        mainCtrl.wait_registry_empty(t, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTTwoReportSubReqAndSubDelOk(t *testing.T) {
@@ -5435,6 +5523,7 @@ func TestRESTTwoReportSubReqAndSubDelOk(t *testing.T) {
        mainCtrl.wait_registry_empty(t, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTTwoReportSubReqAndSubDelOkNoActParams(t *testing.T) {
@@ -5469,6 +5558,7 @@ func TestRESTTwoReportSubReqAndSubDelOkNoActParams(t *testing.T) {
        mainCtrl.wait_registry_empty(t, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTFullAmountReportSubReqAndSubDelOk(t *testing.T) {
@@ -5503,6 +5593,7 @@ func TestRESTFullAmountReportSubReqAndSubDelOk(t *testing.T) {
        mainCtrl.wait_registry_empty(t, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubReqReportSameActionDiffEventTriggerDefinitionLen(t *testing.T) {
@@ -5549,7 +5640,7 @@ func TestRESTSubReqReportSameActionDiffEventTriggerDefinitionLen(t *testing.T) {
        waitSubsCleanup(t, e2SubsId2, 10)
 
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubReqReportSameActionDiffActionListLen(t *testing.T) {
@@ -5601,7 +5692,7 @@ func TestRESTSubReqReportSameActionDiffActionListLen(t *testing.T) {
        waitSubsCleanup(t, e2SubsId2, 10)
 
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubReqReportSameActionDiffActionID(t *testing.T) {
@@ -5647,7 +5738,7 @@ func TestRESTSubReqReportSameActionDiffActionID(t *testing.T) {
        waitSubsCleanup(t, e2SubsId2, 10)
 
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubReqDiffActionType(t *testing.T) {
@@ -5698,7 +5789,7 @@ func TestRESTSubReqDiffActionType(t *testing.T) {
        waitSubsCleanup(t, e2SubsId2, 10)
 
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubReqPolicyAndSubDelOkSameAction(t *testing.T) {
@@ -5749,7 +5840,7 @@ func TestRESTSubReqPolicyAndSubDelOkSameAction(t *testing.T) {
        waitSubsCleanup(t, e2SubsId2, 10)
 
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubReqReportSameActionDiffActionDefinitionLen(t *testing.T) {
@@ -5796,7 +5887,7 @@ func TestRESTSubReqReportSameActionDiffActionDefinitionLen(t *testing.T) {
        waitSubsCleanup(t, e2SubsId2, 10)
 
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubReqReportSameActionDiffActionDefinitionContents(t *testing.T) {
@@ -5843,7 +5934,7 @@ func TestRESTSubReqReportSameActionDiffActionDefinitionContents(t *testing.T) {
        waitSubsCleanup(t, e2SubsId2, 10)
 
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubReqReportSameActionDiffSubsAction(t *testing.T) {
@@ -5888,7 +5979,7 @@ func TestRESTSubReqReportSameActionDiffSubsAction(t *testing.T) {
        waitSubsCleanup(t, e2SubsId2, 10)
 
        mainCtrl.VerifyCounterValues(t)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -5947,12 +6038,15 @@ func TestRESTUnpackSubscriptionResponseDecodeFail(t *testing.T) {
        instanceId := xappConn1.WaitRESTNotification(t, restSubId)
        xapp.Logger.Debug("TEST: REST notification received e2SubsId=%v", instanceId)
 
+       xappConn1.SendRESTSubsDelReq(t, &restSubId)
+
        // Wait that subs is cleaned
        mainCtrl.wait_subs_clean(t, crereq.RequestId.InstanceId, 10)
 
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6021,12 +6115,15 @@ func TestRESTUnpackSubscriptionResponseUnknownInstanceId(t *testing.T) {
        instanceId := xappConn1.WaitRESTNotification(t, restSubId)
        xapp.Logger.Debug("TEST: REST notification received e2SubsId=%v", instanceId)
 
+       xappConn1.SendRESTSubsDelReq(t, &restSubId)
+
        // Wait that subs is cleaned
        mainCtrl.wait_subs_clean(t, orgInstanceId, 10)
 
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6103,13 +6200,15 @@ func TestRESTUnpackSubscriptionResponseNoTransaction(t *testing.T) {
        instanceId := xappConn1.WaitRESTNotification(t, restSubId)
        xapp.Logger.Debug("TEST: REST notification received e2SubsId=%v", instanceId)
 
+       xappConn1.SendRESTSubsDelReq(t, &restSubId)
+
        // Wait that subs is cleaned
        mainCtrl.wait_subs_clean(t, crereq.RequestId.InstanceId, 10)
 
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
-
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6168,12 +6267,15 @@ func TestRESTUnpackSubscriptionFailureDecodeFail(t *testing.T) {
        instanceId := xappConn1.WaitRESTNotification(t, restSubId)
        xapp.Logger.Debug("TEST: REST notification received e2SubsId=%v", instanceId)
 
+       xappConn1.SendRESTSubsDelReq(t, &restSubId)
+
        // Wait that subs is cleaned
        mainCtrl.wait_subs_clean(t, crereq.RequestId.InstanceId, 10)
 
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6240,12 +6342,15 @@ func TestRESTUnpackSubscriptionFailureUnknownInstanceId(t *testing.T) {
        instanceId := xappConn1.WaitRESTNotification(t, restSubId)
        xapp.Logger.Debug("TEST: REST notification received e2SubsId=%v", instanceId)
 
+       xappConn1.SendRESTSubsDelReq(t, &restSubId)
+
        // Wait that subs is cleaned
        mainCtrl.wait_subs_clean(t, crereq.RequestId.InstanceId, 10)
 
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6317,12 +6422,15 @@ func TestRESTUnpackSubscriptionFailureNoTransaction(t *testing.T) {
        instanceId := xappConn1.WaitRESTNotification(t, restSubId)
        xapp.Logger.Debug("TEST: REST notification received e2SubsId=%v", instanceId)
 
+       xappConn1.SendRESTSubsDelReq(t, &restSubId)
+
        // Wait that subs is cleaned
        mainCtrl.wait_subs_clean(t, crereq.RequestId.InstanceId, 10)
 
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6384,6 +6492,7 @@ func TestRESTUnpackSubscriptionDeleteResponseDecodeFail(t *testing.T) {
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6445,6 +6554,7 @@ func TestRESTUnpackSubscriptionDeleteResponseUnknownInstanceId(t *testing.T) {
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6507,6 +6617,7 @@ func TestRESTUnpackSubscriptionDeleteResponseNoTransaction(t *testing.T) {
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6567,6 +6678,7 @@ func TestRESTUnpackSubscriptionDeleteFailureDecodeFail(t *testing.T) {
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6628,6 +6740,7 @@ func TestRESTUnpackSubscriptionDeleteailureUnknownInstanceId(t *testing.T) {
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6690,6 +6803,7 @@ func TestRESTUnpackSubscriptionDeleteFailureNoTransaction(t *testing.T) {
        xappConn1.TestMsgChanEmpty(t)
        e2termConn1.TestMsgChanEmpty(t)
        mainCtrl.wait_registry_empty(t, 10)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6728,6 +6842,8 @@ func TestRESTSubReqFailAsn1PackSubReqError(t *testing.T) {
                Counter{cRestSubReqFromXapp, 1},
                Counter{cRestSubRespToXapp, 1},
                Counter{cRestSubFailNotifToXapp, 1},
+               Counter{cRestSubDelReqFromXapp, 1},
+               Counter{cRestSubDelRespToXapp, 1},
        })
 
        const subReqCount int = 1
@@ -6747,9 +6863,13 @@ func TestRESTSubReqFailAsn1PackSubReqError(t *testing.T) {
        xapp.Logger.Debug("TEST: REST notification received e2SubsId=%v", e2SubsId)
 
        e2ap_wrapper.AllowE2apToProcess(e2ap_wrapper.SUB_REQ, true)
+
+       xappConn1.SendRESTSubsDelReq(t, &restSubId)
+
        // Wait that subs is cleaned
        waitSubsCleanup(t, e2SubsId, 10)
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 func TestRESTSubReqPolicyUpdateTimeoutAndSubDelOkSameAction(t *testing.T) {
@@ -6759,6 +6879,7 @@ func TestRESTSubReqPolicyUpdateTimeoutAndSubDelOkSameAction(t *testing.T) {
                Counter{cRestSubReqFromXapp, 2},
                Counter{cRestSubRespToXapp, 2},
                Counter{cSubReqToE2, 2},
+               Counter{cSubReqTimerExpiry, 1},
                Counter{cSubRespFromE2, 1},
                Counter{cRestSubNotifToXapp, 1},
                Counter{cRestSubFailNotifToXapp, 1},
@@ -6808,6 +6929,7 @@ func TestRESTSubReqPolicyUpdateTimeoutAndSubDelOkSameAction(t *testing.T) {
        waitSubsCleanup(t, e2SubsId, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 //-----------------------------------------------------------------------------
@@ -6865,15 +6987,12 @@ func TestPolicyUpdateRESTSubReqAndSubDelOkWithRestartInMiddle(t *testing.T) {
                Counter{cSubReqToE2, 2},
                Counter{cSubRespFromE2, 1},
                Counter{cRestSubNotifToXapp, 1},
-               Counter{cRestSubNotifToXapp, 1},
                Counter{cRestSubDelReqFromXapp, 1},
                Counter{cSubDelReqToE2, 1},
+               Counter{cSubDelRespFromE2, 1},
                Counter{cRestSubDelRespToXapp, 1},
        })
 
-       // Remove possible existing subscription
-       mainCtrl.removeExistingSubscriptions(t)
-
        const e2Timeout int64 = 1
        const e2RetryCount int64 = 0
        const routingNeeded bool = false
@@ -6911,13 +7030,13 @@ func TestPolicyUpdateRESTSubReqAndSubDelOkWithRestartInMiddle(t *testing.T) {
        mainCtrl.SimulateRestart(t)
        xapp.Logger.Debug("mainCtrl.SimulateRestart done")
 
+       // ReadE2Subscriptions() for testing is running in own go routine (go mainCtrl.c.ReadE2Subscriptions())
+       // That needs to be completed before successful subscription query is possible
+       <-time.After(time.Second * 1)
+
        // Check subscription
        queryXappSubscription(t, int64(e2SubsId), "RAN_NAME_1", []string{"localhost:13560"})
 
-       xapp.Logger.Debug("Here 1")
-       //<-time.After(3 * time.Second)
-       xapp.Logger.Debug("Here 2")
-
        // Delete subscription
        xappConn1.SendRESTSubsDelReq(t, &restSubId)
        delreq, delmsg := e2termConn1.RecvSubsDelReq(t)
@@ -6927,6 +7046,7 @@ func TestPolicyUpdateRESTSubReqAndSubDelOkWithRestartInMiddle(t *testing.T) {
        waitSubsCleanup(t, e2SubsId, 10)
 
        mainCtrl.VerifyCounterValues(t)
+       mainCtrl.VerifyAllClean(t)
 }
 
 ////////////////////////////////////////////////////////////////////////////////////
@@ -7020,9 +7140,9 @@ func deleteXapp2Subscription(t *testing.T, restSubId *string) {
 
 func queryXappSubscription(t *testing.T, e2SubsId int64, meid string, endpoint []string) {
        resp, _ := xapp.Subscription.QuerySubscriptions()
-       assert.Equal(t, resp[0].SubscriptionID, e2SubsId)
-       assert.Equal(t, resp[0].Meid, meid)
-       assert.Equal(t, resp[0].ClientEndpoint, endpoint)
+       assert.Equal(t, e2SubsId, resp[0].SubscriptionID)
+       assert.Equal(t, meid, resp[0].Meid)
+       assert.Equal(t, endpoint, resp[0].ClientEndpoint)
 }
 
 func waitSubsCleanup(t *testing.T, e2SubsId uint32, timeout int) {