You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.2 KiB

  1. package privval
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. )
  7. func TestTCPTimeoutListenerAcceptDeadline(t *testing.T) {
  8. ln, err := net.Listen("tcp", "127.0.0.1:0")
  9. if err != nil {
  10. t.Fatal(err)
  11. }
  12. ln = newTCPTimeoutListener(ln, time.Millisecond, time.Second, time.Second)
  13. _, err = ln.Accept()
  14. opErr, ok := err.(*net.OpError)
  15. if !ok {
  16. t.Fatalf("have %v, want *net.OpError", err)
  17. }
  18. if have, want := opErr.Op, "accept"; have != want {
  19. t.Errorf("have %v, want %v", have, want)
  20. }
  21. }
  22. func TestTCPTimeoutListenerConnDeadline(t *testing.T) {
  23. ln, err := net.Listen("tcp", "127.0.0.1:0")
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. ln = newTCPTimeoutListener(ln, time.Second, time.Millisecond, time.Second)
  28. donec := make(chan struct{})
  29. go func(ln net.Listener) {
  30. defer close(donec)
  31. c, err := ln.Accept()
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. time.Sleep(2 * time.Millisecond)
  36. _, err = c.Write([]byte("foo"))
  37. opErr, ok := err.(*net.OpError)
  38. if !ok {
  39. t.Fatalf("have %v, want *net.OpError", err)
  40. }
  41. if have, want := opErr.Op, "write"; have != want {
  42. t.Errorf("have %v, want %v", have, want)
  43. }
  44. }(ln)
  45. _, err = net.Dial("tcp", ln.Addr().String())
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. <-donec
  50. }