1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
// Tideland Go Library - Time Extensions - Unit Tests
//
// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package timex_test
//--------------------
// IMPORTS
//--------------------
import (
"errors"
"testing"
"time"
"github.com/tideland/golib/audit"
"github.com/tideland/golib/timex"
)
//--------------------
// TESTS
//--------------------
// Test time containments.
func TestTimeContainments(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
// Create some test data.
ts := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
years := []int{2008, 2009, 2010}
months := []time.Month{10, 11, 12}
days := []int{10, 11, 12, 13, 14}
hours := []int{20, 21, 22, 23}
minutes := []int{0, 5, 10, 15, 20, 25}
seconds := []int{0, 15, 30, 45}
weekdays := []time.Weekday{time.Monday, time.Tuesday, time.Wednesday}
assert.True(timex.YearInList(ts, years), "Go time in year list.")
assert.True(timex.YearInRange(ts, 2005, 2015), "Go time in year range.")
assert.True(timex.MonthInList(ts, months), "Go time in month list.")
assert.True(timex.MonthInRange(ts, 7, 12), "Go time in month range.")
assert.True(timex.DayInList(ts, days), "Go time in day list.")
assert.True(timex.DayInRange(ts, 5, 15), "Go time in day range .")
assert.True(timex.HourInList(ts, hours), "Go time in hour list.")
assert.True(timex.HourInRange(ts, 20, 31), "Go time in hour range .")
assert.True(timex.MinuteInList(ts, minutes), "Go time in minute list.")
assert.True(timex.MinuteInRange(ts, 0, 5), "Go time in minute range .")
assert.True(timex.SecondInList(ts, seconds), "Go time in second list.")
assert.True(timex.SecondInRange(ts, 0, 5), "Go time in second range .")
assert.True(timex.WeekdayInList(ts, weekdays), "Go time in weekday list.")
assert.True(timex.WeekdayInRange(ts, time.Monday, time.Friday), "Go time in weekday range .")
}
// TestBeginOf tests the calculation of a beginning of a unit of time.
func TestBeginOf(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
ts := time.Date(2015, time.August, 2, 15, 10, 45, 12345, time.UTC)
assert.Equal(timex.BeginOf(ts, timex.Second), time.Date(2015, time.August, 2, 15, 10, 45, 0, time.UTC))
assert.Equal(timex.BeginOf(ts, timex.Minute), time.Date(2015, time.August, 2, 15, 10, 0, 0, time.UTC))
assert.Equal(timex.BeginOf(ts, timex.Hour), time.Date(2015, time.August, 2, 15, 0, 0, 0, time.UTC))
assert.Equal(timex.BeginOf(ts, timex.Day), time.Date(2015, time.August, 2, 0, 0, 0, 0, time.UTC))
assert.Equal(timex.BeginOf(ts, timex.Month), time.Date(2015, time.August, 1, 0, 0, 0, 0, time.UTC))
assert.Equal(timex.BeginOf(ts, timex.Year), time.Date(2015, time.January, 1, 0, 0, 0, 0, time.UTC))
}
// TestEndOf tests the calculation of a ending of a unit of time.
func TestEndOf(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
ts := time.Date(2012, time.February, 2, 15, 10, 45, 12345, time.UTC)
assert.Equal(timex.EndOf(ts, timex.Second), time.Date(2012, time.February, 2, 15, 10, 45, 999999999, time.UTC))
assert.Equal(timex.EndOf(ts, timex.Minute), time.Date(2012, time.February, 2, 15, 10, 59, 999999999, time.UTC))
assert.Equal(timex.EndOf(ts, timex.Hour), time.Date(2012, time.February, 2, 15, 59, 59, 999999999, time.UTC))
assert.Equal(timex.EndOf(ts, timex.Day), time.Date(2012, time.February, 2, 23, 59, 59, 999999999, time.UTC))
assert.Equal(timex.EndOf(ts, timex.Month), time.Date(2012, time.February, 29, 23, 59, 59, 999999999, time.UTC))
assert.Equal(timex.EndOf(ts, timex.Year), time.Date(2012, time.December, 31, 23, 59, 59, 999999999, time.UTC))
}
// Test crontab keeping the job.
func TestCrontabKeep(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
// Create test crontab with job.
c := timex.NewCrontab(50 * time.Millisecond)
j := &cronjob{[]time.Time{}, false, false}
c.Add("keep", j)
time.Sleep(time.Second)
c.Remove("keep")
c.Stop()
for i := range j.times {
if i > 0 {
d := j.times[i].Sub(j.times[i-1])
assert.True(d.Seconds() >= 0.05)
}
}
}
// Test crontab removing the job.
func TestCrontabRemove(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
// Create test crontab with job.
c := timex.NewCrontab(10 * time.Millisecond)
j := &cronjob{[]time.Time{}, false, false}
c.Add("remove", j)
time.Sleep(500 * time.Millisecond)
c.Remove("remove")
c.Stop()
assert.Length(j.times, 10, "job counter increased max ten times")
}
// Test crontab removing the job after an error.
func TestCrontabError(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
// Create test crontab with job.
c := timex.NewCrontab(10 * time.Millisecond)
j := &cronjob{[]time.Time{}, false, true}
c.Add("remove", j)
time.Sleep(500 * time.Millisecond)
c.Remove("remove")
c.Stop()
assert.Length(j.times, 5, "job counter increased max five times")
}
// TestRetrySuccess tests a successful retry.
func TestRetrySuccess(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
count := 0
err := timex.Retry(func() (bool, error) {
count++
return count == 5, nil
}, timex.ShortAttempt())
assert.Nil(err)
assert.Equal(count, 5)
}
// TestRetryFuncError tests an error inside the retried func.
func TestRetryFuncError(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
err := timex.Retry(func() (bool, error) {
return false, errors.New("ouch")
}, timex.ShortAttempt())
assert.ErrorMatch(err, "ouch")
}
// TestRetryTooLong tests a retry timout.
func TestRetryTooLong(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
rs := timex.RetryStrategy{
Count: 10,
Break: 5 * time.Millisecond,
BreakIncrement: 5 * time.Millisecond,
Timeout: 50 * time.Millisecond,
}
err := timex.Retry(func() (bool, error) {
return false, nil
}, rs)
assert.ErrorMatch(err, ".* retried longer than .*")
}
// TestRetryTooOften tests a retry count error.
func TestRetryTooOften(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
rs := timex.RetryStrategy{
Count: 5,
Break: 5 * time.Millisecond,
BreakIncrement: 5 * time.Millisecond,
Timeout: time.Second,
}
err := timex.Retry(func() (bool, error) {
return false, nil
}, rs)
assert.ErrorMatch(err, ".* retried more than .* times")
}
//--------------------
// HELPERS
//--------------------
type cronjob struct {
times []time.Time
flip bool
fail bool
}
func (j *cronjob) ShallExecute(t time.Time) bool {
j.flip = !j.flip
return j.flip
}
func (j *cronjob) Execute() (bool, error) {
j.times = append(j.times, time.Now())
if j.fail && len(j.times) == 5 {
return false, errors.New("failed")
}
if len(j.times) == 10 {
return false, nil
}
return true, nil
}
// EOF
|