|
| 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 orchestrator |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | + "github.com/uber-go/tally" |
| 23 | + basehook "github.com/uber/submitqueue/api/base/hook" |
| 24 | + hookext "github.com/uber/submitqueue/platform/extension/hook" |
| 25 | + hooknoop "github.com/uber/submitqueue/platform/extension/hook/noop" |
| 26 | + "github.com/uber/submitqueue/platform/pipeline" |
| 27 | + "go.uber.org/zap/zaptest" |
| 28 | +) |
| 29 | + |
| 30 | +type noopHooks struct{} |
| 31 | + |
| 32 | +func (noopHooks) For(*basehook.HookEvent) []hookext.Hook { |
| 33 | + return []hookext.Hook{hooknoop.New()} |
| 34 | +} |
| 35 | + |
| 36 | +func hookStage(t *testing.T) pipeline.Stage[Deps] { |
| 37 | + t.Helper() |
| 38 | + for _, s := range Stages { |
| 39 | + if s.Key == basehook.TopicKeyHook { |
| 40 | + return s |
| 41 | + } |
| 42 | + } |
| 43 | + require.FailNow(t, "no stage is registered for the hook topic key") |
| 44 | + return pipeline.Stage[Deps]{} |
| 45 | +} |
| 46 | + |
| 47 | +func TestHookStage(t *testing.T) { |
| 48 | + deps := func(t *testing.T) Deps { |
| 49 | + return Deps{ |
| 50 | + Logger: zaptest.NewLogger(t).Sugar(), |
| 51 | + Scope: tally.NoopScope, |
| 52 | + Hooks: noopHooks{}, |
| 53 | + } |
| 54 | + } |
| 55 | + stageContext := func(key string) pipeline.StageContext { |
| 56 | + return pipeline.StageContext{ |
| 57 | + TopicKey: basehook.TopicKey(key), |
| 58 | + ConsumerGroup: "orchestrator", |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + t.Run("the hook controller subscribes to the key the engine assigns", func(t *testing.T) { |
| 63 | + controller, err := hookStage(t).New(deps(t), stageContext("hook")) |
| 64 | + require.NoError(t, err) |
| 65 | + assert.Equal(t, basehook.TopicKeyHook, controller.TopicKey()) |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("the dead-letter controller subscribes to the key the engine derives", func(t *testing.T) { |
| 69 | + controller, err := hookStage(t).DLQ(deps(t), stageContext("hook_dlq")) |
| 70 | + require.NoError(t, err) |
| 71 | + assert.Equal(t, basehook.TopicKey("hook_dlq"), controller.TopicKey()) |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("a host that wires no hooks resolver fails to construct", func(t *testing.T) { |
| 75 | + d := deps(t) |
| 76 | + d.Hooks = nil |
| 77 | + _, err := hookStage(t).New(d, stageContext("hook")) |
| 78 | + require.Error(t, err) |
| 79 | + }) |
| 80 | +} |
0 commit comments