-
Notifications
You must be signed in to change notification settings - Fork 6
/
repro_test.go
118 lines (89 loc) · 1.98 KB
/
repro_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
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
// SPDX-FileCopyrightText: 2021 Henry Bubert
//
// SPDX-License-Identifier: MIT
// +build ignore
// added as a helper to test against dumps of connections
package muxrpc
import (
"bytes"
"context"
"fmt"
"io"
"os"
"sync"
"testing"
"github.com/stretchr/testify/require"
)
func TestNegativeRequestID(t *testing.T) {
r := require.New(t)
var fh FakeHandler
fh.HandledReturns(true)
fh.HandleCallCalls(func(_ context.Context, req *Request) {
t.Logf("called %s (req ID: %d)", req.Method.String(), req.id)
})
rxFile, err := os.Open("testfixtures/negreq/three/rx")
r.NoError(err)
var writeBuf = new(bytes.Buffer)
var rwc = &testRwc{
rd: rxFile,
wr: writeBuf,
header: true,
}
pkr := NewPacker(rwc)
edp := Handle(pkr, &fh)
// register the outgoing call(s?)
// go func() {
// _, _, err := edp.Duplex(context.TODO(), TypeBinary, Method{"tunnel", "connect"})
// r.NoError(err)
// }()
err = edp.(Server).Serve()
r.NoError(err, "after %d bytes", rwc.bytesRead)
}
type testRwc struct {
rd io.Reader
wr io.Writer
mu sync.Mutex
closed bool
bytesRead int
header bool
}
func (t *testRwc) Write(b []byte) (int, error) {
t.mu.Lock()
defer t.mu.Unlock()
if t.closed {
return -1, fmt.Errorf("testRwc: cant write - already closed")
}
return t.wr.Write(b)
}
func (t *testRwc) Read(b []byte) (int, error) {
t.mu.Lock()
defer t.mu.Unlock()
if t.closed {
return -1, fmt.Errorf("testRwc: cant read - already closed")
}
read, err := t.rd.Read(b)
t.bytesRead += read
// if t.header {
// var h codec.Header
// rd := bytes.NewReader(b)
// err := binary.Read(rd, binary.BigEndian, &h)
// if err != nil {
// return -1, err
// }
// fmt.Printf("header: %+v\n", h)
// } else {
// fmt.Println("body:")
// fmt.Println(hex.Dump(b))
// }
// t.header = !t.header
return read, err
}
func (t *testRwc) Close() error {
t.mu.Lock()
defer t.mu.Unlock()
if t.closed {
return fmt.Errorf("testRwc: cant close - already closed")
}
t.closed = true
return nil
}