MCPcopy Index your code
hub / github.com/coder/coder / TestPublisherMissingEvents

Function TestPublisherMissingEvents

enterprise/coderd/usage/publisher_test.go:354–426  ·  view source on GitHub ↗

TestPublisherMissingEvents tests that the publisher notices events that are not returned by the Tallyman server and marks them as temporarily rejected.

(t *testing.T)

Source from the content-addressed store, hash-verified

352// TestPublisherMissingEvents tests that the publisher notices events that are
353// not returned by the Tallyman server and marks them as temporarily rejected.
354func 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 }()

Callers

nothing calls this directly

Calls 15

EXPECTMethod · 0.95
ContextFunction · 0.92
NewMockStoreFunction · 0.92
NewTallymanPublisherFunction · 0.92
PublisherWithClockFunction · 0.92
PublisherWithIngestURLFunction · 0.92
RequireSendFunction · 0.92
RequireReceiveFunction · 0.92
configureMockDeploymentFunction · 0.85
fakeServerFunction · 0.85
tallymanHandlerFunction · 0.85
jsoninateFunction · 0.85

Tested by

no test coverage detected