-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent_test.go
70 lines (62 loc) · 1.48 KB
/
concurrent_test.go
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
package flash
import (
"context"
"runtime"
"testing"
"time"
"github.com/ahab94/engine"
)
func TestConcurrent_Execute(t *testing.T) {
e := engine.NewEngine(context.TODO())
e.Start(10)
t.Parallel()
type fields struct {
executables []Executable
completion bool
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "success - Work 100 tasks - expect complete",
fields: fields{
executables: nTasks(100),
completion: true,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewConcurrent(context.TODO(), e, true)
for _, task := range tt.fields.executables {
c.Add(task)
}
if err := c.Execute(); (err != nil) != tt.wantErr {
t.Errorf("Execute() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.fields.completion != c.IsCompleted() {
t.Errorf("Execute() tasks expected to be completed but incomplete %+v", tt.fields.executables)
}
})
}
}
func BenchmarkConcurrent_Execute(b *testing.B) {
e := engine.NewEngine(context.TODO())
e.Start(100)
tasks := nTasks(1000)
b.Logf("goroutines before adding tasks %d", runtime.NumGoroutine())
c := NewConcurrent(context.TODO(), e, true)
for _, task := range tasks {
c.Add(task)
}
b.ResetTimer()
b.Logf("starting goroutines %d", runtime.NumGoroutine())
if err := c.Execute(); err != nil {
b.Errorf("Execute() error = %v", err)
}
time.Sleep(5 * time.Second)
b.Logf("ending goroutines %d", runtime.NumGoroutine())
}