github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/scheduler/task_finder_test.go (about) 1 package scheduler 2 3 import ( 4 "testing" 5 6 "github.com/evergreen-ci/evergreen" 7 "github.com/evergreen-ci/evergreen/db" 8 "github.com/evergreen-ci/evergreen/model/task" 9 "github.com/evergreen-ci/evergreen/testutil" 10 "github.com/mongodb/grip" 11 . "github.com/smartystreets/goconvey/convey" 12 ) 13 14 var taskFinderTestConf = testutil.TestConfig() 15 16 func init() { 17 db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(taskFinderTestConf)) 18 grip.CatchError(grip.SetSender(testutil.SetupTestSender(taskFinderTestConf.Scheduler.LogFile))) 19 } 20 21 func TestDBTaskFinder(t *testing.T) { 22 23 var taskIds []string 24 var tasks []*task.Task 25 var depTaskIds []string 26 var depTasks []*task.Task 27 var taskFinder *DBTaskFinder 28 29 Convey("With a DBTaskFinder", t, func() { 30 31 taskFinder = &DBTaskFinder{} 32 33 taskIds = []string{"t1", "t2", "t3", "t4"} 34 tasks = []*task.Task{ 35 {Id: taskIds[0], Status: evergreen.TaskUndispatched, Activated: true}, 36 {Id: taskIds[1], Status: evergreen.TaskUndispatched, Activated: true}, 37 {Id: taskIds[2], Status: evergreen.TaskUndispatched, Activated: true}, 38 {Id: taskIds[3], Status: evergreen.TaskUndispatched, Activated: true, Priority: -1}, 39 } 40 41 depTaskIds = []string{"td1", "td2"} 42 depTasks = []*task.Task{ 43 {Id: depTaskIds[0]}, 44 {Id: depTaskIds[1]}, 45 } 46 47 So(db.Clear(task.Collection), ShouldBeNil) 48 49 Convey("if there are no runnable tasks, an empty slice (with no error)"+ 50 " should be returned", func() { 51 runnableTasks, err := taskFinder.FindRunnableTasks() 52 So(err, ShouldBeNil) 53 So(len(runnableTasks), ShouldEqual, 0) 54 }) 55 56 Convey("inactive tasks should not be returned", func() { 57 58 // insert the tasks, setting one to inactive 59 tasks[2].Activated = false 60 for _, testTask := range tasks { 61 So(testTask.Insert(), ShouldBeNil) 62 } 63 64 // finding the runnable tasks should return two tasks 65 runnableTasks, err := taskFinder.FindRunnableTasks() 66 So(err, ShouldBeNil) 67 So(len(runnableTasks), ShouldEqual, 2) 68 69 }) 70 71 Convey("tasks with unmet dependencies should not be returned", func() { 72 73 // insert the dependency tasks, setting one to have finished 74 // successfully and one to have finished unsuccessfully 75 depTasks[0].Status = evergreen.TaskFailed 76 depTasks[1].Status = evergreen.TaskSucceeded 77 for _, depTask := range depTasks { 78 So(depTask.Insert(), ShouldBeNil) 79 } 80 81 // insert the tasks, setting one to have unmet dependencies, one to 82 // have no dependencies, and one to have successfully met 83 // dependencies 84 tasks[0].DependsOn = []task.Dependency{} 85 tasks[1].DependsOn = []task.Dependency{{depTasks[0].Id, evergreen.TaskSucceeded}} 86 tasks[2].DependsOn = []task.Dependency{{depTasks[1].Id, evergreen.TaskSucceeded}} 87 for _, testTask := range tasks { 88 So(testTask.Insert(), ShouldBeNil) 89 } 90 91 // finding the runnable tasks should return two tasks (the one with 92 // no dependencies and the one with successfully met dependencies 93 runnableTasks, err := taskFinder.FindRunnableTasks() 94 So(err, ShouldBeNil) 95 So(len(runnableTasks), ShouldEqual, 2) 96 97 }) 98 99 }) 100 101 }