Skip to content

Commit 9eaea33

Browse files
authored
Guarantee output order in batch insertion example (#1206)
Aims to fix an intermittently failing example wherein we can print an "Inserted ..." message after the work messages coming from each inserted job. https://github.com/riverqueue/river/actions/runs/24310710305/job/70979718727 --- FAIL: Example_batchInsert (0.09s) got: Worked a job Worked a job Worked a job Worked a job Worked a job Inserted 5 jobs want: Inserted 5 jobs Worked a job Worked a job Worked a job Worked a job Worked a job FAIL FAIL github.com/riverqueue/river 26.753s The fix is pretty straightforward in making sure we do the inserts (and therefore print the right message) before starting the client and introducing the possibility that jobs can be worked.
1 parent 27a4c30 commit 9eaea33

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

example_batch_insert_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ func Example_batchInsert() {
6262
subscribeChan, subscribeCancel := riverClient.Subscribe(river.EventKindJobCompleted)
6363
defer subscribeCancel()
6464

65-
if err := riverClient.Start(ctx); err != nil {
66-
panic(err)
67-
}
68-
6965
results, err := riverClient.InsertMany(ctx, []river.InsertManyParams{
7066
{Args: BatchInsertArgs{}},
7167
{Args: BatchInsertArgs{}},
@@ -78,6 +74,12 @@ func Example_batchInsert() {
7874
}
7975
fmt.Printf("Inserted %d jobs\n", len(results))
8076

77+
// Start the client after inserting and printing so that the "Inserted"
78+
// message is guaranteed to appear before any "Worked" messages.
79+
if err := riverClient.Start(ctx); err != nil {
80+
panic(err)
81+
}
82+
8183
// Wait for jobs to complete. Only needed for purposes of the example test.
8284
riversharedtest.WaitOrTimeoutN(testutil.PanicTB(), subscribeChan, 5)
8385

0 commit comments

Comments
 (0)