4
4
"io"
5
5
"strings"
6
6
"testing"
7
- "time"
8
7
9
8
"github.com/stretchr/testify/assert"
10
9
"github.com/stretchr/testify/require"
@@ -14,15 +13,12 @@ import (
14
13
15
14
// Fix for TestBody_PartialRead
16
15
func TestBody_PartialRead (t * testing.T ) {
17
- // Create a small pool for testing
18
- pool := collector .NewBodyBufferPool (10 * 1024 , 5 * 1024 ) // 10KB pool, 5KB max per body
19
-
20
16
// Create test data
21
17
testData := "This is test data for partial reading"
22
18
testReader := io .NopCloser (strings .NewReader (testData ))
23
19
24
20
// Create a Body
25
- body := collector .NewBody (testReader , pool )
21
+ body := collector .NewBody (testReader , 100 )
26
22
27
23
// Read first 10 bytes
28
24
buf := make ([]byte , 10 )
@@ -50,15 +46,12 @@ func TestBody_PartialRead(t *testing.T) {
50
46
51
47
// Fix for TestBody_ReadAfterClose
52
48
func TestBody_ReadAfterClose (t * testing.T ) {
53
- // Create a pool
54
- pool := collector .NewBodyBufferPool (10 * 1024 , 5 * 1024 )
55
-
56
49
// Create test data
57
50
testData := "This is test data"
58
51
testReader := io .NopCloser (strings .NewReader (testData ))
59
52
60
53
// Create a Body
61
- body := collector .NewBody (testReader , pool )
54
+ body := collector .NewBody (testReader , 100 )
62
55
63
56
// Close the body
64
57
err := body .Close ()
@@ -72,129 +65,3 @@ func TestBody_ReadAfterClose(t *testing.T) {
72
65
assert .Error (t , err )
73
66
assert .Equal (t , collector .ErrBodyClosed , err )
74
67
}
75
-
76
- // Fix for TestBodyBufferPool_EnsureCapacity
77
- func TestBodyBufferPool_EnsureCapacity (t * testing.T ) {
78
- // Create a small pool for testing
79
- maxPoolSize := int64 (1000 ) // 1000 bytes
80
- maxBodySize := int64 (200 ) // 200 bytes per body
81
- pool := collector .NewBodyBufferPool (maxPoolSize , maxBodySize )
82
-
83
- // Create buffers that will fit in the pool
84
- buffers := make ([]* collector.BodyBuffer , 0 )
85
-
86
- // Add 5 buffers of 100 bytes each (total 500 bytes)
87
- for i := 0 ; i < 5 ; i ++ {
88
- // Get a buffer from the pool
89
- buffer := pool .GetBuffer ()
90
-
91
- // Fill with 100 bytes of data
92
- data := strings .Repeat ("a" , 100 )
93
- buffer .WriteString (data )
94
-
95
- buffers = append (buffers , buffer )
96
-
97
- // Sleep to ensure different timestamps
98
- time .Sleep (10 * time .Millisecond )
99
- }
100
-
101
- // Verify all buffers have data
102
- for i , buffer := range buffers {
103
- assert .Equal (t , 100 , buffer .Len (), "Buffer %d should have data" , i )
104
- }
105
-
106
- // Add one more large buffer that will trigger garbage collection
107
- buffer := pool .GetBuffer ()
108
- buffer .WriteString (strings .Repeat ("b" , 600 )) // 600 bytes
109
-
110
- // The newest buffer should have data
111
- assert .Equal (t , 600 , buffer .Len ())
112
-
113
- // Verify the combined data is still under the pool max size
114
- // This is difficult to test directly without exposing pool internals
115
- // But we can verify the latest buffer is intact
116
- assert .Equal (t , strings .Repeat ("b" , 600 ), buffer .String ())
117
- }
118
-
119
- func TestBody_IOReadAllWithoutClose (t * testing.T ) {
120
- pool := collector .NewBodyBufferPool (100 , 50 )
121
-
122
- testData := strings .Repeat ("X" , 30 ) // 30 bytes
123
- testReader := io .NopCloser (strings .NewReader (testData ))
124
- body := collector .NewBody (testReader , pool )
125
-
126
- // Some clients do io.ReadAll without Close()
127
- data , err := io .ReadAll (body )
128
- require .NoError (t , err )
129
- assert .Equal (t , testData , string (data ))
130
-
131
- // Ensure pool tracks size even without Close()
132
- currentSize := pool .GetCurrentSize ()
133
- assert .Equal (t , int64 (30 ), currentSize , "Pool should track buffer size without Close()" )
134
- }
135
-
136
- func TestBody_PoolCapacityEnforcement_Multiple_ReadAll (t * testing.T ) {
137
- pool := collector .NewBodyBufferPool (100 , 50 ) // Small pool to force cleanup
138
-
139
- var bodies []* collector.Body
140
-
141
- // Create bodies that exceed pool capacity
142
- for i := 0 ; i < 5 ; i ++ {
143
- testData := strings .Repeat ("A" , 40 ) // 40 bytes each
144
- testReader := io .NopCloser (strings .NewReader (testData ))
145
- body := collector .NewBody (testReader , pool )
146
-
147
- // Consume all data, do not close
148
- _ , err := io .ReadAll (body )
149
- require .NoError (t , err )
150
-
151
- bodies = append (bodies , body )
152
- }
153
-
154
- poolBytesLen := 0
155
- bodySizes := 0
156
- for _ , body := range bodies {
157
- poolBytesLen += len (body .Bytes ())
158
- bodySizes += int (body .Size ())
159
- }
160
-
161
- // Ensure bodies are cleaned up
162
- assert .LessOrEqual (t , poolBytesLen , 100 , "Some bodies should be cleaned up when pool exceeds capacity" )
163
- assert .LessOrEqual (t , bodySizes , 100 , "Total body sizes should not exceed pool capacity" )
164
- }
165
-
166
- func TestBody_PoolCapacityEnforcement_Multiple_Read_Close (t * testing.T ) {
167
- pool := collector .NewBodyBufferPool (100 , 50 ) // Small pool to force cleanup
168
-
169
- var bodies []* collector.Body
170
-
171
- // Create bodies that exceed pool capacity
172
- buf := make ([]byte , 30 )
173
- for i := 0 ; i < 5 ; i ++ {
174
- testData := strings .Repeat ("A" , 40 ) // 40 bytes each
175
- testReader := io .NopCloser (strings .NewReader (testData ))
176
- body := collector .NewBody (testReader , pool )
177
-
178
- // Only read a part of the body
179
- n , err := body .Read (buf )
180
- require .NoError (t , err )
181
- assert .Equal (t , 30 , n )
182
-
183
- // Then close the body
184
- err = body .Close ()
185
- require .NoError (t , err )
186
-
187
- bodies = append (bodies , body )
188
- }
189
-
190
- poolBytesLen := 0
191
- bodySizes := 0
192
- for _ , body := range bodies {
193
- poolBytesLen += len (body .Bytes ())
194
- bodySizes += int (body .Size ())
195
- }
196
-
197
- // Ensure bodies are cleaned up
198
- assert .LessOrEqual (t , poolBytesLen , 100 , "Some bodies should be cleaned up when pool exceeds capacity" )
199
- assert .LessOrEqual (t , bodySizes , 100 , "Total body sizes should not exceed pool capacity" )
200
- }
0 commit comments