z_last_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package net
  5. import (
  6. "flag"
  7. "fmt"
  8. "testing"
  9. "time"
  10. )
  11. var testDNSFlood = flag.Bool("dnsflood", false, "whether to test dns query flooding")
  12. func TestDNSThreadLimit(t *testing.T) {
  13. if !*testDNSFlood {
  14. t.Skip("test disabled; use -dnsflood to enable")
  15. }
  16. const N = 10000
  17. c := make(chan int, N)
  18. for i := 0; i < N; i++ {
  19. go func(i int) {
  20. LookupIP(fmt.Sprintf("%d.net-test.golang.org", i))
  21. c <- 1
  22. }(i)
  23. }
  24. // Don't bother waiting for the stragglers; stop at 0.9 N.
  25. for i := 0; i < N*9/10; i++ {
  26. if i%100 == 0 {
  27. //println("TestDNSThreadLimit:", i)
  28. }
  29. <-c
  30. }
  31. // If we're still here, it worked.
  32. }
  33. func TestLookupIPDeadline(t *testing.T) {
  34. if !*testDNSFlood {
  35. t.Skip("test disabled; use -dnsflood to enable")
  36. }
  37. const N = 5000
  38. const timeout = 3 * time.Second
  39. c := make(chan error, 2*N)
  40. for i := 0; i < N; i++ {
  41. name := fmt.Sprintf("%d.net-test.golang.org", i)
  42. go func() {
  43. _, err := lookupIPDeadline(name, time.Now().Add(timeout/2))
  44. c <- err
  45. }()
  46. go func() {
  47. _, err := lookupIPDeadline(name, time.Now().Add(timeout))
  48. c <- err
  49. }()
  50. }
  51. qstats := struct {
  52. succeeded, failed int
  53. timeout, temporary, other int
  54. unknown int
  55. }{}
  56. deadline := time.After(timeout + time.Second)
  57. for i := 0; i < 2*N; i++ {
  58. select {
  59. case <-deadline:
  60. t.Fatal("deadline exceeded")
  61. case err := <-c:
  62. switch err := err.(type) {
  63. case nil:
  64. qstats.succeeded++
  65. case Error:
  66. qstats.failed++
  67. if err.Timeout() {
  68. qstats.timeout++
  69. }
  70. if err.Temporary() {
  71. qstats.temporary++
  72. }
  73. if !err.Timeout() && !err.Temporary() {
  74. qstats.other++
  75. }
  76. default:
  77. qstats.failed++
  78. qstats.unknown++
  79. }
  80. }
  81. }
  82. // A high volume of DNS queries for sub-domain of golang.org
  83. // would be coordinated by authoritative or recursive server,
  84. // or stub resolver which implements query-response rate
  85. // limitation, so we can expect some query successes and more
  86. // failures including timeout, temporary and other here.
  87. // As a rule, unknown must not be shown but it might possibly
  88. // happen due to issue 4856 for now.
  89. t.Logf("%v succeeded, %v failed (%v timeout, %v temporary, %v other, %v unknown)", qstats.succeeded, qstats.failed, qstats.timeout, qstats.temporary, qstats.other, qstats.unknown)
  90. }