{"record":{"id":"43b134ed82ce429c","repo":"hibiken/asynq","slug":"testutil-redis-is-down","errorCode":null,"errorMessage":"testutil: redis is down","messagePattern":"testutil: redis is down","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/testbroker/testbroker.go","lineNumber":18,"sourceCode":"// Copyright 2020 Kentaro Hibino. All rights reserved.\n// Use of this source code is governed by a MIT license\n// that can be found in the LICENSE file.\n\n// Package testbroker exports a broker implementation that should be used in package testing.\npackage testbroker\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"sync\"\n\t\"time\"\n\n\t\"github.com/hibiken/asynq/internal/base\"\n\t\"github.com/redis/go-redis/v9\"\n)\n\nvar errRedisDown = errors.New(\"testutil: redis is down\")\n\n// TestBroker is a broker implementation which enables\n// to simulate Redis failure in tests.\ntype TestBroker struct {\n\tmu       sync.Mutex\n\tsleeping bool\n\n\t// real broker\n\treal base.Broker\n}\n\n// Make sure TestBroker implements Broker interface at compile time.\nvar _ base.Broker = (*TestBroker)(nil)\n\nfunc NewTestBroker(b base.Broker) *TestBroker {\n\treturn &TestBroker{real: b}\n}\n","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/hibiken/asynq/blob/d135f1439bee74e989b7f9b41ecd542cc87f024a/internal/testbroker/testbroker.go#L1-L36","documentation":"errRedisDown is a sentinel error in the internal test broker (TestBroker) that simulates Redis being unavailable. TestBroker wraps a real broker; when its 'sleeping' flag is set (simulated outage, typically via Sleep()/WakeUp() in tests), every broker method including Enqueue, EnqueueUnique, BatchEnqueue, Dequeue, Done, and MarkAsComplete returns this error instead of touching Redis.","triggerScenarios":"Calling any broker method on a TestBroker while tb.sleeping is true — i.e. after test code has put the broker into its simulated-down state — or writing tests that call TestBroker.Sleep() and then exercise client/server paths that hit the broker.","commonSituations":"This error appears only in test environments: unit tests that simulate Redis outages to verify retry/failure handling, or a test that forgot to call the broker's wake-up/restore method after putting it to sleep, causing subsequent operations in the same test to fail unexpectedly.","solutions":["If you hit this in your own tests, call the TestBroker method that restores availability (WakeUp) after asserting the down-state behavior.","If this error is unexpected, check for a leaked Sleep() from a prior test step or missing cleanup (e.g. defer WakeUp).","In code under test, handle the broker error as a transient outage: rely on asynq's retry/delay mechanisms or surface it to the test as the expected failure."],"exampleFix":"// before\ntb.Sleep()\nclient.Enqueue(task) // returns \"testutil: redis is down\"\n// after\ntb.Sleep()\n// ... assertions on failure behavior ...\ntb.WakeUp()\nerr := client.Enqueue(task) // succeeds again","handlingStrategy":"try-catch","validationCode":"if tb, ok := broker.(*testbroker.TestBroker); ok && tb.Sleeping() { /* restore before calling broker methods */ }","typeGuard":"func isRedisDown(err error) bool { return errors.Is(err, errRedisDown) }","tryCatchPattern":"if err := broker.Enqueue(ctx, msg); err != nil {\n    if errors.Is(err, errRedisDown) {\n        t.Fatal(\"broker used while simulated outage active — call WakeUp first\")\n    }\n    t.Fatalf(\"enqueue failed: %v\", err)\n}","preventionTips":["Always defer WakeUp when calling Sleep in tests.","Keep simulated-outage tests self-contained so sleeping state cannot leak to other tests.","Assert on the specific sentinel error, not generic error equality."],"tags":["go","test","redis","simulated-outage","testbroker"],"backgroundTag":"connection-refused","analyzedSha":"d135f1439bee74e989b7f9b41ecd542cc87f024a","analyzedAt":"2026-09-07T19:02:34.660Z","contentChangedAt":"2026-09-07T19:02:34.660Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}