From 0c17a0b4f202eeb13a9de6e6e8d7b1cea4f01523 Mon Sep 17 00:00:00 2001 From: Kevin Kuehler Date: Sun, 20 Jan 2019 01:50:23 -0800 Subject: [PATCH] util: Add TestShutdown to socketset Test to ensure that SocketSet is nonblocking and will close when SocketSet.Shutdown() is called. Create a SocketSet that will never receive any data and leave it running in a goroutine with a WaitGroup for a second. If Shutdown is working correctly, the goroutine will be terminated after the timer expires. --- util/socketset/socketset_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/util/socketset/socketset_test.go b/util/socketset/socketset_test.go index 5a0c38a5..81747117 100644 --- a/util/socketset/socketset_test.go +++ b/util/socketset/socketset_test.go @@ -18,6 +18,7 @@ package socketset import ( + "sync" "testing" "golang.org/x/sys/unix" @@ -104,3 +105,30 @@ func TestNfd(t *testing.T) { } } } + +// test SocketSet.Shutdown() +func TestShutdown(t *testing.T) { + wg := &sync.WaitGroup{} + defer wg.Wait() + + // pass 0 so we create a socket that doesn't receive any events + ss, err := NewSocketSet(0, "pipe.sock", 0) + if err != nil { + t.Errorf("could not create SocketSet: %+v", err) + } + closeChan := make(chan struct{}) + defer close(closeChan) + defer ss.Close() + defer ss.Shutdown() + + // create a listener that never receives any data + wg.Add(1) // add a waitgroup to ensure this will block if we don't properly unblock Select + go func() { + defer wg.Done() + _, _ = ss.ReceiveBytes() // this should block + select { + case <-closeChan: + return + } + }() +}