go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gce/appengine/backend/internal/metrics/tasks_test.go (about) 1 // Copyright 2019 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 metrics 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "go.chromium.org/luci/common/tsmon" 23 "go.chromium.org/luci/gae/impl/memory" 24 "go.chromium.org/luci/gae/service/datastore" 25 26 . "github.com/smartystreets/goconvey/convey" 27 ) 28 29 func TestTasks(t *testing.T) { 30 t.Parallel() 31 32 Convey("Tasks", t, func() { 33 c, _ := tsmon.WithDummyInMemory(memory.Use(context.Background())) 34 datastore.GetTestable(c).AutoIndex(true) 35 datastore.GetTestable(c).Consistent(true) 36 s := tsmon.Store(c) 37 38 Convey("TaskCount", func() { 39 tc := &TaskCount{} 40 So(tc.Executing, ShouldEqual, 0) 41 So(tc.Total, ShouldEqual, 0) 42 So(tc.Update(c, "queue", 1, 2), ShouldBeNil) 43 44 tc = &TaskCount{ 45 ID: "queue", 46 } 47 So(datastore.Get(c, tc), ShouldBeNil) 48 So(tc.Executing, ShouldEqual, 1) 49 So(tc.Total, ShouldEqual, 2) 50 So(tc.Queue, ShouldEqual, tc.ID) 51 }) 52 53 Convey("updateTasks", func() { 54 fields := []any{"queue"} 55 56 tc := &TaskCount{ 57 ID: "queue", 58 Executing: 1, 59 Queue: "queue", 60 Total: 3, 61 } 62 63 tc.Computed = time.Time{} 64 So(datastore.Put(c, tc), ShouldBeNil) 65 updateTasks(c) 66 So(s.Get(c, tasksExecuting, time.Time{}, fields), ShouldBeNil) 67 So(s.Get(c, tasksPending, time.Time{}, fields), ShouldBeNil) 68 So(s.Get(c, tasksTotal, time.Time{}, fields), ShouldBeNil) 69 So(datastore.Get(c, &TaskCount{ 70 ID: tc.ID, 71 }), ShouldEqual, datastore.ErrNoSuchEntity) 72 73 tc.Computed = time.Now().UTC() 74 So(datastore.Put(c, tc), ShouldBeNil) 75 updateTasks(c) 76 So(s.Get(c, tasksExecuting, time.Time{}, fields).(int64), ShouldEqual, 1) 77 So(s.Get(c, tasksPending, time.Time{}, fields).(int64), ShouldEqual, 2) 78 So(s.Get(c, tasksTotal, time.Time{}, fields).(int64), ShouldEqual, 3) 79 So(datastore.Get(c, &TaskCount{ 80 ID: tc.ID, 81 }), ShouldBeNil) 82 }) 83 }) 84 }