-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaistry_test.go
87 lines (69 loc) · 1.59 KB
/
maistry_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package maistry_test
import (
"github.com/stretchr/testify/assert"
"maistry"
"testing"
"time"
)
type TestClient struct {
jobManager *maistry.JobManager
internalFluPerSec int
maxClientQps int
waitMiliSec int
name string
}
var count1 int
var count2 int
type dummyLogger struct {
}
func (d dummyLogger) Error(message string, err error, args map[string]interface{}) {
}
func (d dummyLogger) Trace(message string, args map[string]interface{}) {
}
func TestDispatcher_Start(t *testing.T) {
clients := []TestClient{
{
jobManager: maistry.NewJobManager(1000, "1"),
internalFluPerSec: 100,
maxClientQps: 2,
waitMiliSec: 200,
name: "1",
},
{
jobManager: maistry.NewJobManager(1, "2"),
internalFluPerSec: 10,
maxClientQps: 10,
waitMiliSec: 300,
name: "2",
},
}
dispatcher := maistry.NewDispatcher(1, dummyLogger{})
for _, c := range clients {
dispatcher.AddJobManager(c.jobManager)
c.jobManager.Run()
}
dispatcher.Start()
for _, c := range clients {
SendData(c)
}
time.Sleep(time.Second * time.Duration(5))
assert.True(t, count1 > 16 && count1 < 20)
assert.True(t, count2 > 3 && count2 < 6)
}
func SendData(c TestClient) {
go func() {
ticker := time.Tick(time.Duration(1000/c.internalFluPerSec) * time.Millisecond)
for {
<-ticker
c.jobManager.PushJob(maistry.NewJob(func() {
time.Sleep(time.Duration(c.waitMiliSec) * time.Millisecond)
if c.name == "1" {
count1++
}
if c.name == "2" {
count2++
}
}, dummyLogger{}))
}
}()
}