• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

coccyx / gogen / 13508585435

24 Feb 2025 09:52PM UTC coverage: 53.112% (+1.0%) from 52.137%
13508585435

Pull #45

github

coccyx
fixing timezone extraction bug in the test
Pull Request #45: Tests/increase coverage

53 of 67 new or added lines in 3 files covered. (79.1%)

250 existing lines in 7 files now uncovered.

2142 of 4033 relevant lines covered (53.11%)

58.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.84
/timer/timer.go
1
package timer
2

3
import (
4
        "time"
5

6
        config "github.com/coccyx/gogen/internal"
7
        log "github.com/coccyx/gogen/logger"
8
        "github.com/coccyx/gogen/rater"
9
)
10

11
// Timer will put work into the generator queue on an interval specified by the Sample.
12
// One instance is created per sample.
13
type Timer struct {
14
        S              *config.Sample
15
        cur            int
16
        GQ             chan *config.GenQueueItem
17
        OQ             chan *config.OutQueueItem
18
        Done           chan int
19
        closed         bool
20
        cacheCounter   int // Number of intervals left to use cache
21
        cacheIntervals int // Number of intervals to cache for
22
}
23

24
// NewTimer creates a new Timer for a sample which will put work into the generator queue on each interval
25
func (t *Timer) NewTimer(cacheIntervals int) {
8✔
26
        s := t.S
8✔
27
        t.cacheIntervals = cacheIntervals
8✔
28
        // If we're not realtime, then we should be backfilling
8✔
29
        if !s.Realtime {
14✔
30
                // Set the end time based on configuration, either now or a specified time in the config
6✔
31
                var endtime time.Time
6✔
32
                n := time.Now()
6✔
33
                if s.EndParsed.Before(n) && !s.EndParsed.IsZero() {
8✔
34
                        endtime = s.EndParsed
2✔
35
                } else {
6✔
36
                        endtime = n
4✔
37
                }
4✔
38
                // Run through as many intervals until we're at endtime
39
                t.backfill(endtime)
6✔
40
                // If we had no endtime set, then keep going in realtime mode
6✔
41
                if s.EndParsed.IsZero() {
9✔
42
                        t.backfill(time.Now())
3✔
43
                        s.Realtime = true
3✔
44
                }
3✔
45
        }
46
        // Endtime can be greater than now, so continue until we've reached the end time... Realtime won't get set, so we'll end after this
47
        if !t.S.Realtime {
11✔
48
                t.backfill(s.EndParsed)
3✔
49
        }
3✔
50
        // In realtime mode, continue until we get an interrupt
51
        if s.Realtime {
13✔
52
                for {
16✔
53
                        if s.Generator == "replay" {
15✔
54
                                t.genWork()
4✔
55
                                time.Sleep(s.ReplayOffsets[t.cur])
4✔
56
                                t.cur++
4✔
57
                                if t.cur >= len(s.ReplayOffsets) {
5✔
58
                                        t.cur = 0
1✔
59
                                }
1✔
60
                        } else {
7✔
61
                                timer := time.NewTimer(time.Duration(s.Interval) * time.Second)
7✔
62
                                <-timer.C
7✔
63
                                t.genWork()
7✔
64
                        }
7✔
65
                        if t.closed {
6✔
66
                                break
×
67
                        }
68
                }
69
        }
70
        t.Done <- 1
3✔
71
}
72

73
func (t *Timer) backfill(until time.Time) {
12✔
74
        for t.S.Current.Before(until) {
121✔
75
                t.genWork()
109✔
76
                t.inc()
109✔
77
                if t.closed {
109✔
78
                        break
×
79
                }
80
        }
81
}
82

83
func (t *Timer) genWork() {
118✔
84
        s := t.S
118✔
85
        now := s.Now()
118✔
86
        var item *config.GenQueueItem
118✔
87
        useCache := t.cacheCounter > 0
118✔
88
        setCache := !useCache && t.cacheIntervals > 0
118✔
89
        t.cacheCounter--
118✔
90
        if t.cacheCounter < 0 {
196✔
91
                t.cacheCounter = t.cacheIntervals
78✔
92
        }
78✔
93
        ci := &config.CacheItem{
118✔
94
                UseCache: useCache,
118✔
95
                SetCache: setCache,
118✔
96
        }
118✔
97
        if s.Generator == "replay" {
123✔
98
                earliest := now
5✔
99
                latest := now
5✔
100
                count := 1
5✔
101
                item = &config.GenQueueItem{S: s, Count: count, Event: t.cur, Earliest: earliest, Latest: latest, Now: now, OQ: t.OQ, Cache: ci}
5✔
102
        } else {
118✔
103
                earliest := now.Add(s.EarliestParsed)
113✔
104
                latest := now.Add(s.LatestParsed)
113✔
105
                count := rater.EventRate(s, now, s.Count)
113✔
106
                item = &config.GenQueueItem{S: s, Count: count, Event: -1, Earliest: earliest, Latest: latest, Now: now, OQ: t.OQ, Cache: ci}
113✔
107
        }
113✔
108
        // log.Debugf("Placing item in queue for sample '%s': %#v", t.S.Name, item)
109
Loop1:
118✔
110
        for {
247✔
111
                select {
129✔
112
                case t.GQ <- item:
116✔
113
                        break Loop1
116✔
114
                case <-time.After(1 * time.Second):
11✔
115
                        if t.closed {
11✔
116
                                break Loop1
×
117
                        }
118
                        continue
11✔
119
                }
120
        }
121
}
122

123
func (t *Timer) inc() {
109✔
124
        s := t.S
109✔
125
        if s.Generator == "replay" {
110✔
126
                s.Current = s.Current.Add(s.ReplayOffsets[t.cur])
1✔
127
                t.cur++
1✔
128
                if t.cur >= len(s.ReplayOffsets) {
1✔
UNCOV
129
                        t.cur = 0
×
UNCOV
130
                }
×
131
        } else {
108✔
132
                s.Current = s.Current.Add(time.Duration(s.Interval) * time.Second)
108✔
133
        }
108✔
134
        if s.Wait {
109✔
135
                timer := time.NewTimer(time.Duration(s.Interval) * time.Second)
×
136
                <-timer.C
×
137
        }
×
138
}
139

140
// Close shuts down a timer
141
func (t *Timer) Close() {
1✔
142
        log.Infof("Closing timer for sample %s", t.S.Name)
1✔
143
        t.closed = true
1✔
144
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc