fix rbuf recyle

master
codeskyblue 8 years ago
parent e34d1b80dd
commit 245630e4ca

@ -6,7 +6,7 @@ import (
"sync"
"time"
"github.com/glycerine/rbuf"
"github.com/codeskyblue/rbuf"
"github.com/qiniu/log"
)
@ -183,7 +183,10 @@ func (wb *WriteBroadcaster) Bytes() []byte {
func (w *WriteBroadcaster) Write(p []byte) (n int, err error) {
w.Lock()
defer w.Unlock()
w.buf.Write(p)
// write with advance
w.buf.WriteWithAdvance(p)
for sw := range w.writers {
// set write timeout
err = GoTimeout(func() error {

@ -4,6 +4,8 @@ import (
"sync"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestBroadcast(t *testing.T) {
@ -43,3 +45,14 @@ func TestBroadcast(t *testing.T) {
}()
wg.Wait()
}
func TestRingBuffer(t *testing.T) {
Convey("Write some string to ring buffer", t, func() {
// buf := rbuf.NewFixedSizeRingBuf(5)
// buf.Write([]byte("abcde"))
// So(string(buf.Bytes()), ShouldEqual, "abcde")
// buf.Advance(2)
// buf.Write([]byte("fg"))
// So(string(buf.Bytes()), ShouldEqual, "cdefg")
})
}

Loading…
Cancel
Save