(t *testing.T)
| 68 | } |
| 69 | |
| 70 | func TestPoolCache(t *testing.T) { |
| 71 | buildCount := 0 |
| 72 | factory := PoolAddrFunc(func(addr string) (PoolClient, error) { |
| 73 | if addr == "bad" { |
| 74 | return nil, fmt.Errorf("Fail") |
| 75 | } |
| 76 | buildCount++ |
| 77 | return mockClient{happy: true, status: grpc_health_v1.HealthCheckResponse_SERVING}, nil |
| 78 | }) |
| 79 | |
| 80 | cfg := PoolConfig{ |
| 81 | HealthCheckTimeout: 50 * time.Millisecond, |
| 82 | CheckInterval: 10 * time.Second, |
| 83 | } |
| 84 | |
| 85 | pool := NewPool("test", cfg, nil, factory, nil, log.NewNopLogger()) |
| 86 | require.NoError(t, services.StartAndAwaitRunning(context.Background(), pool)) |
| 87 | defer services.StopAndAwaitTerminated(context.Background(), pool) //nolint:errcheck |
| 88 | |
| 89 | _, err := pool.GetClientFor("1") |
| 90 | require.NoError(t, err) |
| 91 | if buildCount != 1 { |
| 92 | t.Errorf("Did not create client") |
| 93 | } |
| 94 | |
| 95 | _, err = pool.GetClientFor("1") |
| 96 | require.NoError(t, err) |
| 97 | if buildCount != 1 { |
| 98 | t.Errorf("Created client that should have been cached") |
| 99 | } |
| 100 | |
| 101 | _, err = pool.GetClientFor("2") |
| 102 | require.NoError(t, err) |
| 103 | if pool.Count() != 2 { |
| 104 | t.Errorf("Expected Count() = 2, got %d", pool.Count()) |
| 105 | } |
| 106 | |
| 107 | pool.RemoveClientFor("1") |
| 108 | if pool.Count() != 1 { |
| 109 | t.Errorf("Expected Count() = 1, got %d", pool.Count()) |
| 110 | } |
| 111 | |
| 112 | _, err = pool.GetClientFor("1") |
| 113 | require.NoError(t, err) |
| 114 | if buildCount != 3 || pool.Count() != 2 { |
| 115 | t.Errorf("Did not re-create client correctly") |
| 116 | } |
| 117 | |
| 118 | _, err = pool.GetClientFor("bad") |
| 119 | if err == nil { |
| 120 | t.Errorf("Bad create should have thrown an error") |
| 121 | } |
| 122 | if pool.Count() != 2 { |
| 123 | t.Errorf("Bad create should not have been added to cache") |
| 124 | } |
| 125 | |
| 126 | addrs := pool.RegisteredAddresses() |
| 127 | if len(addrs) != pool.Count() { |
nothing calls this directly
no test coverage detected