(t *testing.T)
| 481 | } |
| 482 | |
| 483 | func TestWorker_WorkJob(t *testing.T) { |
| 484 | t.Parallel() |
| 485 | |
| 486 | ctx := context.Background() |
| 487 | |
| 488 | type testBundle struct { |
| 489 | client *river.Client[pgx.Tx] |
| 490 | config *river.Config |
| 491 | driver *riverpgxv5.Driver |
| 492 | tx pgx.Tx |
| 493 | workFunc func(ctx context.Context, job *river.Job[testArgs]) error |
| 494 | } |
| 495 | |
| 496 | setup := func(t *testing.T) (*Worker[testArgs, pgx.Tx], *testBundle) { |
| 497 | t.Helper() |
| 498 | |
| 499 | var ( |
| 500 | config = &river.Config{ID: "rivertest-workjob"} |
| 501 | driver = riverpgxv5.New(nil) |
| 502 | ) |
| 503 | |
| 504 | client, err := river.NewClient(driver, config) |
| 505 | require.NoError(t, err) |
| 506 | |
| 507 | bundle := &testBundle{ |
| 508 | client: client, |
| 509 | config: config, |
| 510 | driver: driver, |
| 511 | tx: riverdbtest.TestTxPgx(ctx, t), |
| 512 | workFunc: func(ctx context.Context, job *river.Job[testArgs]) error { return nil }, |
| 513 | } |
| 514 | |
| 515 | worker := river.WorkFunc(func(ctx context.Context, job *river.Job[testArgs]) error { |
| 516 | return bundle.workFunc(ctx, job) |
| 517 | }) |
| 518 | |
| 519 | return NewWorker(t, driver, config, worker), bundle |
| 520 | } |
| 521 | |
| 522 | t.Run("Success", func(t *testing.T) { |
| 523 | t.Parallel() |
| 524 | |
| 525 | testWorker, bundle := setup(t) |
| 526 | |
| 527 | bundle.workFunc = func(ctx context.Context, job *river.Job[testArgs]) error { |
| 528 | require.WithinDuration(t, time.Now(), *job.AttemptedAt, 5*time.Second) |
| 529 | require.Equal(t, []string{"rivertest-workjob"}, job.AttemptedBy) |
| 530 | require.Equal(t, rivertype.JobStateRunning, job.State) |
| 531 | return nil |
| 532 | } |
| 533 | |
| 534 | insertRes, err := bundle.client.InsertTx(ctx, bundle.tx, testArgs{}, nil) |
| 535 | require.NoError(t, err) |
| 536 | |
| 537 | res, err := testWorker.WorkJob(ctx, t, bundle.tx, insertRes.Job) |
| 538 | require.NoError(t, err) |
| 539 | require.Equal(t, river.EventKindJobCompleted, res.EventKind) |
| 540 | }) |
nothing calls this directly
no test coverage detected
searching dependent graphs…