deps: Update bencode to pick up uint16 fix

This commit is contained in:
Justin Li 2015-02-20 12:29:02 -05:00
parent 5eda5f451d
commit be5720dadd
5 changed files with 40 additions and 17 deletions

4
Godeps/Godeps.json generated
View file

@ -4,7 +4,7 @@
"Deps": [
{
"ImportPath": "github.com/chihaya/bencode",
"Rev": "e60878f635e1a61315c413492e133dd39769b1d1"
"Rev": "3c485a8d166ff6a79baba90c2c2da01c8348e930"
},
{
"ImportPath": "github.com/golang/glog",
@ -16,7 +16,7 @@
},
{
"ImportPath": "github.com/pushrax/bufferpool",
"Rev": "c8962db3ad1e65fe9a83e7298dcd213938cf02d5"
"Rev": "7d6e1653dee10a165d1f357f3a57bc8031e9621b"
},
{
"ImportPath": "github.com/pushrax/faststats",

View file

@ -62,6 +62,12 @@ func marshal(w io.Writer, data interface{}) error {
case uint:
marshalUint(w, uint64(v))
case int16:
marshalInt(w, int64(v))
case uint16:
marshalUint(w, uint64(v))
case int64:
marshalInt(w, v)

View file

@ -19,6 +19,8 @@ var marshalTests = []struct {
{uint(43), "i43e"},
{int64(44), "i44e"},
{uint64(45), "i45e"},
{int16(44), "i44e"},
{uint16(45), "i45e"},
{"example", "7:example"},
{[]byte("example"), "7:example"},

View file

@ -29,7 +29,7 @@ func New(poolSize, bufferSize int) *BufferPool {
// Take is used to obtain a new zeroed buffer. This will allocate a new buffer
// if the pool was empty.
func (pool *BufferPool) Take() *bytes.Buffer {
return bytes.NewBuffer(pool.TakeSlice())
return bytes.NewBuffer(pool.TakeSlice()[:0])
}
// TakeSlice is used to obtain a new slice. This will allocate a new slice
@ -38,7 +38,7 @@ func (pool *BufferPool) TakeSlice() (slice []byte) {
select {
case slice = <-pool.pool:
default:
slice = make([]byte, 0, pool.bufferSize)
slice = make([]byte, pool.bufferSize)
}
return
}
@ -46,12 +46,13 @@ func (pool *BufferPool) TakeSlice() (slice []byte) {
// Give is used to attempt to return a buffer to the pool. It may not
// be added to the pool if it was already full.
func (pool *BufferPool) Give(buf *bytes.Buffer) error {
if buf.Len() != pool.bufferSize {
buf.Reset()
slice := buf.Bytes()
if cap(slice) < pool.bufferSize {
return errors.New("Gave an incorrectly sized buffer to the pool.")
}
buf.Reset()
slice := buf.Bytes()
return pool.GiveSlice(slice[:buf.Len()])
}

View file

@ -12,6 +12,21 @@ import (
"github.com/pushrax/bufferpool"
)
func ExampleNew() {
bp := bufferpool.New(10, 255)
dogBuffer := bp.Take()
dogBuffer.WriteString("Dog!")
bp.Give(dogBuffer)
catBuffer := bp.Take() // dogBuffer is reused and reset.
catBuffer.WriteString("Cat!")
fmt.Println(catBuffer)
// Output:
// Cat!
}
func TestTakeFromEmpty(t *testing.T) {
bp := bufferpool.New(1, 1)
poolBuf := bp.Take()
@ -37,17 +52,16 @@ func TestTakeFromFilled(t *testing.T) {
}
}
func ExampleNew() {
bp := bufferpool.New(10, 255)
func TestSliceSemantics(t *testing.T) {
bp := bufferpool.New(1, 8)
dogBuffer := bp.Take()
dogBuffer.WriteString("Dog!")
bp.Give(dogBuffer)
buf := bp.Take()
buf.WriteString("abc")
bp.Give(buf)
catBuffer := bp.Take() // dogBuffer is reused and reset.
catBuffer.WriteString("Cat!")
buf2 := bp.TakeSlice()
fmt.Println(catBuffer)
// Output:
// Cat!
if !bytes.Equal(buf2[:3], []byte("abc")) {
t.Fatalf("Buffer from filled bufferpool was recycled incorrectly.")
}
}