(t *testing.T)
| 2675 | } |
| 2676 | |
| 2677 | func Test_Client_JobContextInheritsFromProvidedContext(t *testing.T) { |
| 2678 | t.Parallel() |
| 2679 | |
| 2680 | deadline := time.Now().Add(2 * time.Minute) |
| 2681 | |
| 2682 | jobCtxCh := make(chan context.Context) |
| 2683 | doneCh := make(chan struct{}) |
| 2684 | close(doneCh) |
| 2685 | |
| 2686 | callbackFunc := func(ctx context.Context, job *Job[callbackWithCustomTimeoutArgs]) error { |
| 2687 | // indicate the job has started, unless context is already done: |
| 2688 | select { |
| 2689 | case <-ctx.Done(): |
| 2690 | return ctx.Err() |
| 2691 | case jobCtxCh <- ctx: |
| 2692 | } |
| 2693 | return nil |
| 2694 | } |
| 2695 | config := newTestConfig(t, "") |
| 2696 | AddWorker(config.Workers, &callbackWorkerWithCustomTimeout{fn: callbackFunc}) |
| 2697 | |
| 2698 | // Set a deadline and a value on the context for the client so we can verify |
| 2699 | // it's propagated through to the job: |
| 2700 | ctx, cancel := context.WithDeadline(context.Background(), deadline) |
| 2701 | t.Cleanup(cancel) |
| 2702 | |
| 2703 | type customContextKey string |
| 2704 | ctx = context.WithValue(ctx, customContextKey("BestGoPostgresQueue"), "River") |
| 2705 | client := runNewTestClient(ctx, t, config) |
| 2706 | |
| 2707 | insertCtx, insertCancel := context.WithTimeout(ctx, time.Second) |
| 2708 | t.Cleanup(insertCancel) |
| 2709 | |
| 2710 | // enqueue job: |
| 2711 | _, err := client.Insert(insertCtx, callbackWithCustomTimeoutArgs{TimeoutValue: 5 * time.Minute}, nil) |
| 2712 | require.NoError(t, err) |
| 2713 | |
| 2714 | var jobCtx context.Context |
| 2715 | select { |
| 2716 | case jobCtx = <-jobCtxCh: |
| 2717 | case <-time.After(5 * time.Second): |
| 2718 | t.Fatal("timed out waiting for job to start") |
| 2719 | } |
| 2720 | |
| 2721 | require.Equal(t, "River", jobCtx.Value(customContextKey("BestGoPostgresQueue")), "job should persist the context value from the client context") |
| 2722 | jobDeadline, ok := jobCtx.Deadline() |
| 2723 | require.True(t, ok, "job should have a deadline") |
| 2724 | require.Equal(t, deadline, jobDeadline, "job should have the same deadline as the client context (shorter than the job's timeout)") |
| 2725 | } |
| 2726 | |
| 2727 | func Test_Client_ClientFromContext(t *testing.T) { |
| 2728 | t.Parallel() |
nothing calls this directly
no test coverage detected
searching dependent graphs…