|
| 1 | +// Copyright (c) 2025 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 standard |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + "go.uber.org/mock/gomock" |
| 25 | + "go.uber.org/zap/zaptest" |
| 26 | + |
| 27 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 28 | + allocatormock "github.com/uber/submitqueue/submitqueue/extension/speculation/allocator/mock" |
| 29 | + "github.com/uber/submitqueue/submitqueue/extension/speculation/allocator/sticky" |
| 30 | + "github.com/uber/submitqueue/submitqueue/extension/speculation/generator/bestfirst" |
| 31 | + generatormock "github.com/uber/submitqueue/submitqueue/extension/speculation/generator/mock" |
| 32 | +) |
| 33 | + |
| 34 | +// betFor returns the bet a path carries for a given dependency batch. |
| 35 | +func betFor(p entity.SpeculationPath, dep string) entity.DependencyBetType { |
| 36 | + for _, b := range p.Bets { |
| 37 | + if b.Batch == dep { |
| 38 | + return b.Bet |
| 39 | + } |
| 40 | + } |
| 41 | + return entity.BetUnknown |
| 42 | +} |
| 43 | + |
| 44 | +// constScorer is a minimal scorer.Scorer that scores every batch identically. |
| 45 | +type constScorer struct{ v float64 } |
| 46 | + |
| 47 | +func (c constScorer) Score(context.Context, entity.Batch) (float64, error) { return c.v, nil } |
| 48 | + |
| 49 | +func TestComposed_EndToEnd_NaivePair(t *testing.T) { |
| 50 | + batches := []entity.Batch{ |
| 51 | + {ID: "q/1", State: entity.BatchStateSpeculating}, |
| 52 | + {ID: "q/2", State: entity.BatchStateSpeculating, Dependencies: []string{"q/1"}}, |
| 53 | + } |
| 54 | + |
| 55 | + // bestfirst generator + sticky allocator with a 2-build budget. |
| 56 | + spec := New(bestfirst.New(constScorer{0.9}), sticky.New(zaptest.NewLogger(t).Sugar(), 2)) |
| 57 | + got, err := spec.Speculate(context.Background(), batches, nil) |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + // Two free budget slots -> two builds. Everything proposed is a build. |
| 61 | + require.Len(t, got, 2) |
| 62 | + var q2 *entity.Speculation |
| 63 | + for i := range got { |
| 64 | + assert.Equal(t, entity.PathActionBuild, got[i].Action) |
| 65 | + if got[i].Path.Head == "q/2" { |
| 66 | + q2 = &got[i] |
| 67 | + } |
| 68 | + } |
| 69 | + // q/2's funded path is the optimistic all-included guess on q/1. |
| 70 | + require.NotNil(t, q2) |
| 71 | + assert.Equal(t, entity.BetIncluded, betFor(q2.Path, "q/1")) |
| 72 | +} |
| 73 | + |
| 74 | +func TestComposed_WiresGeneratorIntoAllocator(t *testing.T) { |
| 75 | + ctrl := gomock.NewController(t) |
| 76 | + |
| 77 | + batches := []entity.Batch{{ID: "q/1", State: entity.BatchStateSpeculating}} |
| 78 | + pathSets := []entity.SpeculationPathSet{{BatchID: "q/1"}} |
| 79 | + want := []entity.Speculation{{ |
| 80 | + Path: entity.SpeculationPath{Head: "q/1"}, |
| 81 | + Action: entity.PathActionBuild, |
| 82 | + }} |
| 83 | + |
| 84 | + iter := generatormock.NewMockPathIterator(ctrl) |
| 85 | + gen := generatormock.NewMockGenerator(ctrl) |
| 86 | + alloc := allocatormock.NewMockAllocator(ctrl) |
| 87 | + |
| 88 | + gen.EXPECT().Open(gomock.Any(), batches, pathSets).Return(iter, nil) |
| 89 | + alloc.EXPECT().Allocate(gomock.Any(), pathSets, iter).Return(want, nil) |
| 90 | + |
| 91 | + got, err := New(gen, alloc).Speculate(context.Background(), batches, pathSets) |
| 92 | + require.NoError(t, err) |
| 93 | + assert.Equal(t, want, got) |
| 94 | +} |
| 95 | + |
| 96 | +func TestComposed_PropagatesGeneratorError(t *testing.T) { |
| 97 | + ctrl := gomock.NewController(t) |
| 98 | + |
| 99 | + errOpen := errors.New("open boom") |
| 100 | + gen := generatormock.NewMockGenerator(ctrl) |
| 101 | + alloc := allocatormock.NewMockAllocator(ctrl) |
| 102 | + |
| 103 | + gen.EXPECT().Open(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errOpen) |
| 104 | + // Allocate must not be called when Open fails (no alloc.EXPECT()). |
| 105 | + |
| 106 | + _, err := New(gen, alloc).Speculate(context.Background(), nil, nil) |
| 107 | + require.ErrorIs(t, err, errOpen) |
| 108 | +} |
0 commit comments