github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/process_runtime_test.go (about) 1 package model 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/evergreen-ci/evergreen/db" 8 "github.com/evergreen-ci/evergreen/testutil" 9 "github.com/mongodb/grip" 10 . "github.com/smartystreets/goconvey/convey" 11 "gopkg.in/mgo.v2/bson" 12 ) 13 14 var ( 15 runtimeId = "runtimeId" 16 allRuntimeIds = []string{runtimeId + "1", runtimeId + "2", runtimeId + "3"} 17 testConfig = testutil.TestConfig() 18 ) 19 20 func init() { 21 db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConfig)) 22 } 23 24 func resetCollection() { 25 grip.CatchError(db.Clear(RuntimesCollection)) 26 } 27 28 // Test that Upserts happen properly 29 func TestRuntimeUpsert(t *testing.T) { 30 Convey("When updating runtimes in an empty collection", t, func() { 31 Convey("An update should insert a document if that id is not"+ 32 " present", func() { 33 found, err := FindEveryProcessRuntime() 34 So(len(found), ShouldEqual, 0) 35 So(err, ShouldBeNil) 36 37 So(SetProcessRuntimeCompleted(allRuntimeIds[0], time.Duration(0)), ShouldBeNil) 38 found, err = FindEveryProcessRuntime() 39 So(len(found), ShouldEqual, 1) 40 So(err, ShouldBeNil) 41 42 So(SetProcessRuntimeCompleted(allRuntimeIds[1], time.Duration(0)), ShouldBeNil) 43 found, err = FindEveryProcessRuntime() 44 So(err, ShouldBeNil) 45 So(len(found), ShouldEqual, 2) 46 }) 47 48 Convey("An update should not create new docs if the Id already"+ 49 " exists", func() { 50 So(SetProcessRuntimeCompleted(allRuntimeIds[0], time.Duration(0)), ShouldBeNil) 51 So(SetProcessRuntimeCompleted(allRuntimeIds[1], time.Duration(0)), ShouldBeNil) 52 found, err := FindEveryProcessRuntime() 53 So(err, ShouldBeNil) 54 So(len(found), ShouldEqual, 2) 55 }) 56 57 Reset(func() { 58 resetCollection() 59 }) 60 }) 61 } 62 63 // Tests that time is properly updated when Report is called 64 func TestRuntimeTimeUpdate(t *testing.T) { 65 Convey("When a new time is reported", t, func() { 66 Convey("The time document should save the time at which it was"+ 67 " updated", func() { 68 So(SetProcessRuntimeCompleted(allRuntimeIds[0], time.Duration(0)), ShouldBeNil) 69 time.Sleep(time.Millisecond) 70 testCutoff := time.Now() 71 time.Sleep(time.Millisecond) 72 runtime, err := FindOneProcessRuntime(bson.M{}, bson.M{}) 73 So(err, ShouldBeNil) 74 So(runtime, ShouldNotBeNil) 75 So(runtime.FinishedAt, ShouldHappenBefore, testCutoff) 76 77 So(SetProcessRuntimeCompleted(allRuntimeIds[0], time.Duration(0)), ShouldBeNil) 78 runtime, err = FindOneProcessRuntime(bson.M{}, bson.M{}) 79 So(err, ShouldBeNil) 80 So(runtime, ShouldNotBeNil) 81 So(runtime.FinishedAt, ShouldHappenAfter, testCutoff) 82 }) 83 84 Reset(func() { 85 resetCollection() 86 }) 87 }) 88 } 89 90 // Tests that only runtimes before the given cutoff are returned 91 func TestFindLateRuntimes(t *testing.T) { 92 Convey("When finding late runtime reports", t, func() { 93 So(SetProcessRuntimeCompleted(allRuntimeIds[0], time.Duration(0)), ShouldBeNil) 94 So(SetProcessRuntimeCompleted(allRuntimeIds[1], time.Duration(0)), ShouldBeNil) 95 time.Sleep(time.Millisecond) 96 testCutoff := time.Now() 97 time.Sleep(time.Millisecond) 98 So(SetProcessRuntimeCompleted(allRuntimeIds[2], time.Duration(0)), ShouldBeNil) 99 100 Convey("Only times committed before the cutoff are returned", func() { 101 all, err := FindEveryProcessRuntime() 102 So(len(all), ShouldEqual, 3) 103 So(err, ShouldBeNil) 104 105 lateRuntimes, err := FindAllLateProcessRuntimes(testCutoff) 106 So(err, ShouldBeNil) 107 So(len(lateRuntimes), ShouldEqual, 2) 108 }) 109 110 Convey("And when all times are more recent than the cutoff, none are returned", func() { 111 So(SetProcessRuntimeCompleted(allRuntimeIds[0], time.Duration(0)), ShouldBeNil) 112 So(SetProcessRuntimeCompleted(allRuntimeIds[1], time.Duration(0)), ShouldBeNil) 113 lateRuntimes, err := FindAllLateProcessRuntimes(testCutoff) 114 So(err, ShouldBeNil) 115 So(len(lateRuntimes), ShouldEqual, 0) 116 }) 117 118 Reset(func() { 119 resetCollection() 120 }) 121 }) 122 } 123 124 func TestRuntimeDuration(t *testing.T) { 125 Convey("When updating a runtime duration", t, func() { 126 Convey("The proper value and type is returned", func() { 127 err := SetProcessRuntimeCompleted(allRuntimeIds[0], time.Minute) 128 So(err, ShouldBeNil) 129 130 runtime, err := FindProcessRuntime(allRuntimeIds[0]) 131 So(runtime, ShouldNotBeNil) 132 So(runtime.Runtime, ShouldEqual, time.Minute) 133 So(err, ShouldBeNil) 134 }) 135 136 Reset(func() { 137 resetCollection() 138 }) 139 }) 140 141 }