-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_test.go
More file actions
181 lines (161 loc) · 4.43 KB
/
Copy pathecho_test.go
File metadata and controls
181 lines (161 loc) · 4.43 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bytes"
"io"
"log"
"testing"
"time"
"github.com/agentio/echo-go/internal/commands"
)
// Socket gRPC
func TestSocketGrpcServiceGrpcClient(t *testing.T) {
socket := "@echotestgrpc"
test_service(t,
[]string{"serve", "grpc", "--socket", socket},
[]string{"--address", "unix:" + socket, "--stack", "grpc"},
)
}
func TestSocketGrpcServiceConnectGrpcClient(t *testing.T) {
const socket = "@echotestgrpc1"
test_service(t,
[]string{"serve", "grpc", "--socket", socket},
[]string{"--address", "unix:" + socket, "--stack", "connect-grpc"},
)
}
// Socket Connect
func TestSocketConnectServiceGrpcClient(t *testing.T) {
socket := "@echotestconnect"
test_service(t,
[]string{"serve", "grpc", "--socket", socket},
[]string{"--address", "unix:" + socket, "--stack", "grpc"},
)
}
func TestSocketConnectServiceConnectClient(t *testing.T) {
const socket = "@echotestconnect1"
test_service(t,
[]string{"serve", "connect", "--socket", socket},
[]string{"--address", "unix:" + socket, "--stack", "connect"},
)
}
func TestSocketConnectServiceConnectGrpcClient(t *testing.T) {
const socket = "@echotestconnect2"
test_service(t,
[]string{"serve", "connect", "--socket", socket},
[]string{"--address", "unix:" + socket, "--stack", "connect-grpc"},
)
}
func TestSocketConnectServiceConnectGrpcWebClient(t *testing.T) {
const socket = "@echotestconnect3"
test_service(t,
[]string{"serve", "connect", "--socket", socket},
[]string{"--address", "unix:" + socket, "--stack", "connect-grpc-web"},
)
}
// Local gRPC
func TestLocalGrpcServiceGrpcClient(t *testing.T) {
const port = "19876"
test_service(t,
[]string{"serve", "grpc", "--port", port},
[]string{"--address", "localhost:" + port, "--stack", "grpc"},
)
}
func TestLocalGrpcServiceConnectGrpcClient(t *testing.T) {
const port = "19875"
test_service(t,
[]string{"serve", "grpc", "--port", port},
[]string{"--address", "localhost:" + port, "--stack", "connect-grpc"},
)
}
// Local Connect
func TestLocalConnectServiceGrpcClient(t *testing.T) {
const port = "19874"
test_service(t,
[]string{"serve", "connect", "--port", port},
[]string{"--address", "localhost:" + port, "--stack", "grpc"},
)
}
func TestLocalConnectServiceConnectClient(t *testing.T) {
const port = "19873"
test_service(t,
[]string{"serve", "connect", "--port", port},
[]string{"--address", "localhost:" + port, "--stack", "connect"},
)
}
func TestLocalConnectServiceConnectGrpcClient(t *testing.T) {
const port = "19872"
test_service(t,
[]string{"serve", "connect", "--port", port},
[]string{"--address", "localhost:" + port, "--stack", "connect-grpc"},
)
}
func TestLocalConnectServiceConnectGrpcWebClient(t *testing.T) {
const port = "19871"
test_service(t,
[]string{"serve", "connect", "--port", port},
[]string{"--address", "localhost:" + port, "--stack", "connect-grpc-web"},
)
}
func test_service(t *testing.T, serverArgs, clientArgs []string) {
go func() {
serveCmd := commands.Cmd()
serveCmd.SetArgs(serverArgs)
err := serveCmd.Execute()
if err != nil {
log.Printf("failed to read output from buffer: %v", err)
}
}()
time.Sleep(100 * time.Millisecond)
tests := []struct {
Args []string
Expected string
}{
{
Args: []string{"call", "get"},
Expected: expected_get,
},
{
Args: []string{"call", "collect"},
Expected: expected_collect,
},
{
Args: []string{"call", "expand"},
Expected: expected_expand,
},
{
Args: []string{"call", "update"},
Expected: expected_update,
},
}
for _, test := range tests {
cmd := commands.Cmd()
buffer := new(bytes.Buffer)
cmd.SetOut(buffer)
cmd.SetArgs(append(test.Args, clientArgs...))
err := cmd.Execute()
if err != nil {
t.Errorf("%s", err)
}
out, err := io.ReadAll(buffer)
if err != nil {
t.Fatalf("failed to read output: %v", err)
}
if string(out) != test.Expected {
t.Errorf("expected %q, got %q", test.Expected, string(out))
}
}
}
const expected_get = `{"text":"Go echo get: hello"}
`
const expected_collect = `{"text":"Go echo collect: hello 0 hello 1 hello 2"}
`
const expected_expand = `{"text":"Go echo expand (0): 1"}
{"text":"Go echo expand (1): 2"}
{"text":"Go echo expand (2): 3"}
`
const expected_update = `{"text":"Go echo update (1): hello 0"}
{"text":"Go echo update (2): hello 1"}
{"text":"Go echo update (3): hello 2"}
{"text":"Go echo update (4): hello 3"}
{"text":"Go echo update (5): hello 4"}
{"text":"Go echo update (6): hello 5"}
`