-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler_test.go
90 lines (82 loc) · 2.03 KB
/
scheduler_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
88
89
90
package eventloop_test
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
log "github.com/sirupsen/logrus"
"github.com/sghaida/go-stuff/src/eventloop"
)
func Test_Scheduler(t *testing.T) {
t.Parallel()
el := eventloop.InitializeEventLoop()
el.Start()
t.Run("emit couple of events", func(t *testing.T) {
channels := make([]chan eventloop.EventReply, 0, 100)
lock := new(sync.RWMutex)
wg := new(sync.WaitGroup)
wg.Add(100)
for i := 0; i < 100; i++ {
go func(index int) {
ch := make(chan eventloop.EventReply, 1)
lock.Lock()
channels = append(channels, ch)
lock.Unlock()
event := eventloop.Event{
EventType: eventloop.RequireFeedback,
Channel: ch,
}
el.Emmit(event, func() eventloop.EventReply {
return eventloop.EventReply{
Payload: fmt.Sprintf("event - %d", index),
Error: nil,
}
})
go func(ev eventloop.Event) {
t := time.Duration(rand.Intn(100))
time.Sleep(t * time.Millisecond)
result := <-ev.Channel
log.Infof(result.Payload.(string))
wg.Done()
}(event)
}(i)
}
wg.Wait()
})
t.Run("emit another couple of events", func(t *testing.T) {
channels := make([]chan eventloop.EventReply, 0, 100)
lock := new(sync.RWMutex)
wg := new(sync.WaitGroup)
wg.Add(100)
for i := 0; i < 100; i++ {
go func(index int) {
ch := make(chan eventloop.EventReply, 1)
lock.Lock()
channels = append(channels, ch)
lock.Unlock()
event := eventloop.Event{
// no feedback is not being checked for the time being
EventType: eventloop.NoFeedback,
Channel: ch,
}
el.Emmit(event, func() eventloop.EventReply {
return eventloop.EventReply{
Payload: struct {
Name string
}{fmt.Sprintf("name: %d", index)},
Error: nil,
}
})
go func(ev eventloop.Event) {
t := time.Duration(rand.Intn(100))
time.Sleep(t * time.Millisecond)
result := <-ev.Channel
log.Info(result.Payload.(struct{ Name string }))
wg.Done()
}(event)
}(i)
}
wg.Wait()
})
}