@@ -60,6 +60,15 @@ func (r *observerRecorder) Observe(_ context.Context, event Event) {
6060 r .events = append (r .events , event )
6161}
6262
63+ type observerContextRecorder struct {
64+ values []string
65+ }
66+
67+ func (r * observerContextRecorder ) Observe (ctx context.Context , _ Event ) {
68+ value , _ := ctx .Value ("decorated" ).(string )
69+ r .values = append (r .values , value )
70+ }
71+
6372func TestChannelObserver_DropIfFullAndBlockingSend (t * testing.T ) {
6473 ch := make (chan Event , 1 )
6574 ch <- Event {Kind : EventEnqueueAccepted }
@@ -116,7 +125,7 @@ func TestObservedQueue_DispatchClassifiesErrors(t *testing.T) {
116125func TestWrapObservedHandler_EmitsRetriedAndArchived (t * testing.T ) {
117126 t .Run ("retry path" , func (t * testing.T ) {
118127 recorder := & observerRecorder {}
119- h := wrapObservedHandler (recorder , DriverSync , "" , "job:retry" , func (context.Context , Job ) error {
128+ h := wrapObservedHandler (recorder , DriverSync , "" , "job:retry" , nil , func (context.Context , Job ) error {
120129 return errors .New ("boom" )
121130 })
122131
@@ -134,7 +143,7 @@ func TestWrapObservedHandler_EmitsRetriedAndArchived(t *testing.T) {
134143
135144 t .Run ("archive path" , func (t * testing.T ) {
136145 recorder := & observerRecorder {}
137- h := wrapObservedHandler (recorder , DriverSync , "" , "job:archive" , func (context.Context , Job ) error {
146+ h := wrapObservedHandler (recorder , DriverSync , "" , "job:archive" , nil , func (context.Context , Job ) error {
138147 return errors .New ("boom" )
139148 })
140149
@@ -151,6 +160,30 @@ func TestWrapObservedHandler_EmitsRetriedAndArchived(t *testing.T) {
151160 })
152161}
153162
163+ func TestWrapObservedHandler_DecoratesObserverContext (t * testing.T ) {
164+ recorder := & observerContextRecorder {}
165+ h := wrapObservedHandler (recorder , DriverSync , "" , "job:decorated" , func (ctx context.Context ) context.Context {
166+ return context .WithValue (ctx , "decorated" , "jobs" )
167+ }, func (ctx context.Context , _ Job ) error {
168+ if got , _ := ctx .Value ("decorated" ).(string ); got != "jobs" {
169+ t .Fatalf ("handler ctx value = %q, want jobs" , got )
170+ }
171+ return nil
172+ })
173+
174+ if err := h (context .Background (), NewJob ("job:decorated" ).OnQueue ("default" )); err != nil {
175+ t .Fatalf ("wrapped handler returned error: %v" , err )
176+ }
177+ if len (recorder .values ) != 2 {
178+ t .Fatalf ("expected 2 observed events, got %d" , len (recorder .values ))
179+ }
180+ for i , got := range recorder .values {
181+ if got != "jobs" {
182+ t .Fatalf ("observer ctx value at index %d = %q, want jobs" , i , got )
183+ }
184+ }
185+ }
186+
154187func TestObservedQueue_WrapperMethods (t * testing.T ) {
155188 recorder := & observerRecorder {}
156189 inner := & queueBackendStub {
0 commit comments