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.
 
 
 

79 line
1.9 KiB

  1. /*
  2. Copyright 2016 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package internal
  14. import (
  15. "testing"
  16. "time"
  17. "google.golang.org/grpc/naming"
  18. )
  19. func TestConnectionPool(t *testing.T) {
  20. addr := "127.0.0.1:123"
  21. ds := DialSettings{Endpoint: addr}
  22. pr := NewPoolResolver(4, &ds)
  23. watcher, err := pr.Resolve(addr)
  24. if err != nil {
  25. t.Fatalf("Resolve: %v", err)
  26. }
  27. updates, err := watcher.Next()
  28. if err != nil {
  29. t.Fatalf("Next: %v", err)
  30. }
  31. if len(updates) != 4 {
  32. t.Fatalf("Update count: %v", err)
  33. }
  34. metaSeen := make(map[interface{}]bool)
  35. for _, u := range updates {
  36. if u.Addr != addr {
  37. t.Errorf("Addr from update: wanted %v, got %v", addr, u.Addr)
  38. }
  39. // Metadata must be unique
  40. if metaSeen[u.Metadata] {
  41. t.Errorf("Wanted %v to be unique, got %v", u.Metadata, metaSeen)
  42. }
  43. metaSeen[u.Metadata] = true
  44. }
  45. // Test that Next blocks until Close and returns nil.
  46. nextc := make(chan []*naming.Update)
  47. go func() {
  48. next, err := watcher.Next()
  49. if err != nil {
  50. t.Errorf("Next: expected success, got %v", err)
  51. }
  52. nextc <- next
  53. }()
  54. time.Sleep(50 * time.Millisecond) // wait for watcher.Next goroutine
  55. select {
  56. case <-nextc:
  57. t.Fatal("next should not have been called yet")
  58. default:
  59. }
  60. watcher.Close()
  61. select {
  62. case next := <-nextc:
  63. if next != nil {
  64. t.Errorf("Next: expected nil, got %v", next)
  65. }
  66. case <-time.After(50 * time.Millisecond):
  67. t.Error("Next: did not return after 100ms")
  68. }
  69. }