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