|
1 | 1 | package msk |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "io" |
6 | 7 | "log/slog" |
@@ -285,6 +286,82 @@ func TestCollectorCollectContinuesWhenRegionListingFails(t *testing.T) { |
285 | 286 | assert.Len(t, results, 2) |
286 | 287 | } |
287 | 288 |
|
| 289 | +func TestCollectorCollectReturnsContextErrWhenContextCancelled(t *testing.T) { |
| 290 | + ctrl := gomock.NewController(t) |
| 291 | + defer ctrl.Finish() |
| 292 | + |
| 293 | + regionClient := mockclient.NewMockClient(ctrl) |
| 294 | + pricingClient := mockclient.NewMockClient(ctrl) |
| 295 | + |
| 296 | + regionClient.EXPECT(). |
| 297 | + ListMSKClusters(gomock.Any()). |
| 298 | + Return(nil, context.Canceled). |
| 299 | + AnyTimes() |
| 300 | + expectPricingLoad(pricingClient, "us-east-1", "USE1", "0.2100000000", "0.1000000000") |
| 301 | + |
| 302 | + collector, err := New(t.Context(), &Config{ |
| 303 | + Regions: []ec2types.Region{{RegionName: aws.String("us-east-1")}}, |
| 304 | + RegionMap: map[string]client.Client{"us-east-1": regionClient}, |
| 305 | + Client: pricingClient, |
| 306 | + Logger: testLogger(), |
| 307 | + AccountID: "123456789012", |
| 308 | + }) |
| 309 | + require.NoError(t, err) |
| 310 | + |
| 311 | + ctx, cancel := context.WithCancel(context.Background()) |
| 312 | + cancel() |
| 313 | + |
| 314 | + ch := make(chan prometheus.Metric, 10) |
| 315 | + err = collector.Collect(ctx, ch) |
| 316 | + close(ch) |
| 317 | + |
| 318 | + assert.ErrorIs(t, err, context.Canceled) |
| 319 | +} |
| 320 | + |
| 321 | +func TestCollectorCollectContinuesOnContextDeadlineExceeded(t *testing.T) { |
| 322 | + ctrl := gomock.NewController(t) |
| 323 | + defer ctrl.Finish() |
| 324 | + |
| 325 | + failingClient := mockclient.NewMockClient(ctrl) |
| 326 | + healthyClient := mockclient.NewMockClient(ctrl) |
| 327 | + pricingClient := mockclient.NewMockClient(ctrl) |
| 328 | + cluster := newProvisionedCluster( |
| 329 | + "test-cluster", |
| 330 | + "arn:aws:kafka:us-west-2:123456789012:cluster/test-cluster", |
| 331 | + "kafka.m5.large", 3, 100, |
| 332 | + ) |
| 333 | + |
| 334 | + failingClient.EXPECT(). |
| 335 | + ListMSKClusters(gomock.Any()). |
| 336 | + Return(nil, context.DeadlineExceeded). |
| 337 | + Times(1) |
| 338 | + healthyClient.EXPECT(). |
| 339 | + ListMSKClusters(gomock.Any()). |
| 340 | + Return([]msktypes.Cluster{cluster}, nil). |
| 341 | + Times(1) |
| 342 | + expectPricingLoad(pricingClient, "us-east-1", "USE1", "0.2100000000", "0.1000000000") |
| 343 | + expectPricingLoad(pricingClient, "us-west-2", "USW2", "0.2100000000", "0.1000000000") |
| 344 | + |
| 345 | + collector, err := New(t.Context(), &Config{ |
| 346 | + Regions: []ec2types.Region{ |
| 347 | + {RegionName: aws.String("us-east-1")}, |
| 348 | + {RegionName: aws.String("us-west-2")}, |
| 349 | + }, |
| 350 | + RegionMap: map[string]client.Client{ |
| 351 | + "us-east-1": failingClient, |
| 352 | + "us-west-2": healthyClient, |
| 353 | + }, |
| 354 | + Client: pricingClient, |
| 355 | + Logger: testLogger(), |
| 356 | + AccountID: "123456789012", |
| 357 | + }) |
| 358 | + require.NoError(t, err) |
| 359 | + |
| 360 | + results, err := collectMetricResults(t, collector) |
| 361 | + require.NoError(t, err) |
| 362 | + assert.Len(t, results, 2) // metrics from us-west-2 still collected |
| 363 | +} |
| 364 | + |
288 | 365 | func newProvisionedCluster(name, arn, instanceType string, brokerCount, volumeSizeGiB int32) msktypes.Cluster { |
289 | 366 | return msktypes.Cluster{ |
290 | 367 | ClusterArn: aws.String(arn), |
|
0 commit comments