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.

65 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. msg := make([]byte, 200)
  37. _, err = c.Read(msg)
  38. opErr, ok := err.(*net.OpError)
  39. if !ok {
  40. t.Fatalf("have %v, want *net.OpError", err)
  41. }
  42. if have, want := opErr.Op, "read"; have != want {
  43. t.Errorf("have %v, want %v", have, want)
  44. }
  45. }(ln)
  46. _, err = net.Dial("tcp", ln.Addr().String())
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. <-donec
  51. }