MCPcopy
hub / github.com/jackc/pgx / TestPutHandlesWrongSizedBuffers

Function TestPutHandlesWrongSizedBuffers

internal/iobufpool/iobufpool_test.go:38–69  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

36}
37
38func TestPutHandlesWrongSizedBuffers(t *testing.T) {
39 for putBufSize := range []int{0, 1, 128, 250, 256, 257, 1023, 1024, 1025, 1 << 28} {
40 putBuf := make([]byte, putBufSize)
41 iobufpool.Put(&putBuf)
42
43 tests := []struct {
44 requestedLen int
45 expectedCap int
46 }{
47 {requestedLen: 0, expectedCap: 256},
48 {requestedLen: 128, expectedCap: 256},
49 {requestedLen: 255, expectedCap: 256},
50 {requestedLen: 256, expectedCap: 256},
51 {requestedLen: 257, expectedCap: 512},
52 {requestedLen: 511, expectedCap: 512},
53 {requestedLen: 512, expectedCap: 512},
54 {requestedLen: 513, expectedCap: 1024},
55 {requestedLen: 1023, expectedCap: 1024},
56 {requestedLen: 1024, expectedCap: 1024},
57 {requestedLen: 33554431, expectedCap: 33554432},
58 {requestedLen: 33554432, expectedCap: 33554432},
59
60 // Above 32 MiB skip the pool and allocate exactly the requested size.
61 {requestedLen: 33554433, expectedCap: 33554433},
62 }
63 for _, tt := range tests {
64 getBuf := iobufpool.Get(tt.requestedLen)
65 assert.Equalf(t, tt.requestedLen, len(*getBuf), "len(putBuf): %d, requestedLen: %d", len(putBuf), tt.requestedLen)
66 assert.Equalf(t, tt.expectedCap, cap(*getBuf), "cap(putBuf): %d, requestedLen: %d", cap(putBuf), tt.requestedLen)
67 }
68 }
69}
70
71func TestPutGetBufferReuse(t *testing.T) {
72 // There is no way to guarantee a buffer will be reused. It should be, but a GC between the Put and the Get will cause

Callers

nothing calls this directly

Calls 2

PutFunction · 0.92
GetFunction · 0.92

Tested by

no test coverage detected