-
Notifications
You must be signed in to change notification settings - Fork 62
/
node_log_test.go
103 lines (85 loc) · 1.96 KB
/
node_log_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
91
92
93
94
95
96
97
98
99
100
101
102
103
package goroslib
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/bluenviron/goroslib/v2/pkg/msgs/rosgraph_msgs"
)
func TestNodeLog(t *testing.T) {
t.Run("rosout", func(t *testing.T) {
m := newContainerMaster(t)
defer m.close()
n1, err := NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib1",
MasterAddress: m.IP() + ":11311",
LogLevel: LogLevelDebug,
})
require.NoError(t, err)
defer n1.Close()
n2, err := NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib2",
MasterAddress: m.IP() + ":11311",
})
require.NoError(t, err)
defer n2.Close()
recv := 0
done := make(chan struct{})
sub, err := NewSubscriber(SubscriberConf{
Node: n2,
Topic: "/rosout",
Callback: func(msg *rosgraph_msgs.Log) {
switch msg.Level {
case rosgraph_msgs.Log_INFO,
rosgraph_msgs.Log_WARN,
rosgraph_msgs.Log_ERROR,
rosgraph_msgs.Log_FATAL:
recv++
if recv >= 4 {
close(done)
}
}
},
})
require.NoError(t, err)
defer sub.Close()
time.Sleep(500 * time.Millisecond)
n1.Log(LogLevelInfo, "test info")
n1.Log(LogLevelWarn, "test warn")
n1.Log(LogLevelError, "test error")
n1.Log(LogLevelFatal, "test fatal")
<-done
})
t.Run("callback", func(t *testing.T) {
m := newContainerMaster(t)
defer m.close()
recv := 0
done := make(chan struct{})
n, err := NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib",
MasterAddress: m.IP() + ":11311",
LogLevel: LogLevelDebug,
OnLog: func(level LogLevel, _ string) {
switch level {
case LogLevelInfo,
LogLevelWarn,
LogLevelError,
LogLevelFatal:
recv++
if recv >= 4 {
close(done)
}
}
},
})
require.NoError(t, err)
defer n.Close()
n.Log(LogLevelInfo, "test info")
n.Log(LogLevelWarn, "test warn")
n.Log(LogLevelError, "test error")
n.Log(LogLevelFatal, "test fatal")
<-done
})
}