Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

83 righe
2.0 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. "google.golang.org/grpc/naming"
  16. "testing"
  17. "time"
  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. closedc := make(chan bool)
  48. go func() {
  49. next, err := watcher.Next()
  50. if err != nil {
  51. t.Errorf("Next: expected success, got %v", err)
  52. }
  53. nextc <- next
  54. }()
  55. go func() {
  56. time.Sleep(50 * time.Millisecond)
  57. watcher.Close()
  58. close(closedc)
  59. }()
  60. select {
  61. case <-nextc:
  62. t.Fatalf("Next: second invocation didn't block, returned before Close()")
  63. case <-closedc:
  64. // OK, watcher was closed before Next() returned.
  65. }
  66. select {
  67. case next := <-nextc:
  68. if next != nil {
  69. t.Errorf("Next: expected nil, got %v", next)
  70. }
  71. case <-time.After(100 * time.Millisecond):
  72. t.Error("Next: did not return after 100ms")
  73. }
  74. }