|
| 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 sqsim |
| 16 | + |
| 17 | +import ( |
| 18 | + "time" |
| 19 | + |
| 20 | + "github.com/uber/submitqueue/sqsim/entity" |
| 21 | +) |
| 22 | + |
| 23 | +// BuildRunnerBehaviorBuilder constructs BuildRunner behavior. |
| 24 | +type BuildRunnerBehaviorBuilder struct { |
| 25 | + triggers []entity.Invocation[entity.BuildTriggerOutcome] |
| 26 | +} |
| 27 | + |
| 28 | +// NewBuildRunnerBehavior returns an empty BuildRunner behavior builder. |
| 29 | +func NewBuildRunnerBehavior() *BuildRunnerBehaviorBuilder { |
| 30 | + return &BuildRunnerBehaviorBuilder{} |
| 31 | +} |
| 32 | + |
| 33 | +// Trigger appends Trigger invocations. |
| 34 | +func (b *BuildRunnerBehaviorBuilder) Trigger(invocations ...entity.Invocation[entity.BuildTriggerOutcome]) *BuildRunnerBehaviorBuilder { |
| 35 | + b.triggers = append(b.triggers, invocations...) |
| 36 | + return b |
| 37 | +} |
| 38 | + |
| 39 | +// StatusFaultOnCall adds a Status fault to the most recent successful Trigger outcome. |
| 40 | +func (b *BuildRunnerBehaviorBuilder) StatusFaultOnCall(call int, fault Fault) *BuildRunnerBehaviorBuilder { |
| 41 | + if len(b.triggers) == 0 { |
| 42 | + return b |
| 43 | + } |
| 44 | + last := &b.triggers[len(b.triggers)-1] |
| 45 | + last.Outcome.Build.StatusFaults = append(last.Outcome.Build.StatusFaults, entity.FaultOnCall{ |
| 46 | + Call: call, |
| 47 | + Fault: fault, |
| 48 | + }) |
| 49 | + return b |
| 50 | +} |
| 51 | + |
| 52 | +func (b *BuildRunnerBehaviorBuilder) build() (entity.BuildRunnerBehavior, error) { |
| 53 | + triggers := append([]entity.Invocation[entity.BuildTriggerOutcome](nil), b.triggers...) |
| 54 | + for i := range triggers { |
| 55 | + triggers[i].Outcome.Build.Timeline = append([]entity.BuildStatusPoint(nil), triggers[i].Outcome.Build.Timeline...) |
| 56 | + triggers[i].Outcome.Build.StatusFaults = append([]entity.FaultOnCall(nil), triggers[i].Outcome.Build.StatusFaults...) |
| 57 | + } |
| 58 | + return entity.BuildRunnerBehavior{Triggers: triggers}, nil |
| 59 | +} |
| 60 | + |
| 61 | +// StatusAt returns a build status timeline point. |
| 62 | +func StatusAt(after time.Duration, status BuildStatus) BuildStatusPoint { |
| 63 | + return entity.BuildStatusPoint{AfterMs: after.Milliseconds(), Status: status} |
| 64 | +} |
| 65 | + |
| 66 | +// BuildCreated returns a successful Trigger invocation with a build timeline. |
| 67 | +func BuildCreated(timeline ...BuildStatusPoint) entity.Invocation[entity.BuildTriggerOutcome] { |
| 68 | + return entity.Invocation[entity.BuildTriggerOutcome]{ |
| 69 | + Outcome: entity.BuildTriggerOutcome{ |
| 70 | + Build: entity.BuildExecution{Timeline: append([]entity.BuildStatusPoint(nil), timeline...)}, |
| 71 | + }, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// BuildCreatedWithFault returns a Trigger invocation that creates a build and then faults. |
| 76 | +func BuildCreatedWithFault(fault Fault, timeline ...BuildStatusPoint) entity.Invocation[entity.BuildTriggerOutcome] { |
| 77 | + invocation := BuildCreated(timeline...) |
| 78 | + invocation.Fault = fault |
| 79 | + return invocation |
| 80 | +} |
| 81 | + |
| 82 | +// BuildTriggerFault returns a Trigger invocation that fails without creating a build. |
| 83 | +func BuildTriggerFault(fault Fault) entity.Invocation[entity.BuildTriggerOutcome] { |
| 84 | + return entity.Invocation[entity.BuildTriggerOutcome]{Fault: fault} |
| 85 | +} |
| 86 | + |
| 87 | +// SuccessfulBuildRunner returns a build behavior that succeeds immediately. |
| 88 | +func SuccessfulBuildRunner() *BuildRunnerBehaviorBuilder { |
| 89 | + return NewBuildRunnerBehavior().Trigger(BuildCreated(StatusAt(0, BuildSucceeded))) |
| 90 | +} |
| 91 | + |
| 92 | +// BuildSucceededAfter returns a build behavior that succeeds after the given duration. |
| 93 | +func BuildSucceededAfter(after time.Duration) *BuildRunnerBehaviorBuilder { |
| 94 | + timeline := []BuildStatusPoint{StatusAt(0, BuildAccepted)} |
| 95 | + if after > 0 { |
| 96 | + timeline = append(timeline, StatusAt(after, BuildSucceeded)) |
| 97 | + } else { |
| 98 | + timeline[0] = StatusAt(0, BuildSucceeded) |
| 99 | + } |
| 100 | + return NewBuildRunnerBehavior().Trigger(BuildCreated(timeline...)) |
| 101 | +} |
| 102 | + |
| 103 | +// BuildFailedAfter returns a build behavior that fails after the given duration. |
| 104 | +func BuildFailedAfter(after time.Duration) *BuildRunnerBehaviorBuilder { |
| 105 | + timeline := []BuildStatusPoint{StatusAt(0, BuildAccepted)} |
| 106 | + if after > 0 { |
| 107 | + timeline = append(timeline, StatusAt(after, BuildFailed)) |
| 108 | + } else { |
| 109 | + timeline[0] = StatusAt(0, BuildFailed) |
| 110 | + } |
| 111 | + return NewBuildRunnerBehavior().Trigger(BuildCreated(timeline...)) |
| 112 | +} |
0 commit comments