forked from bluenviron/goroslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleactionserver_test.go
116 lines (99 loc) · 2.47 KB
/
simpleactionserver_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
104
105
106
107
108
109
110
111
112
113
114
115
116
package goroslib
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestSimpleActionServer(t *testing.T) {
for _, client := range []string{
"cpp",
"go",
} {
t.Run(client, func(t *testing.T) {
m, err := newContainerMaster()
require.NoError(t, err)
defer m.close()
ns, err := NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib-server",
MasterAddress: m.IP() + ":11311",
})
require.NoError(t, err)
defer ns.Close()
sas, err := NewSimpleActionServer(SimpleActionServerConf{
Node: ns,
Name: "test_action",
Action: &DoSomethingAction{},
OnExecute: func(sas *SimpleActionServer, goal *DoSomethingActionGoal) {
if goal.Input == 3 {
return
}
time.Sleep(500 * time.Millisecond)
sas.PublishFeedback(&DoSomethingActionFeedback{
PercentComplete: 0.5,
})
time.Sleep(500 * time.Millisecond)
if goal.Input == 2 {
sas.SetAborted(&DoSomethingActionResult{})
return
}
sas.SetSucceeded(&DoSomethingActionResult{
Output: 123456,
})
},
})
require.NoError(t, err)
defer sas.Close()
switch client {
case "cpp":
c, err := newContainer("node-simpleactionclient", m.IP())
require.NoError(t, err)
defer c.close()
require.Equal(t, "0.50\nSUCCEEDED\n123456\n", c.waitOutput())
case "go":
nc, err := NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib-client",
MasterAddress: m.IP() + ":11311",
})
require.NoError(t, err)
defer nc.Close()
sac, err := NewSimpleActionClient(SimpleActionClientConf{
Node: nc,
Name: "test_action",
Action: &DoSomethingAction{},
})
require.NoError(t, err)
defer sac.Close()
sac.WaitForServer()
activeDone := make(chan struct{})
fbDone := make(chan struct{})
doneDone := make(chan struct{})
err = sac.SendGoal(SimpleActionClientGoalConf{
Goal: &DoSomethingActionGoal{
Input: 1234312,
},
OnDone: func(res *DoSomethingActionResult) {
require.Equal(t, &DoSomethingActionResult{
Output: 123456,
}, res)
close(doneDone)
},
OnActive: func() {
close(activeDone)
},
OnFeedback: func(fb *DoSomethingActionFeedback) {
require.Equal(t, &DoSomethingActionFeedback{
PercentComplete: 0.5,
}, fb)
close(fbDone)
},
})
require.NoError(t, err)
<-activeDone
<-fbDone
<-doneDone
}
})
}
}