TestPublisherMissingEvents tests that the publisher notices events that are not returned by the Tallyman server and marks them as temporarily rejected.
(t *testing.T)
| 352 | // TestPublisherMissingEvents tests that the publisher notices events that are |
| 353 | // not returned by the Tallyman server and marks them as temporarily rejected. |
| 354 | func TestPublisherMissingEvents(t *testing.T) { |
| 355 | t.Parallel() |
| 356 | ctx := testutil.Context(t, testutil.WaitLong) |
| 357 | log := slogtest.Make(t, nil) |
| 358 | ctrl := gomock.NewController(t) |
| 359 | db := dbmock.NewMockStore(ctrl) |
| 360 | deploymentID, licenseJWT := configureMockDeployment(t, db) |
| 361 | clock := quartz.NewMock(t) |
| 362 | now := time.Now() |
| 363 | clock.Set(now) |
| 364 | |
| 365 | var calls int |
| 366 | ingestURL := fakeServer(t, tallymanHandler(t, deploymentID.String(), licenseJWT, func(req usagetypes.TallymanV1IngestRequest) any { |
| 367 | calls++ |
| 368 | return usagetypes.TallymanV1IngestResponse{ |
| 369 | AcceptedEvents: []usagetypes.TallymanV1IngestAcceptedEvent{}, |
| 370 | RejectedEvents: []usagetypes.TallymanV1IngestRejectedEvent{}, |
| 371 | } |
| 372 | })) |
| 373 | |
| 374 | publisher := usage.NewTallymanPublisher(ctx, log, db, coderdenttest.Keys, |
| 375 | usage.PublisherWithClock(clock), |
| 376 | usage.PublisherWithIngestURL(ingestURL), |
| 377 | ) |
| 378 | |
| 379 | // Expect the publisher to call SelectUsageEventsForPublishing, followed by |
| 380 | // UpdateUsageEventsPostPublish. |
| 381 | events := []database.UsageEvent{ |
| 382 | { |
| 383 | ID: uuid.New().String(), |
| 384 | EventType: string(usagetypes.UsageEventTypeDCManagedAgentsV1), |
| 385 | EventData: []byte(jsoninate(t, usagetypes.DCManagedAgentsV1{ |
| 386 | Count: 1, |
| 387 | })), |
| 388 | CreatedAt: now, |
| 389 | PublishedAt: sql.NullTime{}, |
| 390 | PublishStartedAt: sql.NullTime{}, |
| 391 | FailureMessage: sql.NullString{}, |
| 392 | }, |
| 393 | } |
| 394 | db.EXPECT().SelectUsageEventsForPublishing(gomock.Any(), gomock.Any()).Return(events, nil).Times(1) |
| 395 | db.EXPECT().UpdateUsageEventsPostPublish(gomock.Any(), gomock.Any()).DoAndReturn( |
| 396 | func(ctx context.Context, params database.UpdateUsageEventsPostPublishParams) error { |
| 397 | assert.Equal(t, []string{events[0].ID}, params.IDs) |
| 398 | assert.Equal(t, []string{"tallyman did not include the event in the response"}, params.FailureMessages) |
| 399 | assert.Equal(t, []bool{false}, params.SetPublishedAts) |
| 400 | return nil |
| 401 | }, |
| 402 | ).Times(1) |
| 403 | |
| 404 | // Start the publisher with a trap. |
| 405 | tickerTrap := clock.Trap().NewTicker() |
| 406 | defer tickerTrap.Close() |
| 407 | startErr := make(chan error) |
| 408 | go func() { |
| 409 | err := publisher.Start() |
| 410 | testutil.RequireSend(ctx, t, startErr, err) |
| 411 | }() |
nothing calls this directly
no test coverage detected