ipv4_go1.11_test.go 638 B

123456789101112131415161718192021222324252627282930313233343536
  1. // +build go1.11
  2. package water
  3. import (
  4. "context"
  5. "testing"
  6. "time"
  7. )
  8. func TestCloseUnblockPendingRead(t *testing.T) {
  9. ifce, err := New(Config{DeviceType: TUN})
  10. if err != nil {
  11. t.Fatalf("creating TUN error: %v\n", err)
  12. }
  13. c := make(chan struct{})
  14. go func() {
  15. ifce.Read(make([]byte, 1<<16))
  16. close(c)
  17. }()
  18. // make sure ifce.Close() happens after ifce.Read() blocks
  19. time.Sleep(1 * time.Second)
  20. ifce.Close()
  21. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  22. defer cancel()
  23. select {
  24. case <-c:
  25. t.Log("Pending Read unblocked")
  26. case <-ctx.Done():
  27. t.Fatal("Timeouted, pending read blocked")
  28. }
  29. }