@@ -1115,3 +1115,70 @@ func TestResponseChain_BurstWithPoolExhaustion(t *testing.T) {
11151115 require .NoError (t , err )
11161116 }
11171117}
1118+
1119+ func TestResponseChain_FullResponseBytes_Race (t * testing.T ) {
1120+ resp := & http.Response {
1121+ StatusCode : 200 ,
1122+ Status : "200 OK" ,
1123+ }
1124+ chain := NewResponseChain (resp , 1024 )
1125+ chain .headers .WriteString ("Header: Value\r \n " )
1126+ chain .body .WriteString ("Body Content" )
1127+
1128+ data := chain .FullResponseBytes ()
1129+ initialContent := string (data )
1130+
1131+ // Trigger buffer reuse
1132+ // We need to get a buffer from the pool.
1133+
1134+ found := false
1135+ for i := 0 ; i < 100 ; i ++ {
1136+ b := getBuffer ()
1137+ b .WriteString ("OVERWRITTEN_DATA_XXXXXXXXXXXXXXXX" )
1138+
1139+ if string (data ) != initialContent {
1140+ found = true
1141+ t .Logf ("Iteration %d: Content changed to %q" , i , string (data ))
1142+
1143+ break
1144+ }
1145+
1146+ putBuffer (b )
1147+ }
1148+
1149+ if found {
1150+ t .Fatalf ("Race detected! Content changed from %q" , initialContent )
1151+ }
1152+ }
1153+
1154+ func TestResponseChain_Close_Idempotency (t * testing.T ) {
1155+ resp := & http.Response {
1156+ StatusCode : 200 ,
1157+ }
1158+ rc := NewResponseChain (resp , 1024 )
1159+
1160+ rc .Close ()
1161+
1162+ defer func () {
1163+ if r := recover (); r != nil {
1164+ t .Errorf ("Close() panicked on second call: %v" , r )
1165+ }
1166+ }()
1167+
1168+ rc .Close ()
1169+ }
1170+
1171+ func TestLimitedBuffer_Pool (t * testing.T ) {
1172+ buf := getBuffer ()
1173+ defer putBuffer (buf )
1174+
1175+ lb := & limitedBuffer {buf : buf , maxCap : 1024 * 1024 }
1176+ data := bytes .Repeat ([]byte ("A" ), 100 * 1024 ) // 100KB
1177+ r := bytes .NewReader (data )
1178+
1179+ n , err := lb .ReadFrom (r )
1180+ require .NoError (t , err )
1181+ require .Equal (t , int64 (len (data )), n )
1182+ require .Equal (t , len (data ), buf .Len ())
1183+ require .Equal (t , data , buf .Bytes ())
1184+ }
0 commit comments