-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathperiodic_thread_test.go
More file actions
60 lines (46 loc) · 1.1 KB
/
Copy pathperiodic_thread_test.go
File metadata and controls
60 lines (46 loc) · 1.1 KB
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
package threads
import (
"context"
"runtime/debug"
"sync"
"testing"
"time"
"github.com/tokenized/logger"
)
func Test_PeriodicThread_Complete(t *testing.T) {
ctx := context.Background()
t.Logf("Starting")
defer t.Logf("Completed")
defer func() {
if err := recover(); err != nil {
t.Errorf("Top level panic : %s : %s", err, string(debug.Stack()))
}
}()
thread := NewPeriodicThread("Test Thread", periodicFunction, time.Millisecond*100)
var wait sync.WaitGroup
thread.SetWait(&wait)
complete := thread.GetCompleteChannel()
thread.Start(ctx)
select {
case <-complete:
t.Errorf("Thread completed")
case <-time.After(time.Millisecond * 200):
t.Logf("Thread correctly did not complete")
}
thread.Stop(ctx)
select {
case <-complete:
t.Logf("Thread completed")
case <-time.After(time.Millisecond * 50):
t.Errorf("Thread should have stopped")
}
wait.Wait()
threadErr := thread.Error()
if threadErr != nil {
t.Errorf("Thread should not return error : %s", threadErr)
}
}
func periodicFunction(ctx context.Context) error {
logger.Info(ctx, "Periodic executed")
return nil
}