MCPcopy
hub / github.com/robfig/cron / TestChainSkipIfStillRunning

Function TestChainSkipIfStillRunning

chain_test.go:155–242  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

153}
154
155func TestChainSkipIfStillRunning(t *testing.T) {
156
157 t.Run("runs immediately", func(t *testing.T) {
158 var j countJob
159 wrappedJob := NewChain(SkipIfStillRunning(DiscardLogger)).Then(&j)
160 go wrappedJob.Run()
161 time.Sleep(2 * time.Millisecond) // Give the job 2ms to complete.
162 if c := j.Done(); c != 1 {
163 t.Errorf("expected job run once, immediately, got %d", c)
164 }
165 })
166
167 t.Run("second run immediate if first done", func(t *testing.T) {
168 var j countJob
169 wrappedJob := NewChain(SkipIfStillRunning(DiscardLogger)).Then(&j)
170 go func() {
171 go wrappedJob.Run()
172 time.Sleep(time.Millisecond)
173 go wrappedJob.Run()
174 }()
175 time.Sleep(3 * time.Millisecond) // Give both jobs 3ms to complete.
176 if c := j.Done(); c != 2 {
177 t.Errorf("expected job run twice, immediately, got %d", c)
178 }
179 })
180
181 t.Run("second run skipped if first not done", func(t *testing.T) {
182 var j countJob
183 j.delay = 10 * time.Millisecond
184 wrappedJob := NewChain(SkipIfStillRunning(DiscardLogger)).Then(&j)
185 go func() {
186 go wrappedJob.Run()
187 time.Sleep(time.Millisecond)
188 go wrappedJob.Run()
189 }()
190
191 // After 5ms, the first job is still in progress, and the second job was
192 // aleady skipped.
193 time.Sleep(5 * time.Millisecond)
194 started, done := j.Started(), j.Done()
195 if started != 1 || done != 0 {
196 t.Error("expected first job started, but not finished, got", started, done)
197 }
198
199 // Verify that the first job completes and second does not run.
200 time.Sleep(25 * time.Millisecond)
201 started, done = j.Started(), j.Done()
202 if started != 1 || done != 1 {
203 t.Error("expected second job skipped, got", started, done)
204 }
205 })
206
207 t.Run("skip 10 jobs on rapid fire", func(t *testing.T) {
208 var j countJob
209 j.delay = 10 * time.Millisecond
210 wrappedJob := NewChain(SkipIfStillRunning(DiscardLogger)).Then(&j)
211 for i := 0; i < 11; i++ {
212 go wrappedJob.Run()

Callers

nothing calls this directly

Calls 7

ThenMethod · 0.95
DoneMethod · 0.95
StartedMethod · 0.95
NewChainFunction · 0.85
SkipIfStillRunningFunction · 0.85
RunMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected