| 1 |
/* |
| 2 |
Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
or more contributor license agreements. See the NOTICE file |
| 4 |
distributed with this work for additional information |
| 5 |
regarding copyright ownership. The ASF licenses this file |
| 6 |
to you under the Apache License, Version 2.0 (the |
| 7 |
"License"); you may not use this file except in compliance |
| 8 |
with the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, |
| 13 |
software distributed under the License is distributed on an |
| 14 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
KIND, either express or implied. See the License for the |
| 16 |
specific language governing permissions and limitations |
| 17 |
under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
// Tests to verify that example code behaves as expected. |
| 21 |
// Run in this directory with `go test example_test.go` |
| 22 |
// |
| 23 |
package main |
| 24 |
|
| 25 |
import ( |
| 26 |
"bufio" |
| 27 |
"bytes" |
| 28 |
"flag" |
| 29 |
"fmt" |
| 30 |
"io" |
| 31 |
"math/rand" |
| 32 |
"net" |
| 33 |
"os" |
| 34 |
"os/exec" |
| 35 |
"path" |
| 36 |
"path/filepath" |
| 37 |
"reflect" |
| 38 |
"testing" |
| 39 |
"time" |
| 40 |
) |
| 41 |
|
| 42 |
func fatalIf(t *testing.T, err error) { |
| 43 |
if err != nil { |
| 44 |
t.Fatalf("%s", err) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// A demo broker process |
| 49 |
type broker struct { |
| 50 |
cmd *exec.Cmd |
| 51 |
addr string |
| 52 |
runerr chan error |
| 53 |
err error |
| 54 |
} |
| 55 |
|
| 56 |
// Try to connect to the broker to verify it is ready, give up after a timeout |
| 57 |
func (b *broker) check() error { |
| 58 |
dialer := net.Dialer{Deadline: time.Now().Add(time.Second * 10)} |
| 59 |
for { |
| 60 |
c, err := dialer.Dial("tcp", b.addr) |
| 61 |
if err == nil { // Success |
| 62 |
c.Close() |
| 63 |
return nil |
| 64 |
} |
| 65 |
select { |
| 66 |
case runerr := <-b.runerr: // Broker exited. |
| 67 |
return runerr |
| 68 |
default: |
| 69 |
} |
| 70 |
if neterr, ok := err.(net.Error); ok && neterr.Timeout() { // Running but timed out |
| 71 |
b.stop() |
| 72 |
return fmt.Errorf("timed out waiting for broker") |
| 73 |
} |
| 74 |
time.Sleep(time.Second / 10) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Start the demo broker, wait till it is listening on *addr. No-op if already started. |
| 79 |
func (b *broker) start(t *testing.T) error { |
| 80 |
if b.cmd == nil { // Not already started |
| 81 |
b.addr = fmt.Sprintf("127.0.0.1:%d", rand.Intn(10000)+10000) |
| 82 |
b.cmd = exampleCommand(t, *brokerName, "-addr", b.addr) |
| 83 |
b.runerr = make(chan error) |
| 84 |
b.cmd.Stderr, b.cmd.Stdout = os.Stderr, os.Stdout |
| 85 |
b.err = b.cmd.Start() |
| 86 |
if b.err == nil { |
| 87 |
go func() { b.runerr <- b.cmd.Wait() }() |
| 88 |
} else { |
| 89 |
b.runerr <- b.err |
| 90 |
} |
| 91 |
b.err = b.check() |
| 92 |
} |
| 93 |
return b.err |
| 94 |
} |
| 95 |
|
| 96 |
func (b *broker) stop() { |
| 97 |
if b != nil && b.cmd != nil { |
| 98 |
b.cmd.Process.Kill() |
| 99 |
<-b.runerr |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func checkEqual(want interface{}, got interface{}) error { |
| 104 |
if reflect.DeepEqual(want, got) { |
| 105 |
return nil |
| 106 |
} |
| 107 |
return fmt.Errorf("%#v != %#v", want, got) |
| 108 |
} |
| 109 |
|
| 110 |
// exampleCommand returns an exec.Cmd to run an example. |
| 111 |
func exampleCommand(t *testing.T, prog string, arg ...string) (cmd *exec.Cmd) { |
| 112 |
args := []string{} |
| 113 |
if *debug { |
| 114 |
args = append(args, "-debug=true") |
| 115 |
} |
| 116 |
args = append(args, arg...) |
| 117 |
prog, err := filepath.Abs(path.Join(*dir, prog)) |
| 118 |
fatalIf(t, err) |
| 119 |
if _, err := os.Stat(prog); err == nil { |
| 120 |
cmd = exec.Command(prog, args...) |
| 121 |
} else if _, err := os.Stat(prog + ".go"); err == nil { |
| 122 |
args = append([]string{"run", prog + ".go"}, args...) |
| 123 |
cmd = exec.Command("go", args...) |
| 124 |
} else { |
| 125 |
t.Fatalf("Cannot find binary or source for %s", prog) |
| 126 |
} |
| 127 |
cmd.Stderr = os.Stderr |
| 128 |
return cmd |
| 129 |
} |
| 130 |
|
| 131 |
// Run an example Go program, return the combined output as a string. |
| 132 |
func runExample(t *testing.T, prog string, arg ...string) (string, error) { |
| 133 |
cmd := exampleCommand(t, prog, arg...) |
| 134 |
out, err := cmd.Output() |
| 135 |
return string(out), err |
| 136 |
} |
| 137 |
|
| 138 |
func prefix(prefix string, err error) error { |
| 139 |
if err != nil { |
| 140 |
return fmt.Errorf("%s: %s", prefix, err) |
| 141 |
} |
| 142 |
return nil |
| 143 |
} |
| 144 |
|
| 145 |
func runExampleWant(t *testing.T, want string, prog string, args ...string) error { |
| 146 |
out, err := runExample(t, prog, args...) |
| 147 |
if err != nil { |
| 148 |
return fmt.Errorf("%s failed: %s: %s", prog, err, out) |
| 149 |
} |
| 150 |
return prefix(prog, checkEqual(want, out)) |
| 151 |
} |
| 152 |
|
| 153 |
func exampleArgs(args ...string) []string { |
| 154 |
for i := 0; i < *connections; i++ { |
| 155 |
args = append(args, fmt.Sprintf("amqp://%s/%s%d", testBroker.addr, "q", i)) |
| 156 |
} |
| 157 |
return args |
| 158 |
} |
| 159 |
|
| 160 |
// Send then receive |
| 161 |
func TestExampleSendReceive(t *testing.T) { |
| 162 |
if testing.Short() { |
| 163 |
t.Skip("Skip demo tests in short mode") |
| 164 |
} |
| 165 |
testBroker.start(t) |
| 166 |
err := runExampleWant(t, |
| 167 |
fmt.Sprintf("Received all %d acknowledgements\n", expected), |
| 168 |
"send", |
| 169 |
exampleArgs("-count", fmt.Sprintf("%d", *count))...) |
| 170 |
if err != nil { |
| 171 |
t.Fatal(err) |
| 172 |
} |
| 173 |
err = runExampleWant(t, |
| 174 |
fmt.Sprintf("Listening on %v connections\nReceived %v messages\n", *connections, *count**connections), |
| 175 |
"receive", |
| 176 |
exampleArgs("-count", fmt.Sprintf("%d", *count**connections))...) |
| 177 |
if err != nil { |
| 178 |
t.Fatal(err) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
var ready error |
| 183 |
|
| 184 |
func init() { ready = fmt.Errorf("Ready") } |
| 185 |
|
| 186 |
// Run receive in a goroutine. |
| 187 |
// Send ready on errchan when it is listening. |
| 188 |
// Send final error when it is done. |
| 189 |
// Returns the Cmd, caller must Wait() |
| 190 |
func goReceiveWant(t *testing.T, errchan chan<- error, want string, arg ...string) *exec.Cmd { |
| 191 |
cmd := exampleCommand(t, "receive", arg...) |
| 192 |
go func() { |
| 193 |
pipe, err := cmd.StdoutPipe() |
| 194 |
if err != nil { |
| 195 |
errchan <- err |
| 196 |
return |
| 197 |
} |
| 198 |
out := bufio.NewReader(pipe) |
| 199 |
cmd.Start() |
| 200 |
line, err := out.ReadString('\n') |
| 201 |
if err != nil && err != io.EOF { |
| 202 |
errchan <- err |
| 203 |
return |
| 204 |
} |
| 205 |
listening := "Listening on 3 connections\n" |
| 206 |
if line != listening { |
| 207 |
errchan <- checkEqual(listening, line) |
| 208 |
return |
| 209 |
} |
| 210 |
errchan <- ready |
| 211 |
buf := bytes.Buffer{} |
| 212 |
io.Copy(&buf, out) // Collect the rest of the output |
| 213 |
cmd.Wait() |
| 214 |
errchan <- checkEqual(want, buf.String()) |
| 215 |
close(errchan) |
| 216 |
}() |
| 217 |
return cmd |
| 218 |
} |
| 219 |
|
| 220 |
// Start receiver first, wait till it is running, then send. |
| 221 |
func TestExampleReceiveSend(t *testing.T) { |
| 222 |
if testing.Short() { |
| 223 |
t.Skip("Skip demo tests in short mode") |
| 224 |
} |
| 225 |
testBroker.start(t) |
| 226 |
|
| 227 |
// Start receiver, wait for "listening" message on stdout |
| 228 |
recvCmd := exampleCommand(t, "receive", exampleArgs(fmt.Sprintf("-count=%d", expected))...) |
| 229 |
pipe, err := recvCmd.StdoutPipe() |
| 230 |
if err != nil { |
| 231 |
t.Fatal(err) |
| 232 |
} |
| 233 |
recvCmd.Start() |
| 234 |
out := bufio.NewReader(pipe) |
| 235 |
line, err := out.ReadString('\n') |
| 236 |
if err := checkEqual("Listening on 3 connections\n", line); err != nil { |
| 237 |
t.Fatal(err) |
| 238 |
} |
| 239 |
|
| 240 |
if err := runExampleWant(t, |
| 241 |
fmt.Sprintf("Received all %d acknowledgements\n", expected), |
| 242 |
"send", |
| 243 |
exampleArgs("-count", fmt.Sprintf("%d", *count))...); err != nil { |
| 244 |
t.Fatal(err) |
| 245 |
} |
| 246 |
|
| 247 |
buf := bytes.Buffer{} |
| 248 |
io.Copy(&buf, out) |
| 249 |
if err := checkEqual(fmt.Sprintf("Received %d messages\n", expected), buf.String()); err != nil { |
| 250 |
t.Fatal(err) |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
var testBroker *broker |
| 255 |
|
| 256 |
var debug = flag.Bool("debug", false, "Debugging output from examples") |
| 257 |
var brokerName = flag.String("broker", "broker", "Name of broker executable to run") |
| 258 |
var count = flag.Int("count", 3, "Count of messages to send in tests") |
| 259 |
var connections = flag.Int("connections", 3, "Number of connections to make in tests") |
| 260 |
var dir = flag.String("dir", "electron", "Directory containing example sources or binaries") |
| 261 |
var expected int |
| 262 |
|
| 263 |
func TestMain(m *testing.M) { |
| 264 |
expected = (*count) * (*connections) |
| 265 |
rand.Seed(time.Now().UTC().UnixNano()) |
| 266 |
testBroker = &broker{} // Broker is started on-demand by tests. |
| 267 |
status := m.Run() |
| 268 |
testBroker.stop() |
| 269 |
os.Exit(status) |
| 270 |
} |