Skip to content

Drop callback args in favor of single, uniform test convention#856

Merged
brandur merged 1 commit into
masterfrom
brandur-drop-callback-args
Apr 28, 2025
Merged

Drop callback args in favor of single, uniform test convention#856
brandur merged 1 commit into
masterfrom
brandur-drop-callback-args

Conversation

@brandur
Copy link
Copy Markdown
Contributor

@brandur brandur commented Apr 26, 2025

This one's largely a cosmetic change. We remove callback args/worker in
favor of using test case local args using JobArgsReflectKind and a
standard invocation to AddWorker:

type JobArgs struct {
    JobArgsReflectKind[JobArgs]
}

AddWorker(config.Workers, WorkFunc(func(ctx context.Context, job *Job[JobArgs]) error {
    return JobCancel(errors.New("oops"))
}))

It's a little more verbose, but has the advantage of using only normal
River primitives, meaning that one has to be familiar with fewer test
helpers to understand what's going on.

Besides that and convention, another small benefit is that it introduces
a little more type safety and makes job args impossible to accidentally
cross reference between test cases. Previously, a test could insert a
callback args while intending to use a callback for them registered in a
different test case. With the new approach, it's very obvious when
JobArgs is either forgotten (a compilation error), or when there's a
missing AddWorker (because that's supposed to live close to the
definition of JobArgs).

Comment thread periodic_job_test.go
nil,
)

return newPeriodicJobBundle(newTestConfig(t, nil), periodicJobEnqueuer), &testBundle{}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made schema the second argument to newTestConfig instead, but honestly could see it with or without. It does save one LOC for test case where schemas are used (which is a lot of them), but it tends to be empty for test cases using the runNewTestClient helper, which injects its own schema.

@brandur brandur requested a review from bgentry April 26, 2025 07:52
Comment thread plugin_test.go Outdated
Comment on lines +105 to +107
)

var (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing a few cases where you have two consecutive var blocks—what's the strategy there? It's more verbose so I'm wanting to understand when this would be beneficial for readability or grouping.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, I think I was just thinking that since dbPool / driver / schema / config are commonly found in a single group, it might make sense for the other two vars to get their own.

I don't feel strongly about it though. I just changed it back to a single var block.

@brandur brandur force-pushed the brandur-drop-callback-args branch from d8e15ce to d6caf1f Compare April 28, 2025 06:52
This one's largely a cosmetic change. We remove callback args/worker in
favor of using test case local args using `JobArgsReflectKind` and a
standard invocation to `AddWorker`:

    type JobArgs struct {
        JobArgsReflectKind[JobArgs]
    }

    AddWorker(config.Workers, WorkFunc(func(ctx context.Context, job *Job[JobArgs]) error {
        return JobCancel(errors.New("oops"))
    }))

It's a little more verbose, but has the advantage of using only normal
River primitives, meaning that one has to be familiar with fewer test
helpers to understand what's going on.

Besides that and convention, another small benefit is that it introduces
a little more type safety and makes job args impossible to accidentally
cross reference between test cases. Previously, a test could insert a
callback args while intending to use a callback for them registered in a
different test case. With the new approach, it's very obvious when
`JobArgs` is either forgotten (a compilation error), or when there's a
missing `AddWorker` (because that's supposed to live close to the
definition of `JobArgs`).
@brandur brandur force-pushed the brandur-drop-callback-args branch from d6caf1f to f5db3fb Compare April 28, 2025 06:54
@brandur
Copy link
Copy Markdown
Contributor Author

brandur commented Apr 28, 2025

Thanks!

@brandur brandur merged commit 71e2657 into master Apr 28, 2025
10 checks passed
@brandur brandur deleted the brandur-drop-callback-args branch April 28, 2025 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants