forked from jacexh/ultron
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslave_agent_test.go
55 lines (47 loc) · 1.34 KB
/
slave_agent_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
package ultron
import (
"fmt"
"sync"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/wosai/ultron/v2/pkg/genproto"
"go.uber.org/zap"
)
func TestSlaveAgent_close(t *testing.T) {
agent := newSlaveAgent(&genproto.SubscribeRequest{SlaveId: uuid.NewString()})
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
agent.send(&genproto.SubscribeResponse{Type: genproto.EventType_PING})
if err := agent.close(); err != nil {
Logger.Error("no consumer for the slave agent", zap.Error(err))
}
}()
}
wg.Wait()
}
func TestSlaveAgent_ID(t *testing.T) {
agent := newSlaveAgent(&genproto.SubscribeRequest{SlaveId: uuid.NewString()})
assert.True(t, agent.ID() != "")
extras := agent.Extras()
assert.EqualValues(t, len(extras), 0)
}
func TestSlaveAgent_closeTimeout(t *testing.T) {
agent := newSlaveAgent(&genproto.SubscribeRequest{SlaveId: uuid.NewString()})
agent.send(&genproto.SubscribeResponse{})
err := agent.close()
assert.NotNil(t, err)
assert.EqualValues(t, err.Error(), fmt.Sprintf("the input channel is blocked: %s", agent.ID()))
}
func TestSlaveAgent_keepAliveOut(t *testing.T) {
agent := newSlaveAgent(&genproto.SubscribeRequest{SlaveId: uuid.NewString()})
go func() {
<-time.After(1 * time.Second)
agent.close()
}()
agent.keepAlives()
}