Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcast notify on Buffer.Close #302

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions packetio/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,7 @@ func (b *Buffer) Close() (err error) {
b.mutex.Unlock()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this "section" is where the bad things happen

if waiting {
select {
case b.notify <- struct{}{}:
default:
}
close(b.notify)
}

return nil
Expand Down
44 changes: 44 additions & 0 deletions packetio/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,47 @@ func BenchmarkBuffer140(b *testing.B) {
func BenchmarkBuffer1400(b *testing.B) {
benchmarkBuffer(b, 1400)
}

func TestBufferConcurrentRead(t *testing.T) {
assert := assert.New(t)

buffer := NewBuffer()
packet := make([]byte, 4)

// Write twice
n, err := buffer.Write([]byte{2, 3, 4})
assert.NoError(err)
assert.Equal(3, n)

n, err = buffer.Write([]byte{5, 6, 7})
assert.NoError(err)
assert.Equal(3, n)

// Read twice
n, err = buffer.Read(packet)
assert.NoError(err)
assert.Equal(3, n)
assert.Equal([]byte{2, 3, 4}, packet[:n])

n, err = buffer.Read(packet)
assert.NoError(err)
assert.Equal(3, n)
assert.Equal([]byte{5, 6, 7}, packet[:n])

errCh := make(chan error, 2)
readIntoErr := func() {
_, readErr := buffer.Read(packet)
errCh <- readErr
}
go readIntoErr()
go readIntoErr()

// Close
err = buffer.Close()
assert.NoError(err)

err = <-errCh
assert.Equal(io.EOF, err)
err = <-errCh
assert.Equal(io.EOF, err)
}
Loading