Browse Source

Buffer: test delayed writes

master
maze 1 year ago
parent
commit
8a7f3b4cf7
2 changed files with 28 additions and 1 deletions
  1. +1
    -0
      buffer.go
  2. +27
    -1
      buffer_test.go

+ 1
- 0
buffer.go View File

@ -108,6 +108,7 @@ func (b *Buffer) ScrubSize(size int) {
}
// Read data from the buffer.
// If no data has been written yet, this will block until a write occurs.
func (b *Buffer) Read(p []byte) (n int, err error) {
if len(p) == 0 {
return


+ 27
- 1
buffer_test.go View File

@ -11,6 +11,7 @@ func TestBuffer(t *testing.T) {
for _, test := range testAllCases {
t.Run(strings.SplitN(test.Test, "\n", 2)[0], func(it *testing.T) {
b := NewBuffer(All)
defer b.Reset()
if _, err := b.WriteString(test.Test); err != nil {
it.Fatal(err)
@ -31,10 +32,33 @@ func TestBuffer(t *testing.T) {
}
}
func TestBuffer_Read(t *testing.T) {
b := NewBuffer(All)
go func(b *Buffer) {
time.Sleep(time.Millisecond * 10)
_, _ = b.WriteString("$apr1$71850310$gh9m4xcAn3MGxogwX/ztb.")
}(b)
// Testing Write-before-Read buffer signalling.
r, err := ioutil.ReadAll(b)
if err != nil {
t.Fatal(err)
return
}
want := "$apr1$*redacted*"
if string(r) != want {
t.Fatalf("expected %q, got %q", want, string(r))
}
}
func TestBuffer_ScrubSize(t *testing.T) {
for _, test := range testAllCases {
t.Run(strings.SplitN(test.Test, "\n", 2)[0], func(it *testing.T) {
b := NewBuffer(All)
defer b.Reset()
b.ScrubSize(len(test.Test))
if _, err := b.WriteString(test.Test); err != nil {
it.Fatal(err)
@ -62,6 +86,8 @@ func TestBuffer_ScrubAfter(t *testing.T) {
for _, test := range testAllCases {
t.Run(strings.SplitN(test.Test, "\n", 2)[0], func(it *testing.T) {
b := NewBuffer(All)
defer b.Reset()
c := b.ScrubAfter(time.Millisecond * 5)
defer c()
@ -71,7 +97,7 @@ func TestBuffer_ScrubAfter(t *testing.T) {
}
// Allow the scrub to take place.
time.Sleep(time.Millisecond * 15)
time.Sleep(time.Millisecond * 25)
r, err := ioutil.ReadAll(b)
if err != nil {


Loading…
Cancel
Save