Make sure all events for logical device are sent before its deletion to avoid race conditions

Change-Id: I5fcbc8e2c176cf866f8e7f68d1da777d6f0b573c
diff --git a/rw_core/core/device/logical_agent_test.go b/rw_core/core/device/logical_agent_test.go
index 1db9b25..dcd6691 100644
--- a/rw_core/core/device/logical_agent_test.go
+++ b/rw_core/core/device/logical_agent_test.go
@@ -289,6 +289,45 @@
 	globalWG.Done()
 }
 
+func (lda *LDATest) stopLogicalAgentAndCheckEventQueueIsEmpty(ctx context.Context, t *testing.T, ldAgent *LogicalAgent) {
+	queueIsEmpty := false
+	err := ldAgent.stop(ctx)
+	assert.Nil(t, err)
+	qp := ldAgent.orderedEvents.assignQueuePosition()
+	if qp.prev != nil { // we will be definitely hitting this case as we pushed events on the queue before
+		// If previous channel is closed which it should be now,
+		// only then we can know that queue is empty.
+		_, ok := <-qp.prev
+		if !ok {
+			queueIsEmpty = true
+		} else {
+			queueIsEmpty = false
+		}
+	} else {
+		queueIsEmpty = true
+	}
+	close(qp.next)
+	assert.True(t, queueIsEmpty)
+}
+
+func (lda *LDATest) updateLogicalDevice(t *testing.T, ldAgent *LogicalAgent) {
+	originalLogicalPorts := ldAgent.listLogicalDevicePorts(context.Background())
+	assert.NotNil(t, originalLogicalPorts)
+
+	// Change the state of the first port to FAILED
+	err := ldAgent.updatePortState(context.Background(), 1, voltha.OperStatus_FAILED)
+	assert.Nil(t, err)
+
+	// Change the state of the second port to TESTING
+	err = ldAgent.updatePortState(context.Background(), 2, voltha.OperStatus_TESTING)
+	assert.Nil(t, err)
+
+	// Change the state of the third port to ACTIVE
+	err = ldAgent.updatePortState(context.Background(), 3, voltha.OperStatus_ACTIVE)
+	assert.Nil(t, err)
+
+}
+
 func TestConcurrentLogicalDeviceUpdate(t *testing.T) {
 	ctx := context.Background()
 	lda := newLDATest(ctx)
@@ -308,3 +347,17 @@
 
 	wg.Wait()
 }
+
+func TestLogicalAgentStopWithEventsInQueue(t *testing.T) {
+	ctx := context.Background()
+	lda := newLDATest(ctx)
+	assert.NotNil(t, lda)
+	defer lda.stopAll(ctx)
+
+	// Start the Core
+	lda.startCore(ctx, false)
+
+	a := lda.createLogicalDeviceAgent(t)
+	lda.updateLogicalDevice(t, a)
+	lda.stopLogicalAgentAndCheckEventQueueIsEmpty(ctx, t, a)
+}