Repository navigation
Expand file tree
/
Copy pathmailbox_test.go
More file actions
826 lines (688 loc) · 19.2 KB
/
Copy pathmailbox_test.go
File metadata and controls
826 lines (688 loc) · 19.2 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
package reign
import (
"context"
"encoding/json"
"reflect"
"testing"
"time"
)
type Stop struct{}
type B struct{ int }
type C struct{ int }
type D struct{ int }
type FakeMailbox struct{}
func (f FakeMailbox) getMailboxID() MailboxID {
return MailboxID(1337)
}
func (f FakeMailbox) send(_ interface{}) error {
return nil
}
func (f FakeMailbox) canBeGloballyRegistered() bool {
return false
}
func (f FakeMailbox) canBeGloballyUnregistered() bool {
return false
}
func (f FakeMailbox) onCloseNotify(_ *Address) {}
func (f FakeMailbox) removeNotify(_ *Address) {}
var anything = func(i interface{}) bool {
return true
}
func TestRegisterTypeCoverage(t *testing.T) {
RegisterType(t)
}
func TestMailboxReceiveNext(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
a, m := connections.NewMailbox()
defer m.Close()
msgs := make(chan interface{})
done := make(chan struct{})
go func() {
ctx := context.Background()
for {
msg, err := m.Receive(ctx)
if err != nil {
panic(err)
}
if _, ok := msg.(Stop); ok {
done <- struct{}{}
return
}
msgs <- msg
}
}()
if err := a.Send("hello"); err != nil {
t.Fatal()
}
received := <-msgs
if received.(string) != "hello" {
t.Fatal("Did not receive the expected value")
}
// this tests that messages can stack up, the goroutine above blocks on
// trying to send the first one out on the chan
if err := a.Send("1"); err != nil {
t.Fatal(err)
}
if err := a.Send("2"); err != nil {
t.Fatal(err)
}
if err := a.Send("3"); err != nil {
t.Fatal(err)
}
if err := a.Send(Stop{}); err != nil {
t.Fatal(err)
}
received = <-msgs
if received.(string) != "1" {
t.Fatal("Did not receive the 1")
}
received = <-msgs
if received.(string) != "2" {
t.Fatal("Did not receive the 2")
}
received = <-msgs
if received.(string) != "3" {
t.Fatal("Did not receive the 3")
}
<-done
}
func TestMailboxReceiveNextAsync(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
a, m := connections.NewMailbox()
defer m.Close()
msgs := []string{"1", "2", "3"}
for _, msg := range msgs {
if err := a.Send(msg); err != nil {
t.Fatal(err)
}
}
for _, msg := range msgs {
recv, ok := m.ReceiveAsync()
if !ok {
t.Fatalf("Did not receive a message. Expected '%s'", msg)
}
if recv != msg {
t.Fatalf("Received incorrect message. Expected '%s', got '%s'", msg, recv)
}
}
_, ok := m.ReceiveAsync()
if ok {
t.Fatal("ReceiveNextAsync() on an empty mailbox should have failed")
}
}
func TestMailboxReceiveNextTimeout(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
a, m := connections.NewMailbox()
defer m.Close()
msgs := []string{"1", "2", "3"}
for _, msg := range msgs {
if err := a.Send(msg); err != nil {
t.Fatal(err)
}
}
for _, msg := range msgs {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
recv, err := m.Receive(ctx)
cancel()
switch {
case err != nil:
t.Fatalf("Did not receive a message. Expected '%s'", msg)
case recv != msg:
t.Fatalf("Received incorrect message. Expected '%s', got '%s'", msg, recv)
}
}
// Now test that we actually wait for at least as long as the timeout specifies
ctx, cancel := context.WithTimeout(context.Background(), timeout)
startTime := time.Now()
_, err := m.Receive(ctx)
endTime := time.Now()
cancel()
expectedEndTime := startTime.Add(timeout)
switch {
case err == nil:
t.Fatal("ReceiveNext() on an empty mailbox should have failed due to time out")
case expectedEndTime.After(endTime):
t.Fatal("Timed out too soon")
}
}
func TestMailboxReceive(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
a, m := connections.NewMailbox()
defer m.Close()
msgs := make(chan interface{})
matches := make(chan func(interface{}) bool)
done := make(chan Stop)
matchC := func(i interface{}) bool {
_, ok := i.(C)
return ok
}
matchStop := func(i interface{}) bool {
_, ok := i.(Stop)
return ok
}
go func() {
ctx := context.Background()
for {
matcher := <-matches
msg, err := m.ReceiveMatch(ctx, matcher)
if err != nil {
panic(err)
}
if _, ok := msg.(Stop); ok {
close(done)
return
}
msgs <- msg
}
}()
b := B{1}
c := C{2}
d := D{3}
// to keep track of what the mailbox should have, we'll keep a list in
// the comments here:
if err := a.Send(b); err != nil {
t.Fatal(err)
}
if err := a.Send(c); err != nil {
t.Fatal(err)
}
if err := a.Send(d); err != nil {
t.Fatal(err)
}
// contains: [b, c, d]
matches <- matchC
msg := <-msgs
if _, ok := msg.(C); !ok {
t.Fatal("Did not retrieve the correct message")
}
// b is currently held in the nextMessage chan, so it isn't in the messages slice.
if expected := []message{{d}}; !reflect.DeepEqual(m.messages, expected) {
t.Fatalf("Did not properly fix up the message queue:\n%#v\n%#v", m.messages, expected)
}
// now test the case where we don't have the message we want
waitingDone := make(chan Stop)
fatal := make(chan Stop)
go func() {
matches <- matchC
msg := <-msgs
if _, ok := msg.(C); !ok {
close(fatal)
}
close(waitingDone)
}()
if err := a.Send(d); err != nil {
t.Fatal(err)
}
if err := a.Send(c); err != nil {
t.Fatal(err)
}
// We will match on c we just sent.
select {
case <-waitingDone:
case <-fatal:
t.Fatal("Did not received the expected message")
}
// b is still in the nextMessage chan.
if !reflect.DeepEqual(m.messages, []message{{d}, {d}}) {
t.Fatal("Did not properly fix up the message queue")
}
if err := a.Send(Stop{}); err != nil {
t.Fatal(err)
}
matches <- matchStop
// Matching on stop will close the done channel.
<-done
}
func TestSimpleMailboxTerminate(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
addr, mailbox := connections.NewMailbox()
// Make certain the mailbox is empty.
m, ok := mailbox.ReceiveAsync()
if ok {
t.Fatalf("Received an unexpected message: %#v", m)
}
// Deliver a message to the mailbox.
err := addr.Send("Hello")
if err != nil {
t.Fatal(err)
}
// The mailbox currently has one message in it. However, we should
// only receive the MailboxTerminated message after we terminate the
// mailbox, *not* the "Hello" message.
mailbox.Close()
m, ok = mailbox.ReceiveAsync()
if _, terminated := m.(MailboxClosed); !ok || !terminated {
t.Fatalf("Expected a MailboxTerminated message: %#v", m)
}
}
func TestTerminate(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
addr1, mailbox1 := connections.NewMailbox()
addr2, mailbox2 := connections.NewMailbox()
defer mailbox2.Close()
addr1.OnCloseNotify(addr2)
mailbox1.Close()
// double-termination is legal
mailbox1.Close()
msg, ok := mailbox2.ReceiveAsync()
if !ok {
t.Fatal("No message received. Expected closed.")
}
if MailboxID(msg.(MailboxClosed)) != addr1.mailboxID {
t.Fatal("Close did not send the right closed message")
}
err := addr1.Send("message")
if err != ErrMailboxClosed {
t.Fatal("Sending to a closed mailbox does not yield the closed error")
}
addr1.OnCloseNotify(addr2)
msg, ok = mailbox2.ReceiveAsync()
if !ok {
t.Fatal("No message received. Expected closed.")
}
if MailboxID(msg.(MailboxClosed)) != addr1.mailboxID {
t.Fatal("Close did not send the right close message for closed mailbox")
}
terminatedResult, ok := mailbox1.ReceiveAsync()
if !ok {
t.Fatal("No message received. Expected closed.")
}
if MailboxID(terminatedResult.(MailboxClosed)) != addr1.mailboxID {
t.Fatal("ReceiveNextAsync from a closed mailbox does not return MailboxClosed properly")
}
addr1S, mailbox1S := connections.NewMailbox()
mailbox1S.Close()
ctx := context.Background()
terminatedResult, err = mailbox1S.ReceiveMatch(ctx, anything)
if err != ErrMailboxClosed {
t.Fatal("Receive from a closed mailbox does not return ErrMailboxClosed error")
}
if MailboxID(terminatedResult.(MailboxClosed)) != addr1S.mailboxID {
t.Fatal("Receive from a closed mailbox does not return correct MailboxClosed message")
}
}
func TestAsyncTerminateOnReceive(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
wantHello := func(i interface{}) bool {
iReal, isStr := i.(string)
if !isStr {
return false
}
return iReal == "Hello"
}
addr1, mailbox1 := connections.NewMailbox()
done := make(chan Stop)
// This goroutine uses private variables watches to see when the first
// message has been sent. The Receive call is not looking for this message,
// so once we see that len(m.messages) is no longer 0, we know that the
// Receive call is in the for loop part of the call.
// Once that happens, this will Terminate the mailbox.
go func() {
select {
case <-done:
return
case prevMsg, ok := <-mailbox1.nextMessage:
if !ok {
return
}
mailbox1.nextMessage <- prevMsg
}
mailbox1.Close()
}()
// And here, we run a Receive call that won't match the first message
// we send it, and assert that it gets the correct MailboxTerminated.
var (
err error
result interface{}
)
go func() {
var ctx = context.Background()
result, err = mailbox1.ReceiveMatch(ctx, wantHello)
if err != nil && err != ErrMailboxClosed {
panic(err)
}
close(done)
}()
if sErr := addr1.Send(1); sErr != nil {
t.Fatal(sErr)
}
<-done
// The end result of all this setup is that we should be able to show
// that the .Receive call ended up with a MailboxTerminated as its result
switch {
case err != ErrMailboxClosed:
t.Fatalf("Closing the mailbox during Receive() doesn't return an ErrMailboxClosed error: %#v", err)
case MailboxID(result.(MailboxClosed)) != addr1.mailboxID:
t.Fatal("Closing the mailbox during Receive() doesn't return an accurate MailboxClosed message")
}
}
func TestAsyncTerminateOnReceiveNext(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
addr1, mailbox1 := connections.NewMailbox()
// And here, we run a Receive call that won't match the first message
// we send it, and assert that it gets the correct MailboxTerminated.
var result interface{}
done := make(chan struct{})
go func() {
var (
ctx = context.Background()
err error
)
result, err = mailbox1.Receive(ctx)
if err != nil && err != ErrMailboxClosed {
panic(err)
}
close(done)
}()
// Make sure the above goroutine has enough time to start listening for
// a new message.
time.Sleep(500 * time.Millisecond)
mailbox1.Close()
<-done
// The end result of all this setup is that we should be able to show
// that the .Receive call ended up with a MailboxTerminated as its result
if MailboxID(result.(MailboxClosed)) != addr1.mailboxID {
t.Fatal("Terminating the ReceiveNext on Terminate doesn't work")
}
}
func TestGetAddress(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
a, m := connections.NewMailbox()
defer m.Close()
a2 := Address{mailboxID: a.mailboxID}
a2.getAddress()
if a2.connectionServer == nil {
t.Fatal("failed to get connection server with call to getAddress()")
}
// Make an invalid node ID
a.mailboxID = MailboxID(1337)
a.mailbox = nil
if !panics(func() { a.getAddress() }) {
t.Fatal("does not panic when getting a remotemailbox from a node that doesn't exist")
}
a.connectionServer = nil
if !panics(func() { a.getAddress() }) {
t.Fatal("does not panic when attempting to get the address of an Address with no connectionServer")
}
}
func TestRemoveOfNotifications(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
addr, mailbox1 := connections.NewMailbox()
defer mailbox1.Close()
addr2, mailbox2 := connections.NewMailbox()
defer mailbox2.Close()
// no crashing
addr.RemoveNotify(addr2)
addr.OnCloseNotify(addr2)
addr.RemoveNotify(addr2)
if len(addr.getAddress().(*Mailbox).notificationAddresses) != 0 {
t.Fatal("Removing addresses doesn't work as expected")
}
}
func TestSendByID(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
_, mailbox := connections.NewMailbox()
defer mailbox.Close()
// Verify that creating a new address with the same ID works
addr := Address{mailboxID: mailbox.id}
err := addr.Send("Hello")
msg, ok := mailbox.ReceiveAsync()
if !ok {
t.Fatal("No message received")
}
str, isStr := msg.(string)
if err != nil || !isStr || str != "Hello" {
t.Fatal("sendByID failed:", msg)
}
addr = Address{mailboxID: MailboxID(256) + MailboxID(connections.ThisNode.ID)}
err = addr.Send("Hello")
if err != ErrMailboxClosed {
t.Fatal("sendByID happily sent to a terminated mailbox")
}
}
func getMarshalsAndTest(a address, t *testing.T) ([]byte, []byte, []byte, string) {
addr := Address{a.getMailboxID(), a, nil}
bin, err := addr.MarshalBinary()
if err != nil {
t.Fatal("fail to marshal binary")
}
text, err := addr.MarshalText()
if err != nil {
t.Fatal("fail to marshal text")
}
json, err := addr.MarshalJSON()
if err != nil {
t.Fatal("fail to marshal JSON")
}
s := addr.String()
// If unmarshalling doesn't go through handleIncomingConnections, no connectionServer will
// be put into the Address or its internal registryMailbox. So we only compare the names
var addrBin Address
err = addrBin.UnmarshalBinary(bin)
if err != nil {
t.Fatalf("Could not unmarshal the marshaled bin: %#x", bin)
}
binID := addrBin.mailboxID
if binID != addr.mailboxID {
t.Fatalf("After unmarshaling the bin, ids are not equal: left = %v, right = %v", binID, addr.mailboxID)
}
var addrText Address
err = addrText.UnmarshalText(text)
if err != nil {
t.Fatal("could not unmarshal the text")
}
textID := addrText.mailboxID
if textID != addr.mailboxID {
t.Fatalf("%#v %#v %#v\nAfter unmarshalling the text, ids are not ==: %#v %#v",
bin, addrText.mailboxID, addr.mailboxID, addrText.mailboxID, addr.mailboxID)
}
var addrJSON Address
err = addrJSON.UnmarshalJSON(json)
if err != nil {
t.Fatalf("could not unmarshal the JSON %q: %s", json, err)
}
jsonID := addrJSON.mailboxID
if jsonID != addr.mailboxID {
t.Fatalf("%#v %#v %#v\nAfter unmarshalling the JSON, ids are not ==: %#v %#v",
bin, addrJSON.mailboxID, addr.mailboxID, addrJSON.mailboxID, addr.mailboxID)
}
return bin, text, json, s
}
func TestMarshaling(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
a, m := connections.NewMailbox()
defer m.Close()
a.mailbox = FakeMailbox{}
_, err := a.MarshalBinary()
if err != ErrIllegalAddressFormat {
t.Fatal("Address with invalid mailbox did not error on binary marshal")
}
mID := MailboxID(257)
connections.ThisNode.ID = mID.NodeID()
mailbox := &Mailbox{id: mID}
bin, text, json, s := getMarshalsAndTest(mailbox, t)
if !reflect.DeepEqual(bin, []byte{60, 0x81, 0x02}) {
t.Error("mailboxID did not binary marshal as expected")
}
if string(text) != "<1:1>" {
t.Error("mailboxID failed to marshal to text " + string(text))
}
if string(json) != "<1:1>" {
t.Error("mailboxID failed to marshal to JSON " + string(json))
}
if s != "<1:1>" {
t.Error("mailboxID failed to String properly")
}
bra := boundRemoteAddress{MailboxID: MailboxID(257)}
bin, text, json, s = getMarshalsAndTest(bra, t)
if !reflect.DeepEqual(bin, []byte{60, 0x81, 0x02}) {
t.Error("bra did not binary marshal as expected")
}
if string(text) != "<1:1>" {
t.Error("bra failed to marshal to text")
}
if string(json) != "<1:1>" {
t.Error("bra failed to marshal to JSON")
}
if s != "<1:1>" {
t.Error("bra failed to String properly")
}
bin, text, json, s = getMarshalsAndTest(noMailbox{}, t)
if !reflect.DeepEqual(bin, []byte("X")) {
t.Error("noMailbox did not binary marshal as expected")
}
if string(text) != "X" {
t.Error("noMailbox did not text marshal as expected")
}
if string(json) != "X" {
t.Error("noMailbox did not JSON marshal as expected")
}
if s != "X" {
t.Error("noMailbox did not String as expected")
}
}
func TestAddressUnmarshalJSON(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
addr1, _ := cs.NewMailbox()
addr1Bytes, err := addr1.MarshalJSON()
if err != nil {
t.Fatal(err)
}
addr2 := &Address{}
err = addr2.UnmarshalJSON(addr1Bytes)
if err != nil {
t.Fatal(err)
}
// This will fail unless the Address has its mailbox cached.
_, err = json.Marshal(addr2)
if err != nil {
t.Fatal(err)
}
}
func TestUnmarshalAddressErrors(t *testing.T) {
cs, _ := noClustering(NullLogger)
defer cs.Terminate()
a := &Address{}
err := a.UnmarshalBinary([]byte{})
if err == nil {
t.Fatal("Can unmarshal an address from 0 bytes?")
}
err = a.UnmarshalBinary([]byte{0xFF})
if err == nil {
t.Fatal("Can unmarshal an address from a blantently illegal value")
}
err = a.UnmarshalText(nil)
if err == nil {
t.Fatal("Can unmarshal an address from nil bytes?")
}
for _, addrText := range []string{
"somethingreallylongthatcan'tpossiblybeanaddress",
"1:1>",
"<1:1",
"<1111>",
"<a:1>",
"<1:a>",
"<258:1>",
"<0:72057594037927937>",
"<0:7205759403792793599>",
"<-1:-1>",
} {
err = a.UnmarshalText([]byte(addrText))
if err == nil {
t.Fatal("Can unmarshal into an address:", addrText)
}
}
}
func TestCoverNoMailbox(t *testing.T) {
mID := MailboxID(257)
nm := noMailbox{mID}
if nm.send(939) != ErrMailboxClosed {
t.Fatal("Can send to the no mailbox somehow")
}
if nm.MailboxID != mID {
t.Fatal("mailboxID incorrectly implemented for noMailbox")
}
nm.onCloseNotify(&Address{mailboxID: mID, mailbox: nm})
nm.removeNotify(&Address{mailboxID: mID, mailbox: nm})
// FIXME: Test marshal/unmarshal
}
func TestCoverNoConnections(t *testing.T) {
setConnections(nil)
if !panics(func() { connections.NewMailbox() }) {
t.Fatal("Mailboxes can be created without connections")
}
}
func TestCoverCanBeRegistered(t *testing.T) {
mbox := Mailbox{}
if !mbox.canBeGloballyRegistered() {
t.Fatal("Can't register mailboxIDs globally")
}
nm := noMailbox{}
if nm.canBeGloballyRegistered() {
t.Fatal("Can globally register noMailboxes")
}
var bra boundRemoteAddress
if bra.canBeGloballyRegistered() {
t.Fatal("Can globally register boundRemoteAddresses")
}
}
// Cover the errors not tested by anything else
func TestCoverAddressMarshaling(t *testing.T) {
cs, r := noClustering(NullLogger)
defer cs.Terminate()
defer r.Terminate()
a := &Address{}
a.connectionServer = cs
b, err := a.MarshalBinary()
if b != nil || err != ErrIllegalAddressFormat {
t.Fatalf("Wrong error from marshaling binary of empty address #%v, #%v", b, err)
}
a = &Address{}
err = a.UnmarshalBinary([]byte("<"))
if err != ErrIllegalAddressFormat {
t.Fatal("Wrong error from unmarshaling illegal binary mailbox")
}
a, m1 := connections.NewMailbox()
defer m1.Close()
a2, m2 := connections.NewMailbox()
defer m2.Close()
a2.UnmarshalFromID(a.mailboxID)
a2.connectionServer = connections
err = a2.Send("test")
if err != nil {
t.Fatal(err)
}
msg, ok := m1.ReceiveAsync()
if !ok {
t.Fatal("Mailbox received nothing")
}
if !reflect.DeepEqual(msg, "test") {
t.Fatal("Can't unmarshal a local address from an ID correctly.")
}
err = a.UnmarshalText([]byte("<23456789012345678901234"))
if err != ErrIllegalAddressFormat {
t.Fatal("fails the length check on normal mailboxes")
}
err = a.UnmarshalText([]byte("\"moo"))
if err != ErrIllegalAddressFormat {
t.Fatal("fails to properly check registry mailboxes in text for quotes")
}
a = &Address{}
_, err = a.MarshalText()
if err == nil {
t.Fatal("can marshal nonexistant address to Text")
}
}