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.
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
package socketset
|
package socketset
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"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
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user