|
9 | 9 |
|
10 | 10 | "github.com/prometheus/client_golang/prometheus" |
11 | 11 | "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/mock" |
12 | 13 | "github.com/stretchr/testify/require" |
13 | 14 | "google.golang.org/grpc" |
14 | 15 |
|
@@ -115,12 +116,18 @@ func createTestIngesterClient(maxInflightPushRequests int64, currentInflightRequ |
115 | 116 |
|
116 | 117 | type mockIngester struct { |
117 | 118 | IngesterClient |
| 119 | + mock.Mock |
118 | 120 | } |
119 | 121 |
|
120 | 122 | func (m *mockIngester) Push(_ context.Context, _ *cortexpb.WriteRequest, _ ...grpc.CallOption) (*cortexpb.WriteResponse, error) { |
121 | 123 | return &cortexpb.WriteResponse{}, nil |
122 | 124 | } |
123 | 125 |
|
| 126 | +func (m *mockIngester) PushStream(ctx context.Context, opts ...grpc.CallOption) (Ingester_PushStreamClient, error) { |
| 127 | + args := m.Called(ctx, opts) |
| 128 | + return args.Get(0).(Ingester_PushStreamClient), nil |
| 129 | +} |
| 130 | + |
124 | 131 | type mockClientConn struct { |
125 | 132 | ClosableClientConn |
126 | 133 | } |
@@ -227,3 +234,40 @@ func TestClosableHealthAndIngesterClient_Close_WithPendingJobs(t *testing.T) { |
227 | 234 | assert.True(t, job1Cancelled, "job1 should have been cancelled") |
228 | 235 | assert.True(t, job2Cancelled, "job2 should have been cancelled") |
229 | 236 | } |
| 237 | + |
| 238 | +type mockClientStream struct { |
| 239 | + mock.Mock |
| 240 | + grpc.ClientStream |
| 241 | +} |
| 242 | + |
| 243 | +func (m *mockClientStream) Send(msg *cortexpb.StreamWriteRequest) error { |
| 244 | + args := m.Called(msg) |
| 245 | + return args.Error(0) |
| 246 | +} |
| 247 | + |
| 248 | +func (m *mockClientStream) Recv() (*cortexpb.WriteResponse, error) { |
| 249 | + return &cortexpb.WriteResponse{}, nil |
| 250 | +} |
| 251 | + |
| 252 | +func TestClosableHealthAndIngesterClient_ShouldNotPanicWhenClose(t *testing.T) { |
| 253 | + ctx, cancel := context.WithCancel(context.Background()) |
| 254 | + streamChan := make(chan *streamWriteJob) |
| 255 | + |
| 256 | + mockIngester := &mockIngester{} |
| 257 | + mockStream := &mockClientStream{} |
| 258 | + mockIngester.On("PushStream", mock.Anything, mock.Anything).Return(mockStream, nil).Once() |
| 259 | + |
| 260 | + client := &closableHealthAndIngesterClient{ |
| 261 | + IngesterClient: mockIngester, |
| 262 | + conn: &mockClientConn{}, |
| 263 | + addr: "test-addr", |
| 264 | + inflightPushRequests: prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{"ingester"}), |
| 265 | + streamCtx: ctx, |
| 266 | + streamCancel: cancel, |
| 267 | + streamPushChan: streamChan, |
| 268 | + } |
| 269 | + require.NoError(t, client.worker(context.Background())) |
| 270 | + require.NoError(t, client.Close()) |
| 271 | + |
| 272 | + time.Sleep(100 * time.Millisecond) |
| 273 | +} |
0 commit comments