go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/rerun/priority_test.go (about)

     1  // Copyright 2022 The LUCI Authors.
     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 rerun
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	. "github.com/smartystreets/goconvey/convey"
    23  	"go.chromium.org/luci/bisection/model"
    24  	"go.chromium.org/luci/common/clock"
    25  	"go.chromium.org/luci/common/clock/testclock"
    26  	"go.chromium.org/luci/gae/impl/memory"
    27  	"go.chromium.org/luci/gae/service/datastore"
    28  )
    29  
    30  func TestPriority(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	Convey("CapPriority", t, func() {
    34  		So(CapPriority(40), ShouldEqual, 40)
    35  		So(CapPriority(260), ShouldEqual, 255)
    36  		So(CapPriority(10), ShouldEqual, 20)
    37  	})
    38  }
    39  
    40  func TestOffsetDuration(t *testing.T) {
    41  	t.Parallel()
    42  	c := memory.Use(context.Background())
    43  	cl := testclock.New(testclock.TestTimeUTC)
    44  	c = clock.Set(c, cl)
    45  
    46  	Convey("OffsetDuration", t, func() {
    47  		now := clock.Now(c)
    48  		fb := &model.LuciFailedBuild{
    49  			Id: 123,
    50  			LuciBuild: model.LuciBuild{
    51  				StartTime: now,
    52  				EndTime:   now.Add(9 * time.Minute),
    53  			},
    54  		}
    55  		So(datastore.Put(c, fb), ShouldBeNil)
    56  		datastore.GetTestable(c).CatchupIndexes()
    57  
    58  		cf := &model.CompileFailure{
    59  			Build: datastore.KeyForObj(c, fb),
    60  		}
    61  		So(datastore.Put(c, cf), ShouldBeNil)
    62  		datastore.GetTestable(c).CatchupIndexes()
    63  
    64  		cfa := &model.CompileFailureAnalysis{
    65  			CompileFailure: datastore.KeyForObj(c, cf),
    66  		}
    67  		So(datastore.Put(c, cfa), ShouldBeNil)
    68  		datastore.GetTestable(c).CatchupIndexes()
    69  
    70  		pri, err := OffsetPriorityBasedOnRunDuration(c, 100, cfa)
    71  		So(err, ShouldBeNil)
    72  		So(pri, ShouldEqual, 80)
    73  
    74  		fb.LuciBuild.EndTime = now.Add(20 * time.Minute)
    75  		So(datastore.Put(c, fb), ShouldBeNil)
    76  		datastore.GetTestable(c).CatchupIndexes()
    77  		pri, err = OffsetPriorityBasedOnRunDuration(c, 100, cfa)
    78  		So(err, ShouldBeNil)
    79  		So(pri, ShouldEqual, 90)
    80  
    81  		fb.LuciBuild.EndTime = now.Add(50 * time.Minute)
    82  		So(datastore.Put(c, fb), ShouldBeNil)
    83  		datastore.GetTestable(c).CatchupIndexes()
    84  		pri, err = OffsetPriorityBasedOnRunDuration(c, 100, cfa)
    85  		So(err, ShouldBeNil)
    86  		So(pri, ShouldEqual, 100)
    87  
    88  		fb.LuciBuild.EndTime = now.Add(90 * time.Minute)
    89  		So(datastore.Put(c, fb), ShouldBeNil)
    90  		datastore.GetTestable(c).CatchupIndexes()
    91  		pri, err = OffsetPriorityBasedOnRunDuration(c, 100, cfa)
    92  		So(err, ShouldBeNil)
    93  		So(pri, ShouldEqual, 120)
    94  
    95  		fb.LuciBuild.EndTime = now.Add(300 * time.Minute)
    96  		So(datastore.Put(c, fb), ShouldBeNil)
    97  		datastore.GetTestable(c).CatchupIndexes()
    98  		pri, err = OffsetPriorityBasedOnRunDuration(c, 100, cfa)
    99  		So(err, ShouldBeNil)
   100  		So(pri, ShouldEqual, 140)
   101  	})
   102  }