github.com/grailbio/base@v0.0.11/traverse/time_estimate_reporter_test.go (about) 1 // Copyright 2018 GRAIL, Inc. All rights reserved. 2 // Use of this source code is governed by the Apache-2.0 3 // license that can be found in the LICENSE file. 4 5 package traverse 6 7 import ( 8 "testing" 9 "time" 10 ) 11 12 func TestBuildTimeLeftStr(t *testing.T) { 13 currentTime := time.Now() 14 15 tests := []struct { 16 reporter *timeEstimateReporter 17 expected string 18 }{ 19 { 20 reporter: &timeEstimateReporter{ 21 numWorkers: 1, 22 numQueued: 10, 23 numRunning: 0, 24 numDone: 0, 25 startTime: currentTime, 26 startTimes: map[int]time.Time{}, 27 cumulativeRuntime: time.Duration(0)}, 28 expected: "(0s left 0s avg)", 29 }, 30 { 31 reporter: &timeEstimateReporter{ 32 numWorkers: 1, 33 numQueued: 9, 34 numRunning: 1, 35 numDone: 0, 36 startTime: currentTime.Add(-1 * time.Second), 37 startTimes: map[int]time.Time{0: currentTime.Add(-1 * time.Second)}, 38 cumulativeRuntime: time.Duration(0), 39 }, 40 expected: "(>9s left 1s avg)", 41 }, 42 { 43 reporter: &timeEstimateReporter{ 44 numWorkers: 1, 45 numQueued: 9, 46 numRunning: 0, 47 numDone: 1, 48 startTime: currentTime.Add(-5 * time.Second), 49 startTimes: map[int]time.Time{}, 50 cumulativeRuntime: time.Duration(5 * time.Second), 51 }, 52 expected: "(~45s left 5s avg)", 53 }, 54 { 55 reporter: &timeEstimateReporter{ 56 numWorkers: 1, 57 numQueued: 8, 58 numRunning: 1, 59 numDone: 1, 60 startTime: currentTime.Add(-10 * time.Second), 61 startTimes: map[int]time.Time{0: currentTime.Add(-4 * time.Second)}, 62 cumulativeRuntime: time.Duration(5 * time.Second), 63 }, 64 expected: "(~41s left 5s avg)", 65 }, 66 { 67 reporter: &timeEstimateReporter{ 68 numWorkers: 1, 69 numQueued: 0, 70 numRunning: 1, 71 numDone: 9, 72 startTime: currentTime.Add(-45 * time.Second), 73 startTimes: map[int]time.Time{0: currentTime.Add(-1 * time.Second)}, 74 cumulativeRuntime: time.Duration(9 * 5 * time.Second), 75 }, 76 expected: "(~4s left 5s avg)", 77 }, 78 { 79 reporter: &timeEstimateReporter{ 80 numWorkers: 2, 81 numQueued: 8, 82 numRunning: 2, 83 numDone: 0, 84 startTime: currentTime.Add(-2 * time.Second), 85 startTimes: map[int]time.Time{0: currentTime.Add(-2 * time.Second), 1: currentTime.Add(-1 * time.Second)}, 86 cumulativeRuntime: time.Duration(0), 87 }, 88 expected: "(>6s left 2s avg)", 89 }, 90 { 91 reporter: &timeEstimateReporter{ 92 numWorkers: 2, 93 numQueued: 6, 94 numRunning: 2, 95 numDone: 2, 96 startTime: currentTime.Add(-14 * time.Second), 97 startTimes: map[int]time.Time{0: currentTime.Add(-4 * time.Second), 1: currentTime.Add(-2 * time.Second)}, 98 cumulativeRuntime: time.Duration(2 * 5 * time.Second), 99 }, 100 expected: "(~17s left 5s avg)", 101 }, 102 { 103 reporter: &timeEstimateReporter{ 104 numWorkers: 2, 105 numQueued: 2, 106 numRunning: 0, 107 numDone: 8, 108 startTime: currentTime.Add(-45 * time.Second), 109 startTimes: map[int]time.Time{}, 110 cumulativeRuntime: time.Duration(8 * 5 * time.Second), 111 }, 112 expected: "(~5s left 5s avg)", 113 }, 114 { // Note even though we have 2 workers, only one can process the single queued job, so expected time left is 5s. 115 reporter: &timeEstimateReporter{ 116 numWorkers: 2, 117 numQueued: 1, 118 numRunning: 0, 119 numDone: 9, 120 startTime: currentTime.Add(-45 * time.Second), 121 startTimes: map[int]time.Time{}, 122 cumulativeRuntime: time.Duration(9 * 5 * time.Second), 123 }, 124 expected: "(~5s left 5s avg)", 125 }, 126 { 127 reporter: &timeEstimateReporter{ 128 numWorkers: 2, 129 numQueued: 0, 130 numRunning: 1, 131 numDone: 9, 132 startTime: currentTime.Add(-48 * time.Second), 133 startTimes: map[int]time.Time{0: currentTime.Add(-3 * time.Second)}, 134 cumulativeRuntime: time.Duration(9 * 5 * time.Second), 135 }, 136 expected: "(~2s left 5s avg)", 137 }, 138 { // Last job is taking longer than average to run. 139 reporter: &timeEstimateReporter{ 140 numWorkers: 2, 141 numQueued: 0, 142 numRunning: 1, 143 numDone: 9, 144 startTime: currentTime.Add(-52 * time.Second), 145 startTimes: map[int]time.Time{0: currentTime.Add(-7 * time.Second)}, 146 cumulativeRuntime: time.Duration(9 * 5 * time.Second), 147 }, 148 expected: "(~0s left 5s avg)", 149 }, 150 } 151 152 for _, test := range tests { 153 timeLeftStr := test.reporter.buildTimeLeftStr(currentTime) 154 if timeLeftStr != test.expected { 155 t.Errorf("Got time left string: %s, expected %s", timeLeftStr, test.expected) 156 } 157 } 158 }