net_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2014 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. // +build !appengine
  5. package internal
  6. import (
  7. "sync"
  8. "testing"
  9. "time"
  10. netcontext "golang.org/x/net/context"
  11. basepb "google.golang.org/appengine/internal/base"
  12. )
  13. func TestDialLimit(t *testing.T) {
  14. // Fill up semaphore with false acquisitions to permit only two TCP connections at a time.
  15. // We don't replace limitSem because that results in a data race when net/http lazily closes connections.
  16. nFake := cap(limitSem) - 2
  17. for i := 0; i < nFake; i++ {
  18. limitSem <- 1
  19. }
  20. defer func() {
  21. for i := 0; i < nFake; i++ {
  22. <-limitSem
  23. }
  24. }()
  25. f, c, cleanup := setup() // setup is in api_test.go
  26. defer cleanup()
  27. f.hang = make(chan int)
  28. // If we make two RunSlowly RPCs (which will wait for f.hang to be strobed),
  29. // then the simple Non200 RPC should hang.
  30. var wg sync.WaitGroup
  31. wg.Add(2)
  32. for i := 0; i < 2; i++ {
  33. go func() {
  34. defer wg.Done()
  35. Call(toContext(c), "errors", "RunSlowly", &basepb.VoidProto{}, &basepb.VoidProto{})
  36. }()
  37. }
  38. time.Sleep(50 * time.Millisecond) // let those two RPCs start
  39. ctx, _ := netcontext.WithTimeout(toContext(c), 50*time.Millisecond)
  40. err := Call(ctx, "errors", "Non200", &basepb.VoidProto{}, &basepb.VoidProto{})
  41. if err != errTimeout {
  42. t.Errorf("Non200 RPC returned with err %v, want errTimeout", err)
  43. }
  44. // Drain the two RunSlowly calls.
  45. f.hang <- 1
  46. f.hang <- 1
  47. wg.Wait()
  48. }