mirror of
https://github.com/soypat/lneto.git
synced 2026-08-16 12:53:26 +00:00
txqueue work
This commit is contained in:
+5
-7
@@ -10,15 +10,12 @@ import (
|
||||
|
||||
var errRingBufferFull = errors.New("lneto/ring: buffer full")
|
||||
|
||||
// NewRing returns a new ring buffer ready for use.
|
||||
func NewRing(buf []byte) *Ring {
|
||||
return &Ring{Buf: buf}
|
||||
}
|
||||
|
||||
// Ring implements basic Ring buffer functionality.
|
||||
type Ring struct {
|
||||
// Buf is used to store data written into Ring
|
||||
// with Write methods and then read out with Read methods.
|
||||
// The capacity of Buf is unused.
|
||||
// There is no readable data when both Off and End are zero.
|
||||
Buf []byte
|
||||
// Start of readable data which indexes into Buf.
|
||||
Off int
|
||||
@@ -35,8 +32,9 @@ func (r *Ring) WriteLimited(b []byte, limitOffset int) (int, error) {
|
||||
if len(b) > len(r.Buf) {
|
||||
return 0, io.ErrShortBuffer
|
||||
}
|
||||
writeEnd := r.Off + len(b)
|
||||
if limitOffset >= r.Off && writeEnd > limitOffset {
|
||||
|
||||
writeEnd := (r.Off + len(b)) % len(b)
|
||||
if limitOffset > r.Off && writeEnd > limitOffset {
|
||||
return 0, errRingBufferFull
|
||||
} else if writeEnd > len(r.Buf) {
|
||||
writeEnd %= len(r.Buf)
|
||||
|
||||
@@ -172,6 +172,20 @@ func TestRing2(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRingWriteLimited(t *testing.T) {
|
||||
rng := rand.New(rand.NewSource(0))
|
||||
const bufSize = 10
|
||||
r := &Ring{
|
||||
Buf: make([]byte, bufSize),
|
||||
}
|
||||
var data [bufSize]byte
|
||||
for i := 0; i < 32; i++ {
|
||||
n, _ := rng.Read(data[:rng.Intn(10)+1])
|
||||
limoff := r.Off
|
||||
r.WriteLimited()
|
||||
}
|
||||
}
|
||||
|
||||
func TestRing_findcrash(t *testing.T) {
|
||||
const maxsize = 33
|
||||
const ntests = 800000
|
||||
|
||||
Reference in New Issue
Block a user