This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
testing.go
73 lines (60 loc) · 1.68 KB
/
testing.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
package blockstream
import (
"context"
"io"
"testing"
"github.com/ipfs/go-cid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/Wondertan/go-blockstream/block"
)
func assertBlockReq(t *testing.T, r io.Reader, in uint32, ids []cid.Cid) {
id, out, err := readBlocksReq(r)
require.Nil(t, err, err)
assert.Equal(t, ids, out)
assert.Equal(t, in, id)
}
func assertBlockResp(t *testing.T, r io.Reader, in uint32, ids []cid.Cid, errIn error) {
id, out, errOut, err := readBlocksResp(r)
require.Nil(t, err, err)
assert.Equal(t, in, id)
assert.Equal(t, len(ids), len(out))
assert.Equal(t, errIn, errOut)
for i, b := range out {
_, err = newBlockCheckCid(b, ids[i])
require.Nil(t, err, err)
}
}
func newRequestPair(ctx context.Context, in, out chan *block.Request) {
s1, s2 := streamPair()
newRequester(ctx, s1, in, logClose)
newResponder(ctx, s2, out, logClose)
}
func newTestResponder(t *testing.T, ctx context.Context, reqs chan *block.Request) io.ReadWriter {
s1, s2 := streamPair()
newResponder(ctx, s2, reqs, logClose)
return s1
}
func newTestRequester(t *testing.T, ctx context.Context, reqs chan *block.Request) io.ReadWriter {
s1, s2 := streamPair()
newRequester(ctx, s2, reqs, logClose)
return s1
}
type fakeStream struct {
read *io.PipeReader
write *io.PipeWriter
}
func streamPair() (*fakeStream, *fakeStream) {
r1, w1 := io.Pipe()
r2, w2 := io.Pipe()
return &fakeStream{r1, w2}, &fakeStream{r2, w1}
}
func (s *fakeStream) Read(b []byte) (n int, err error) {
return s.read.Read(b)
}
func (s *fakeStream) Write(b []byte) (n int, err error) {
return s.write.Write(b)
}
func (s *fakeStream) Close() error {
return s.write.Close()
}