File: pool_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.17.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 384,244 kB
  • sloc: java: 13,538; makefile: 400; sh: 137
file content (197 lines) | stat: -rw-r--r-- 3,955 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package manager

import (
	"context"
	"sync"
	"sync/atomic"
	"testing"
)

func TestMaxSlicePool(t *testing.T) {
	pool := newMaxSlicePool(0)

	var wg sync.WaitGroup
	for i := 0; i < 100; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()

			// increase pool capacity by 2
			pool.ModifyCapacity(2)

			// remove 2 items
			bsOne, err := pool.Get(context.Background())
			if err != nil {
				t.Errorf("failed to get slice from pool: %v", err)
			}
			bsTwo, err := pool.Get(context.Background())
			if err != nil {
				t.Errorf("failed to get slice from pool: %v", err)
			}

			done := make(chan struct{})
			go func() {
				defer close(done)

				// attempt to remove a 3rd in parallel
				bs, err := pool.Get(context.Background())
				if err != nil {
					t.Errorf("failed to get slice from pool: %v", err)
				}
				pool.Put(bs)

				// attempt to remove a 4th that has been canceled
				ctx, cancel := context.WithCancel(context.Background())
				cancel()
				bs, err = pool.Get(ctx)
				if err == nil {
					pool.Put(bs)
					t.Errorf("expected no slice to be returned")
					return
				}
			}()

			pool.Put(bsOne)

			<-done

			pool.ModifyCapacity(-1)

			pool.Put(bsTwo)

			pool.ModifyCapacity(-1)

			// any excess returns should drop
			rando := make([]byte, 0)
			pool.Put(&rando)
		}()
	}
	wg.Wait()

	if e, a := 0, len(pool.slices); e != a {
		t.Errorf("expected %v, got %v", e, a)
	}
	if e, a := 0, len(pool.allocations); e != a {
		t.Errorf("expected %v, got %v", e, a)
	}
	if e, a := 0, pool.max; e != a {
		t.Errorf("expected %v, got %v", e, a)
	}

	_, err := pool.Get(context.Background())
	if err == nil {
		t.Errorf("expected error on zero capacity pool")
	}

	pool.Close()
}

func TestPoolShouldPreferAllocatedSlicesOverNewAllocations(t *testing.T) {
	pool := newMaxSlicePool(0)
	defer pool.Close()

	// Prepare pool: make it so that pool contains 1 allocated slice and 1 allocation permit
	pool.ModifyCapacity(2)
	initialSlice, err := pool.Get(context.Background())
	if err != nil {
		t.Errorf("failed to get slice from pool: %v", err)
	}
	pool.Put(initialSlice)

	for i := 0; i < 100; i++ {
		newSlice, err := pool.Get(context.Background())
		if err != nil {
			t.Errorf("failed to get slice from pool: %v", err)
			return
		}

		if newSlice != initialSlice {
			t.Errorf("pool allocated a new slice despite it having pre-allocated one")
			return
		}
		pool.Put(newSlice)
	}
}

type recordedPartPool struct {
	recordedAllocs      uint64
	recordedGets        uint64
	recordedOutstanding int64
	*maxSlicePool
}

func newRecordedPartPool(sliceSize int64) *recordedPartPool {
	sp := newMaxSlicePool(sliceSize)

	rp := &recordedPartPool{}

	allocator := sp.allocator
	sp.allocator = func() *[]byte {
		atomic.AddUint64(&rp.recordedAllocs, 1)
		return allocator()
	}

	rp.maxSlicePool = sp

	return rp
}

func (r *recordedPartPool) Get(ctx context.Context) (*[]byte, error) {
	atomic.AddUint64(&r.recordedGets, 1)
	atomic.AddInt64(&r.recordedOutstanding, 1)
	return r.maxSlicePool.Get(ctx)
}

func (r *recordedPartPool) Put(b *[]byte) {
	atomic.AddInt64(&r.recordedOutstanding, -1)
	r.maxSlicePool.Put(b)
}

func swapByteSlicePool(f func(sliceSize int64) byteSlicePool) func() {
	orig := newByteSlicePool

	newByteSlicePool = f

	return func() {
		newByteSlicePool = orig
	}
}

type syncSlicePool struct {
	sync.Pool
	sliceSize int64
}

func newSyncSlicePool(sliceSize int64) *syncSlicePool {
	p := &syncSlicePool{sliceSize: sliceSize}
	p.New = func() interface{} {
		bs := make([]byte, p.sliceSize)
		return &bs
	}
	return p
}

func (s *syncSlicePool) Get(ctx context.Context) (*[]byte, error) {
	select {
	case <-ctx.Done():
		return nil, ctx.Err()
	default:
		return s.Pool.Get().(*[]byte), nil
	}
}

func (s *syncSlicePool) Put(bs *[]byte) {
	s.Pool.Put(bs)
}

func (s *syncSlicePool) ModifyCapacity(_ int) {
	return
}

func (s *syncSlicePool) SliceSize() int64 {
	return s.sliceSize
}

func (s *syncSlicePool) Close() {
	return
}