From 3c8f48a8ac9e2122be70c7ae2080cfc7f72cb26b Mon Sep 17 00:00:00 2001 From: st-user Date: Tue, 25 May 2021 09:51:57 +0900 Subject: [PATCH] Dji Tello Halt does not terminate all the related goroutines and may wait forever when it is called multiple times Add failing unit tests. --- platforms/dji/tello/driver_test.go | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/platforms/dji/tello/driver_test.go b/platforms/dji/tello/driver_test.go index c8d25b2a8..18a584d6c 100644 --- a/platforms/dji/tello/driver_test.go +++ b/platforms/dji/tello/driver_test.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "fmt" "io" + "sync" "testing" "time" @@ -14,6 +15,15 @@ import ( var _ gobot.Driver = (*Driver)(nil) +type WriteCloserDoNothing struct{} + +func (w *WriteCloserDoNothing) Write(p []byte) (n int, err error) { + return 0, nil +} +func (w *WriteCloserDoNothing) Close() error { + return nil +} + func TestTelloDriver(t *testing.T) { d := NewDriver("8888") @@ -134,3 +144,38 @@ func TestHandleResponse(t *testing.T) { }) } } + +func TestHaltShouldTerminateAllTheRelatedGoroutines(t *testing.T) { + d := NewDriver("8888") + d.cmdConn = &WriteCloserDoNothing{} + + var wg sync.WaitGroup + wg.Add(3) + go func() { + <-d.doneCh + wg.Done() + fmt.Println("Done routine 1.") + }() + go func() { + <-d.doneCh + wg.Done() + fmt.Println("Done routine 2.") + }() + go func() { + <-d.doneCh + wg.Done() + fmt.Println("Done routine 3.") + }() + + d.Halt() + wg.Wait() +} + +func TestHaltNotWaitForeverWhenCalledMultipleTimes(t *testing.T) { + d := NewDriver("8888") + d.cmdConn = &WriteCloserDoNothing{} + + d.Halt() + d.Halt() + d.Halt() +}