MCPcopy Index your code
hub / github.com/riverqueue/river / TestMiddleware

Function TestMiddleware

riverlog/river_log_test.go:254–562  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

252}
253
254func TestMiddleware(t *testing.T) {
255 t.Parallel()
256
257 ctx := context.Background()
258
259 type testBundle struct {
260 clientConfig *river.Config
261 driver *riverpgxv5.Driver
262 middleware *Middleware
263 tx pgx.Tx
264 }
265
266 setup := func(t *testing.T, config *MiddlewareConfig) (*rivertest.Worker[loggingArgs, pgx.Tx], *testBundle) {
267 t.Helper()
268
269 var (
270 driver = riverpgxv5.New(nil)
271 middleware = NewMiddleware(func(w io.Writer) slog.Handler {
272 return slog.NewTextHandler(w, &slog.HandlerOptions{ReplaceAttr: slogutil.NoLevelTime})
273 }, config)
274 clientConfig = &river.Config{
275 Middleware: []rivertype.Middleware{middleware},
276 }
277 tx = riverdbtest.TestTxPgx(ctx, t)
278 worker = &loggingWorker{}
279 )
280
281 return rivertest.NewWorker(t, driver, clientConfig, worker), &testBundle{
282 clientConfig: clientConfig,
283 driver: driver,
284 middleware: middleware,
285 tx: tx,
286 }
287 }
288
289 t.Run("Success", func(t *testing.T) {
290 t.Parallel()
291
292 testWorker, bundle := setup(t, nil)
293
294 workRes, err := testWorker.Work(ctx, t, bundle.tx, loggingArgs{Message: "Logged from worker"}, nil)
295 require.NoError(t, err)
296
297 var metadataWithLog metadataWithLog
298 require.NoError(t, json.Unmarshal(workRes.Job.Metadata, &metadataWithLog))
299
300 require.Equal(t, []logAttempt{
301 {
302 Attempt: 1,
303 Log: `msg="Logged from worker"` + "\n",
304 },
305 },
306 metadataWithLog.RiverLog,
307 )
308 })
309
310 t.Run("MultipleAttempts", func(t *testing.T) {
311 t.Parallel()

Callers

nothing calls this directly

Calls 12

WorkMethod · 0.95
NewFunction · 0.92
TestTxPgxFunction · 0.92
NewWorkerFunction · 0.92
WorkFuncFunction · 0.92
NewMiddlewareFunction · 0.85
WorkJobMethod · 0.80
HelperMethod · 0.65
RunMethod · 0.65
JobUpdateFullMethod · 0.65
UnwrapExecutorMethod · 0.65
ValueMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…