Skip to content

Commit 0ed3697

Browse files
author
Frank Mueller
authored
Fixed timex tests (#22)
* Update README.md * More robustness for the timex tests * Final fixes plus doc changes
1 parent 7ac4cb7 commit 0ed3697

File tree

8 files changed

+40
-30
lines changed

8 files changed

+40
-30
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Tideland Go Library
22

3+
## 2017-03-13
4+
5+
- Fixed flaky tests in *timex* package
6+
37
## 2017-02-13
48

59
- *audit.Assertion* now is better in comparing strings with

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ I hope you like them. ;)
1818

1919
## Version
2020

21-
Version 4.21.4
21+
Version 4.21.5
2222

2323
## Packages
2424

timex/crontab.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Library - Time Extensions
22
//
3-
// Copyright (C) 2009-2016 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.
@@ -44,18 +44,18 @@ type command struct {
4444
// Crontab is one cron server. A system can run multiple in
4545
// parallel.
4646
type Crontab struct {
47+
frequency time.Duration
4748
jobs map[string]Job
4849
commandChan chan *command
49-
ticker *time.Ticker
5050
loop loop.Loop
5151
}
5252

5353
// NewCrontab creates a cron server.
5454
func NewCrontab(freq time.Duration) *Crontab {
5555
c := &Crontab{
56+
frequency: freq,
5657
jobs: make(map[string]Job),
5758
commandChan: make(chan *command),
58-
ticker: time.NewTicker(freq),
5959
}
6060
c.loop = loop.GoRecoverable(c.backendLoop, c.checkRecovering, "crontab", freq.String())
6161
return c
@@ -78,6 +78,7 @@ func (c *Crontab) Remove(id string) {
7878

7979
// backendLoop runs the server backend.
8080
func (c *Crontab) backendLoop(l loop.Loop) error {
81+
ticker := time.NewTicker(c.frequency)
8182
for {
8283
select {
8384
case <-l.ShallStop():
@@ -88,7 +89,7 @@ func (c *Crontab) backendLoop(l loop.Loop) error {
8889
} else {
8990
delete(c.jobs, cmd.id)
9091
}
91-
case now := <-c.ticker.C:
92+
case now := <-ticker.C:
9293
for id, job := range c.jobs {
9394
c.do(id, job, now)
9495
}

timex/doc.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Tideland Go Library - Time Extensions
22
//
3-
// Copyright (C) 2009-2015 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.
77

8-
// The Tideland Go Library timex package helps when working with times and dates.
9-
// Beside tests it contains a crontab for chronological jobs and a retry function
10-
// to let code blocks be retried under well defined conditions regarding time and
11-
// count.
8+
// Package timex helps when working with times and dates. Beside
9+
// tests it contains a crontab for chronological jobs and a retry
10+
// function to let code blocks be retried under well defined conditions
11+
// regarding time and count.
1212
package timex
1313

1414
// EOF

timex/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Library - Time Extensions
22
//
3-
// Copyright (C) 2015 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2015-2017 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.

timex/retry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Library - Time Extensions
22
//
3-
// Copyright (C) 2009-2015 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.

timex/timex.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Library - Time Extensions
22
//
3-
// Copyright (C) 2009-2015 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.

timex/timex_test.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tideland Go Library - Time Extensions - Unit Tests
22
//
3-
// Copyright (C) 2009-2015 Frank Mueller / Tideland / Oldenburg / Germany
3+
// Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
44
//
55
// All rights reserved. Use of this source code is governed
66
// by the new BSD license.
@@ -87,15 +87,20 @@ func TestCrontabKeep(t *testing.T) {
8787
assert := audit.NewTestingAssertion(t, true)
8888

8989
// Create test crontab with job.
90-
c := timex.NewCrontab(10 * time.Millisecond)
91-
j := &cronjob{0, false, false}
90+
c := timex.NewCrontab(50 * time.Millisecond)
91+
j := &cronjob{[]time.Time{}, false, false}
9292

9393
c.Add("keep", j)
94-
time.Sleep(50 * time.Millisecond)
94+
time.Sleep(time.Second)
9595
c.Remove("keep")
9696
c.Stop()
9797

98-
assert.Equal(j.counter, 3, "job counter increased twice")
98+
for i := range j.times {
99+
if i > 0 {
100+
d := j.times[i].Sub(j.times[i-1])
101+
assert.True(d.Seconds() >= 0.05)
102+
}
103+
}
99104
}
100105

101106
// Test crontab removing the job.
@@ -104,14 +109,14 @@ func TestCrontabRemove(t *testing.T) {
104109

105110
// Create test crontab with job.
106111
c := timex.NewCrontab(10 * time.Millisecond)
107-
j := &cronjob{0, false, false}
112+
j := &cronjob{[]time.Time{}, false, false}
108113

109114
c.Add("remove", j)
110-
time.Sleep(250 * time.Millisecond)
115+
time.Sleep(500 * time.Millisecond)
111116
c.Remove("remove")
112117
c.Stop()
113118

114-
assert.Equal(j.counter, 10, "job counter increased max ten times")
119+
assert.Length(j.times, 10, "job counter increased max ten times")
115120
}
116121

117122
// Test crontab removing the job after an error.
@@ -120,14 +125,14 @@ func TestCrontabError(t *testing.T) {
120125

121126
// Create test crontab with job.
122127
c := timex.NewCrontab(10 * time.Millisecond)
123-
j := &cronjob{0, false, true}
128+
j := &cronjob{[]time.Time{}, false, true}
124129

125130
c.Add("remove", j)
126-
time.Sleep(250 * time.Millisecond)
131+
time.Sleep(500 * time.Millisecond)
127132
c.Remove("remove")
128133
c.Stop()
129134

130-
assert.Equal(j.counter, 5, "job counter increased max five times")
135+
assert.Length(j.times, 5, "job counter increased max five times")
131136
}
132137

133138
// TestRetrySuccess tests a successful retry.
@@ -190,9 +195,9 @@ func TestRetryTooOften(t *testing.T) {
190195
//--------------------
191196

192197
type cronjob struct {
193-
counter int
194-
flip bool
195-
fail bool
198+
times []time.Time
199+
flip bool
200+
fail bool
196201
}
197202

198203
func (j *cronjob) ShallExecute(t time.Time) bool {
@@ -201,11 +206,11 @@ func (j *cronjob) ShallExecute(t time.Time) bool {
201206
}
202207

203208
func (j *cronjob) Execute() (bool, error) {
204-
j.counter++
205-
if j.fail && j.counter == 5 {
209+
j.times = append(j.times, time.Now())
210+
if j.fail && len(j.times) == 5 {
206211
return false, errors.New("failed")
207212
}
208-
if j.counter == 10 {
213+
if len(j.times) == 10 {
209214
return false, nil
210215
}
211216
return true, nil

0 commit comments

Comments
 (0)