aboutsummaryrefslogtreecommitdiff
path: root/network/protocol/sideband64k/helpers_test.go
blob: f9b2608f1b455dbcdfed2d781a101a705778a4e2 (about) (plain) (blame)
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
package sideband64k_test

import (
	"bytes"
	"io"
)

type limitWriter struct {
	buf         bytes.Buffer
	maxPerWrite int
	flushes     int
	shortWrite  bool
}

func (w *limitWriter) Write(p []byte) (int, error) {
	if w.shortWrite {
		return 0, nil
	}

	if w.maxPerWrite > 0 && len(p) > w.maxPerWrite {
		p = p[:w.maxPerWrite]
	}

	return w.buf.Write(p)
}

func (w *limitWriter) Flush() error {
	w.flushes++

	return nil
}

type byteReader struct {
	data []byte
}

func (r *byteReader) Read(p []byte) (int, error) {
	if len(r.data) == 0 {
		return 0, io.EOF
	}

	p[0] = r.data[0]
	r.data = r.data[1:]

	return 1, nil
}