|
| 1 | +package buffer |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +type countingRecycler struct { |
| 8 | + count int |
| 9 | +} |
| 10 | + |
| 11 | +func (r *countingRecycler) Recycle(b *Buffer) { |
| 12 | + r.count++ |
| 13 | +} |
| 14 | + |
| 15 | +func TestEnsure(t *testing.T) { |
| 16 | + for _, tc := range []struct { |
| 17 | + name string |
| 18 | + buf *Buffer |
| 19 | + size int |
| 20 | + wantKept bool |
| 21 | + }{ |
| 22 | + {"nil buffer", nil, 100, false}, |
| 23 | + {"sufficient cap", New(make([]byte, 200), &countingRecycler{}), 100, true}, |
| 24 | + {"insufficient cap", New(make([]byte, 100), &countingRecycler{}), 200, false}, |
| 25 | + } { |
| 26 | + t.Run(tc.name, func(t *testing.T) { |
| 27 | + var r *countingRecycler |
| 28 | + if tc.buf != nil { |
| 29 | + r = tc.buf.recycler.(*countingRecycler) |
| 30 | + } |
| 31 | + buf := Ensure(tc.buf, tc.size, NewPoolSource()) |
| 32 | + if len(buf.Bytes()) != tc.size { |
| 33 | + t.Errorf("len = %d, want %d", len(buf.Bytes()), tc.size) |
| 34 | + } |
| 35 | + if tc.wantKept != (buf == tc.buf) { |
| 36 | + t.Errorf("kept same buffer: got %v, want %v", buf == tc.buf, tc.wantKept) |
| 37 | + } |
| 38 | + if r != nil && !tc.wantKept && r.count != 1 { |
| 39 | + t.Errorf("recycler not called, count = %d, want 1", r.count) |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestRelease(t *testing.T) { |
| 46 | + for _, tc := range []struct { |
| 47 | + name string |
| 48 | + buf *Buffer |
| 49 | + }{ |
| 50 | + {"nil buffer", nil}, |
| 51 | + {"nil recycler", New(nil, nil)}, |
| 52 | + {"non-nil recycler", New(make([]byte, 10), &countingRecycler{})}, |
| 53 | + } { |
| 54 | + t.Run(tc.name, func(t *testing.T) { |
| 55 | + var r *countingRecycler |
| 56 | + if tc.buf != nil { |
| 57 | + r, _ = tc.buf.recycler.(*countingRecycler) |
| 58 | + } |
| 59 | + Release(tc.buf) // must not panic |
| 60 | + if r != nil && r.count != 1 { |
| 61 | + t.Errorf("recycler not called, count = %d, want 1", r.count) |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +func TestClaim(t *testing.T) { |
| 69 | + for _, tc := range []struct { |
| 70 | + name string |
| 71 | + slot *Buffer |
| 72 | + }{ |
| 73 | + {"nil slot", nil}, |
| 74 | + {"non-nil slot", New(make([]byte, 10), nil)}, |
| 75 | + } { |
| 76 | + t.Run(tc.name, func(t *testing.T) { |
| 77 | + want := tc.slot |
| 78 | + claimed := Claim(&tc.slot) |
| 79 | + if tc.slot != nil { |
| 80 | + t.Error("slot should be nil after Claim") |
| 81 | + } |
| 82 | + if claimed != want { |
| 83 | + t.Error("claimed buffer does not match original") |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestReleaseAll(t *testing.T) { |
| 90 | + for _, tc := range []struct { |
| 91 | + name string |
| 92 | + bufs []*Buffer |
| 93 | + }{ |
| 94 | + {"nil slice", nil}, |
| 95 | + {"empty slice", []*Buffer{}}, |
| 96 | + {"slice with nil buffers", []*Buffer{nil, nil}}, |
| 97 | + {"slice with non-nil buffers", []*Buffer{New(make([]byte, 10), nil), New(make([]byte, 20), nil)}}, |
| 98 | + } { |
| 99 | + t.Run(tc.name, func(t *testing.T) { |
| 100 | + l := len(tc.bufs) |
| 101 | + ReleaseAll(tc.bufs) // must not panic |
| 102 | + if len(tc.bufs) != l { |
| 103 | + t.Errorf("length of bufs changed: got %d, want %d", len(tc.bufs), l) |
| 104 | + } |
| 105 | + for i, b := range tc.bufs { |
| 106 | + if b != nil { |
| 107 | + t.Errorf("bufs[%d] should be nil after ReleaseAll", i) |
| 108 | + } |
| 109 | + } |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments