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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
commit 739590b081fbd12e66da6e9be87c9012afb33b8c
Author: Frank Mueller <mue@tideland.biz>
Date: Sun Mar 12 17:31:55 2017 +0100
Improved time test
Old one has been flaky.
Index: golang-github-tideland-golib/audit/generators_test.go
===================================================================
--- golang-github-tideland-golib.orig/audit/generators_test.go
+++ golang-github-tideland-golib/audit/generators_test.go
@@ -304,18 +304,18 @@ func TestTimes(t *testing.T) {
assert.True(t.Before(now.Add(dur)) || t.Equal(now.Add(dur)))
}
- one := 1 * time.Millisecond
- two := 2 * time.Millisecond
- three := 3 * time.Millisecond
- four := 4 * time.Millisecond
- five := 5 * time.Millisecond
+ sleeps := map[int]time.Duration{
+ 1: 1 * time.Millisecond,
+ 2: 2 * time.Millisecond,
+ 3: 3 * time.Millisecond,
+ 4: 4 * time.Millisecond,
+ 5: 5 * time.Millisecond,
+ }
for i := 0; i < 1000; i++ {
- before := time.Now().UnixNano()
- gen.SleepOneOf(one, two, three, four, five)
- after := time.Now().UnixNano()
- duration := (after - before) / 1000000
- assert.True(duration > 0)
- assert.True(duration < 6)
+ sleep := gen.SleepOneOf(sleeps[1], sleeps[2], sleeps[3], sleeps[4], sleeps[5])
+ s := int(sleep) / 1000000
+ _, ok := sleeps[s]
+ assert.True(ok, "Choosen duration is one the arguments")
}
}
commit 0ed36976b78b3859c1e8d35858f2b1425704263e
Author: Frank Mueller <mue@tideland.biz>
Date: Mon Mar 13 22:35:47 2017 +0100
Fixed timex tests (#22)
* Update README.md
* More robustness for the timex tests
* Final fixes plus doc changes
diff --git a/timex/crontab.go b/timex/crontab.go
index 99ee65f..284c634 100644
--- a/timex/crontab.go
+++ b/timex/crontab.go
@@ -1,6 +1,6 @@
// Tideland Go Library - Time Extensions
//
-// Copyright (C) 2009-2016 Frank Mueller / Tideland / Oldenburg / Germany
+// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
@@ -44,18 +44,18 @@ type command struct {
// Crontab is one cron server. A system can run multiple in
// parallel.
type Crontab struct {
+ frequency time.Duration
jobs map[string]Job
commandChan chan *command
- ticker *time.Ticker
loop loop.Loop
}
// NewCrontab creates a cron server.
func NewCrontab(freq time.Duration) *Crontab {
c := &Crontab{
+ frequency: freq,
jobs: make(map[string]Job),
commandChan: make(chan *command),
- ticker: time.NewTicker(freq),
}
c.loop = loop.GoRecoverable(c.backendLoop, c.checkRecovering, "crontab", freq.String())
return c
@@ -78,6 +78,7 @@ func (c *Crontab) Remove(id string) {
// backendLoop runs the server backend.
func (c *Crontab) backendLoop(l loop.Loop) error {
+ ticker := time.NewTicker(c.frequency)
for {
select {
case <-l.ShallStop():
@@ -88,7 +89,7 @@ func (c *Crontab) backendLoop(l loop.Loop) error {
} else {
delete(c.jobs, cmd.id)
}
- case now := <-c.ticker.C:
+ case now := <-ticker.C:
for id, job := range c.jobs {
c.do(id, job, now)
}
diff --git a/timex/doc.go b/timex/doc.go
index 08400ed..749f7e4 100644
--- a/timex/doc.go
+++ b/timex/doc.go
@@ -1,14 +1,14 @@
// Tideland Go Library - Time Extensions
//
-// Copyright (C) 2009-2015 Frank Mueller / Tideland / Oldenburg / Germany
+// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
-// The Tideland Go Library timex package helps when working with times and dates.
-// Beside tests it contains a crontab for chronological jobs and a retry function
-// to let code blocks be retried under well defined conditions regarding time and
-// count.
+// Package timex helps when working with times and dates. Beside
+// tests it contains a crontab for chronological jobs and a retry
+// function to let code blocks be retried under well defined conditions
+// regarding time and count.
package timex
// EOF
diff --git a/timex/errors.go b/timex/errors.go
index 780a333..ba84128 100644
--- a/timex/errors.go
+++ b/timex/errors.go
@@ -1,6 +1,6 @@
// Tideland Go Library - Time Extensions
//
-// Copyright (C) 2015 Frank Mueller / Tideland / Oldenburg / Germany
+// Copyright (C) 2015-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
diff --git a/timex/retry.go b/timex/retry.go
index 259ec21..4303fcb 100644
--- a/timex/retry.go
+++ b/timex/retry.go
@@ -1,6 +1,6 @@
// Tideland Go Library - Time Extensions
//
-// Copyright (C) 2009-2015 Frank Mueller / Tideland / Oldenburg / Germany
+// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
diff --git a/timex/timex.go b/timex/timex.go
index 6e18324..4888e4d 100644
--- a/timex/timex.go
+++ b/timex/timex.go
@@ -1,6 +1,6 @@
// Tideland Go Library - Time Extensions
//
-// Copyright (C) 2009-2015 Frank Mueller / Tideland / Oldenburg / Germany
+// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
diff --git a/timex/timex_test.go b/timex/timex_test.go
index 58523e7..cea7045 100644
--- a/timex/timex_test.go
+++ b/timex/timex_test.go
@@ -1,6 +1,6 @@
// Tideland Go Library - Time Extensions - Unit Tests
//
-// Copyright (C) 2009-2015 Frank Mueller / Tideland / Oldenburg / Germany
+// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
@@ -87,15 +87,20 @@ func TestCrontabKeep(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
// Create test crontab with job.
- c := timex.NewCrontab(10 * time.Millisecond)
- j := &cronjob{0, false, false}
+ c := timex.NewCrontab(50 * time.Millisecond)
+ j := &cronjob{[]time.Time{}, false, false}
c.Add("keep", j)
- time.Sleep(50 * time.Millisecond)
+ time.Sleep(time.Second)
c.Remove("keep")
c.Stop()
- assert.Equal(j.counter, 3, "job counter increased twice")
+ 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.
@@ -104,14 +109,14 @@ func TestCrontabRemove(t *testing.T) {
// Create test crontab with job.
c := timex.NewCrontab(10 * time.Millisecond)
- j := &cronjob{0, false, false}
+ j := &cronjob{[]time.Time{}, false, false}
c.Add("remove", j)
- time.Sleep(250 * time.Millisecond)
+ time.Sleep(500 * time.Millisecond)
c.Remove("remove")
c.Stop()
- assert.Equal(j.counter, 10, "job counter increased max ten times")
+ assert.Length(j.times, 10, "job counter increased max ten times")
}
// Test crontab removing the job after an error.
@@ -120,14 +125,14 @@ func TestCrontabError(t *testing.T) {
// Create test crontab with job.
c := timex.NewCrontab(10 * time.Millisecond)
- j := &cronjob{0, false, true}
+ j := &cronjob{[]time.Time{}, false, true}
c.Add("remove", j)
- time.Sleep(250 * time.Millisecond)
+ time.Sleep(500 * time.Millisecond)
c.Remove("remove")
c.Stop()
- assert.Equal(j.counter, 5, "job counter increased max five times")
+ assert.Length(j.times, 5, "job counter increased max five times")
}
// TestRetrySuccess tests a successful retry.
@@ -190,9 +195,9 @@ func TestRetryTooOften(t *testing.T) {
//--------------------
type cronjob struct {
- counter int
- flip bool
- fail bool
+ times []time.Time
+ flip bool
+ fail bool
}
func (j *cronjob) ShallExecute(t time.Time) bool {
@@ -201,11 +206,11 @@ func (j *cronjob) ShallExecute(t time.Time) bool {
}
func (j *cronjob) Execute() (bool, error) {
- j.counter++
- if j.fail && j.counter == 5 {
+ j.times = append(j.times, time.Now())
+ if j.fail && len(j.times) == 5 {
return false, errors.New("failed")
}
- if j.counter == 10 {
+ if len(j.times) == 10 {
return false, nil
}
return true, nil
|