-
Notifications
You must be signed in to change notification settings - Fork 6
/
packer_test.go
92 lines (75 loc) · 1.71 KB
/
packer_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
// SPDX-FileCopyrightText: 2021 Henry Bubert
//
// SPDX-License-Identifier: MIT
package muxrpc
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"reflect"
"testing"
"github.com/ssbc/go-muxrpc/v2/codec"
)
// TestPacker is a crappy golden path test
func TestPacker(t *testing.T) {
c1, c2 := loPipe(t)
pkr1 := NewPacker(c1)
pkr2 := NewPacker(c2)
ctx := context.Background()
pkt := codec.Packet{
Req: 123,
Flag: codec.FlagString | codec.FlagStream,
Body: []byte("wat"),
}
errc := make(chan error)
go func() {
err := pkr1.w.WritePacket(pkt)
if err != nil {
errc <- fmt.Errorf("failed to send test packet (wat): %w", err)
}
close(errc)
}()
var hdr codec.Header
err := pkr2.NextHeader(ctx, &hdr)
if err != nil {
t.Fatal(err)
}
var buf = new(bytes.Buffer)
err = pkr2.r.ReadBodyInto(buf, hdr.Len)
if err != nil {
t.Fatal(err)
}
var want = codec.Packet{
Flag: hdr.Flag,
Req: -hdr.Req,
Body: buf.Bytes(),
}
if !reflect.DeepEqual(want, pkt) {
t.Log("Req matches:", reflect.DeepEqual(pkt.Req, want.Req))
t.Log("Flag matches:", reflect.DeepEqual(pkt.Flag, want.Flag))
t.Log("Body matches:", reflect.DeepEqual(pkt.Body, want.Body))
t.Log("Packet matches:", reflect.DeepEqual(&pkt, want))
t.Logf("expected packet %#v", &pkt)
t.Logf("got %#v", want)
t.Fatal("packet mismatch")
}
err = pkr1.Close()
if err != nil {
t.Fatal(err)
}
err = pkr2.NextHeader(ctx, &hdr)
if !errors.Is(err, io.EOF) {
t.Fatal("expected EOF, got:", err)
}
err = pkr1.w.WritePacket(want)
if err == nil {
t.Fatal("expected write-to-close-conn error, got nil")
}
sendErr, ok := <-errc
if ok {
t.Fatalf("%+v", sendErr)
}
t.Log("this error should be about pouring to a closed sink:", err)
}