Skip to content

Commit

Permalink
Improve inline testing example
Browse files Browse the repository at this point in the history
**Problem**

Previous example depended on the manager having been started via `Run`
or `RunWithContext`. This kicks off goroutines that result in data races
and flaky builds.

**Solution**

Manually add the `mgr.Pool` in inline test example to avoid requiring
a call to `mgr.Run*` and the resulting data races.
  • Loading branch information
Adam Steel committed Jan 22, 2024
1 parent a1dfffe commit 25cf0fb
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,25 @@ func Push(mgr worker.Manager, job *faktory.Job) error {
}

func syntheticPush(mgr worker.Manager, job *faktory.Job) error {
if mgr.IsRegistered(job.Type) {
return mgr.Dispatch(job)
if !mgr.IsRegistered(job.Type) {
return fmt.Errorf("failed to execute job type %s inline, job not registered", job.Type)
}

return fmt.Errorf("inline job execution failed, unregistered job type %s", job.Type)
// Manually defining the Pool avoids using mgr.Run/mgr.RunWithContext, which can trigger data races
if mgr.Pool == nil {
pool, err := faktory.NewPool(2)
if err != nil {
return fmt.Errorf("couldn't create Faktory connection pool in synthetic push: %w", err)
}
mgr.Pool = pool
}

err := mgr.Dispatch(job)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("job was immediately executed but failed. Job type %s, with args %+v", job.Type, job.Args))
}

return nil
}

func realPush(job *faktory.Job) error {
Expand Down

0 comments on commit 25cf0fb

Please sign in to comment.