|
| 1 | +// Copyright (c) 2026 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package composite |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + basehook "github.com/uber/submitqueue/api/base/hook" |
| 25 | + "github.com/uber/submitqueue/platform/extension/hook" |
| 26 | +) |
| 27 | + |
| 28 | +// recordingHook records the events it saw and fails with a fixed error. |
| 29 | +type recordingHook struct { |
| 30 | + name string |
| 31 | + err error |
| 32 | + seen []string |
| 33 | +} |
| 34 | + |
| 35 | +var _ hook.Hook = (*recordingHook)(nil) |
| 36 | + |
| 37 | +func (h *recordingHook) Handle(_ context.Context, event *basehook.HookEvent) error { |
| 38 | + h.seen = append(h.seen, event.GetId()) |
| 39 | + return h.err |
| 40 | +} |
| 41 | + |
| 42 | +func (h *recordingHook) Name() string { return h.name } |
| 43 | + |
| 44 | +func event() *basehook.HookEvent { |
| 45 | + return &basehook.HookEvent{Id: "submitqueue/batch.failed/batch-778/4", Source: "submitqueue", Type: "batch.failed"} |
| 46 | +} |
| 47 | + |
| 48 | +func TestHandle(t *testing.T) { |
| 49 | + t.Run("no children", func(t *testing.T) { |
| 50 | + require.NoError(t, New().Handle(context.Background(), event())) |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("every child sees the event", func(t *testing.T) { |
| 54 | + first := &recordingHook{name: "first"} |
| 55 | + second := &recordingHook{name: "second"} |
| 56 | + |
| 57 | + require.NoError(t, New(first, second).Handle(context.Background(), event())) |
| 58 | + assert.Equal(t, []string{event().GetId()}, first.seen) |
| 59 | + assert.Equal(t, []string{event().GetId()}, second.seen) |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("a failing child does not stop the others", func(t *testing.T) { |
| 63 | + boom := errors.New("boom") |
| 64 | + failing := &recordingHook{name: "failing", err: boom} |
| 65 | + healthy := &recordingHook{name: "healthy"} |
| 66 | + |
| 67 | + err := New(failing, healthy).Handle(context.Background(), event()) |
| 68 | + |
| 69 | + require.Error(t, err) |
| 70 | + assert.ErrorIs(t, err, boom) |
| 71 | + assert.Equal(t, []string{event().GetId()}, healthy.seen, "the healthy child runs after the failing one") |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("every failure survives the join", func(t *testing.T) { |
| 75 | + first := errors.New("first failure") |
| 76 | + second := errors.New("second failure") |
| 77 | + |
| 78 | + err := New( |
| 79 | + &recordingHook{name: "first", err: first}, |
| 80 | + &recordingHook{name: "second", err: second}, |
| 81 | + ).Handle(context.Background(), event()) |
| 82 | + |
| 83 | + require.Error(t, err) |
| 84 | + assert.ErrorIs(t, err, first) |
| 85 | + assert.ErrorIs(t, err, second) |
| 86 | + }) |
| 87 | +} |
0 commit comments