-
Notifications
You must be signed in to change notification settings - Fork 2
/
stream.go
73 lines (58 loc) · 1.45 KB
/
stream.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
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mock
import (
"bytes"
"github.com/ethsana/sana/pkg/p2p"
)
type Stream struct {
readBuffer *bytes.Buffer
writeBuffer *bytes.Buffer
writeCounter int
readCounter int
readError error
writeError error
readErrCheckmark int
writeErrCheckmark int
}
func NewStream(readBuffer, writeBuffer *bytes.Buffer) *Stream {
return &Stream{readBuffer: readBuffer, writeBuffer: writeBuffer}
}
func (s *Stream) SetReadErr(err error, checkmark int) {
s.readError = err
s.readErrCheckmark = checkmark
}
func (s *Stream) SetWriteErr(err error, checkmark int) {
s.writeError = err
s.writeErrCheckmark = checkmark
}
func (s *Stream) Read(p []byte) (n int, err error) {
if s.readError != nil && s.readErrCheckmark <= s.readCounter {
return 0, s.readError
}
s.readCounter++
return s.readBuffer.Read(p)
}
func (s *Stream) Write(p []byte) (n int, err error) {
if s.writeError != nil && s.writeErrCheckmark <= s.writeCounter {
return 0, s.writeError
}
s.writeCounter++
return s.writeBuffer.Write(p)
}
func (s *Stream) Headers() p2p.Headers {
return nil
}
func (s *Stream) ResponseHeaders() p2p.Headers {
return nil
}
func (s *Stream) Close() error {
return nil
}
func (s *Stream) FullClose() error {
return nil
}
func (s *Stream) Reset() error {
return nil
}