| 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 |
package electron |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"testing" |
| 25 |
"time" |
| 26 |
|
| 27 |
"github.com/apache/qpid-proton/go/pkg/amqp" |
| 28 |
"github.com/apache/qpid-proton/go/pkg/internal/test" |
| 29 |
) |
| 30 |
|
| 31 |
// Send a message one way with a client sender and server receiver, verify ack. |
| 32 |
func TestClientSender(t *testing.T) { |
| 33 |
p := newPipe(t, nil, nil) |
| 34 |
defer func() { p.close() }() |
| 35 |
|
| 36 |
nLinks := 3 |
| 37 |
nMessages := 3 |
| 38 |
|
| 39 |
s := make([]Sender, nLinks) |
| 40 |
r := make([]Receiver, nLinks) |
| 41 |
for i := 0; i < nLinks; i++ { |
| 42 |
s[i], r[i] = p.sender(Target(fmt.Sprintf("foo%d", i))) |
| 43 |
} |
| 44 |
|
| 45 |
for i := 0; i < nLinks; i++ { |
| 46 |
for j := 0; j < nMessages; j++ { |
| 47 |
// Client send |
| 48 |
ack := make(chan Outcome, 1) |
| 49 |
sendDone := make(chan struct{}) |
| 50 |
go func() { |
| 51 |
defer close(sendDone) |
| 52 |
m := amqp.NewMessageWith(fmt.Sprintf("foobar%v-%v", i, j)) |
| 53 |
var err error |
| 54 |
s[i].SendAsync(m, ack, "testing") |
| 55 |
test.FatalIf(t, err) |
| 56 |
}() |
| 57 |
|
| 58 |
// Server receive |
| 59 |
rm, err := r[i].Receive() |
| 60 |
test.FatalIf(t, err) |
| 61 |
if want, got := interface{}(fmt.Sprintf("foobar%v-%v", i, j)), rm.Message.Body(); want != got { |
| 62 |
t.Errorf("%#v != %#v", want, got) |
| 63 |
} |
| 64 |
|
| 65 |
// Should not be acknowledged on client yet |
| 66 |
<-sendDone |
| 67 |
select { |
| 68 |
case <-ack: |
| 69 |
t.Errorf("unexpected ack") |
| 70 |
default: |
| 71 |
} |
| 72 |
|
| 73 |
// Server send ack |
| 74 |
if err = rm.Reject(); err != nil { |
| 75 |
t.Error(err) |
| 76 |
} |
| 77 |
// Client get ack. |
| 78 |
if a := <-ack; a.Value != "testing" || a.Error != nil || a.Status != Rejected { |
| 79 |
t.Error("unexpected ack: ", a.Status, a.Error, a.Value) |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func TestClientReceiver(t *testing.T) { |
| 86 |
nMessages := 3 |
| 87 |
p := newPipe(t, nil, nil) |
| 88 |
defer func() { p.close() }() |
| 89 |
r, s := p.receiver(Source("foo"), Capacity(nMessages), Prefetch(true)) |
| 90 |
go func() { |
| 91 |
for i := 0; i < nMessages; i++ { // Server sends |
| 92 |
out := s.SendSync(amqp.NewMessageWith(int32(i))) |
| 93 |
test.FatalIf(t, out.Error) |
| 94 |
} |
| 95 |
}() |
| 96 |
for i := 0; i < nMessages; i++ { // Client receives |
| 97 |
rm, err := r.Receive() |
| 98 |
test.FatalIf(t, err) |
| 99 |
test.ErrorIf(t, test.Differ(int32(i), rm.Message.Body())) |
| 100 |
test.ErrorIf(t, rm.Accept()) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Test timeout versions of waiting functions. |
| 105 |
func TestTimeouts(t *testing.T) { |
| 106 |
p := newPipe(t, nil, nil) |
| 107 |
defer func() { p.close() }() |
| 108 |
snd, rcv := p.sender(Target("test")) |
| 109 |
|
| 110 |
// Test send with timeout |
| 111 |
short := time.Millisecond |
| 112 |
long := time.Second |
| 113 |
m := amqp.NewMessage() |
| 114 |
if err := snd.SendSyncTimeout(m, 0).Error; err != Timeout { // No credit, expect timeout. |
| 115 |
t.Error("want Timeout got", err) |
| 116 |
} |
| 117 |
if err := snd.SendSyncTimeout(m, short).Error; err != Timeout { // No credit, expect timeout. |
| 118 |
t.Error("want Timeout got", err) |
| 119 |
} |
| 120 |
// Test receive with timeout |
| 121 |
if _, err := rcv.ReceiveTimeout(0); err != Timeout { // No credit, expect timeout. |
| 122 |
t.Error("want Timeout got", err) |
| 123 |
} |
| 124 |
// Test receive with timeout |
| 125 |
if _, err := rcv.ReceiveTimeout(short); err != Timeout { // No credit, expect timeout. |
| 126 |
t.Error("want Timeout got", err) |
| 127 |
} |
| 128 |
// There is now a credit on the link due to receive |
| 129 |
ack := make(chan Outcome) |
| 130 |
snd.SendAsyncTimeout(m, ack, nil, short) |
| 131 |
// Disposition should timeout |
| 132 |
select { |
| 133 |
case <-ack: |
| 134 |
t.Errorf("want Timeout got %#v", ack) |
| 135 |
case <-time.After(short): |
| 136 |
} |
| 137 |
|
| 138 |
// Receive and accept |
| 139 |
rm, err := rcv.ReceiveTimeout(long) |
| 140 |
if err != nil { |
| 141 |
t.Fatal(err) |
| 142 |
} |
| 143 |
if err = rm.Accept(); err != nil { |
| 144 |
t.Fatal(err) |
| 145 |
} |
| 146 |
// Sender get ack |
| 147 |
if a := <-ack; a.Status != Accepted || a.Error != nil { |
| 148 |
t.Errorf("want (accepted, nil) got %#v", a) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
type result struct { |
| 153 |
label string |
| 154 |
err error |
| 155 |
value interface{} |
| 156 |
} |
| 157 |
|
| 158 |
func (r result) String() string { return fmt.Sprintf("%v(%v)", r.err, r.label) } |
| 159 |
|
| 160 |
func doSend(snd Sender, results chan result) { |
| 161 |
err := snd.SendSync(amqp.NewMessage()).Error |
| 162 |
results <- result{"send", err, nil} |
| 163 |
} |
| 164 |
|
| 165 |
func doReceive(rcv Receiver, results chan result) { |
| 166 |
msg, err := rcv.Receive() |
| 167 |
results <- result{"receive", err, msg} |
| 168 |
} |
| 169 |
|
| 170 |
func doDisposition(ack <-chan Outcome, results chan result) { |
| 171 |
results <- result{"disposition", (<-ack).Error, nil} |
| 172 |
} |
| 173 |
|
| 174 |
// Senders get credit immediately if receivers have prefetch set |
| 175 |
func TestSendReceivePrefetch(t *testing.T) { |
| 176 |
p := newPipe(t, nil, nil) |
| 177 |
p.prefetch = true |
| 178 |
s, r := p.sender() |
| 179 |
s.SendAsyncTimeout(amqp.NewMessage(), nil, nil, time.Second) // Should not block for credit. |
| 180 |
if _, err := r.Receive(); err != nil { |
| 181 |
t.Error(err) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// Senders do not get credit till Receive() if receivers don't have prefetch |
| 186 |
func TestSendReceiveNoPrefetch(t *testing.T) { |
| 187 |
p := newPipe(t, nil, nil) |
| 188 |
p.prefetch = false |
| 189 |
s, r := p.sender() |
| 190 |
done := make(chan struct{}, 1) |
| 191 |
go func() { |
| 192 |
s.SendAsyncTimeout(amqp.NewMessage(), nil, nil, time.Second) // Should block for credit. |
| 193 |
close(done) |
| 194 |
}() |
| 195 |
select { |
| 196 |
case <-done: |
| 197 |
t.Errorf("send should be blocked on credit") |
| 198 |
default: |
| 199 |
if _, err := r.Receive(); err != nil { |
| 200 |
t.Error(err) |
| 201 |
} else { |
| 202 |
<-done |
| 203 |
} // Should be unblocked now |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
// Test that closing Links interrupts blocked link functions. |
| 208 |
func TestLinkCloseInterrupt(t *testing.T) { |
| 209 |
want := amqp.Error{Name: "x", Description: "all bad"} |
| 210 |
p := newPipe(t, nil, nil) |
| 211 |
results := make(chan result) // Collect expected errors |
| 212 |
|
| 213 |
// Note closing the link does not interrupt Send() calls, the AMQP spec says |
| 214 |
// that deliveries can be settled after the link is closed. |
| 215 |
|
| 216 |
// Receiver.Close() interrupts Receive() |
| 217 |
snd, rcv := p.sender() |
| 218 |
go doReceive(rcv, results) |
| 219 |
rcv.Close(want) |
| 220 |
if r := <-results; want != r.err { |
| 221 |
t.Errorf("want %#v got %#v", want, r) |
| 222 |
} |
| 223 |
|
| 224 |
// Remote Sender.Close() interrupts Receive() |
| 225 |
snd, rcv = p.sender() |
| 226 |
go doReceive(rcv, results) |
| 227 |
snd.Close(want) |
| 228 |
if r := <-results; want != r.err { |
| 229 |
t.Errorf("want %#v got %#v", want, r) |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
// Test closing the server end of a connection. |
| 234 |
func TestConnectionCloseInterrupt1(t *testing.T) { |
| 235 |
want := amqp.Error{Name: "x", Description: "bad"} |
| 236 |
p := newSocketPair(t, nil, nil) |
| 237 |
p.prefetch = true |
| 238 |
results := make(chan result) // Collect expected errors |
| 239 |
|
| 240 |
// Connection.Close() interrupts Send, Receive, Disposition. |
| 241 |
snd, rcv := p.sender() |
| 242 |
go doSend(snd, results) |
| 243 |
|
| 244 |
if _, err := rcv.Receive(); err != nil { |
| 245 |
t.Error("receive", err) |
| 246 |
} |
| 247 |
rcv, snd = p.receiver() |
| 248 |
go doReceive(rcv, results) |
| 249 |
|
| 250 |
snd, rcv = p.sender() |
| 251 |
ack := snd.SendWaitable(amqp.NewMessage()) |
| 252 |
if _, err := rcv.Receive(); err != nil { |
| 253 |
t.Error("receive", err) |
| 254 |
} |
| 255 |
go doDisposition(ack, results) |
| 256 |
|
| 257 |
p.server.Close(want) |
| 258 |
for i := 0; i < 3; i++ { |
| 259 |
if r := <-results; want != r.err { |
| 260 |
t.Errorf("want %v got %v", want, r) |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
// Test closing the client end of the connection. |
| 266 |
func TestConnectionCloseInterrupt2(t *testing.T) { |
| 267 |
want := amqp.Error{Name: "x", Description: "bad"} |
| 268 |
p := newSocketPair(t, nil, nil) |
| 269 |
p.prefetch = true |
| 270 |
results := make(chan result) // Collect expected errors |
| 271 |
|
| 272 |
// Connection.Close() interrupts Send, Receive, Disposition. |
| 273 |
snd, rcv := p.sender() |
| 274 |
go doSend(snd, results) |
| 275 |
if _, err := rcv.Receive(); err != nil { |
| 276 |
t.Error("receive", err) |
| 277 |
} |
| 278 |
|
| 279 |
rcv, snd = p.receiver() |
| 280 |
go doReceive(rcv, results) |
| 281 |
|
| 282 |
snd, rcv = p.sender() |
| 283 |
ack := snd.SendWaitable(amqp.NewMessage()) |
| 284 |
go doDisposition(ack, results) |
| 285 |
|
| 286 |
p.client.Connection().Close(want) |
| 287 |
for i := 0; i < 3; i++ { |
| 288 |
if r := <-results; want != r.err { |
| 289 |
t.Errorf("want %v got %v", want, r.err) |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
func TestHeartbeat(t *testing.T) { |
| 295 |
p := newSocketPair(t, |
| 296 |
[]ConnectionOption{Heartbeat(102 * time.Millisecond)}, |
| 297 |
[]ConnectionOption{Heartbeat(101 * time.Millisecond)}) |
| 298 |
defer func() { p.close() }() |
| 299 |
|
| 300 |
// Function to freeze the server to stop it sending heartbeats. |
| 301 |
unfreeze := make(chan bool) |
| 302 |
defer close(unfreeze) |
| 303 |
freeze := func() error { return p.server.(*connection).engine.Inject(func() { <-unfreeze }) } |
| 304 |
|
| 305 |
test.FatalIf(t, p.client.Sync()) |
| 306 |
test.ErrorIf(t, test.Differ(101*time.Millisecond, p.client.Connection().Heartbeat())) |
| 307 |
test.ErrorIf(t, test.Differ(102*time.Millisecond, p.server.Heartbeat())) |
| 308 |
|
| 309 |
// Freeze the server for less than a heartbeat |
| 310 |
test.FatalIf(t, freeze()) |
| 311 |
time.Sleep(5 * time.Millisecond) |
| 312 |
unfreeze <- true |
| 313 |
// Make sure server is still responding. |
| 314 |
s, _ := p.sender() |
| 315 |
test.ErrorIf(t, s.Sync()) |
| 316 |
|
| 317 |
// Freeze the server till the p.client times out the connection |
| 318 |
test.FatalIf(t, freeze()) |
| 319 |
select { |
| 320 |
case <-p.client.Done(): |
| 321 |
if amqp.ResourceLimitExceeded != p.client.Error().(amqp.Error).Name { |
| 322 |
t.Error("bad timeout error:", p.client.Error()) |
| 323 |
} |
| 324 |
case <-time.After(1400 * time.Millisecond): |
| 325 |
t.Error("connection failed to time out") |
| 326 |
} |
| 327 |
|
| 328 |
unfreeze <- true // Unfreeze the server |
| 329 |
<-p.server.Done() |
| 330 |
if p.server.Error() == nil { |
| 331 |
t.Error("expected server side time-out or connection abort error") |
| 332 |
} |
| 333 |
} |