mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-17 15:23:44 +08:00
test(render): add concurrency test for shared Reader headers map
This commit is contained in:
parent
50453fd62e
commit
283f36e5cc
@ -7,8 +7,10 @@ package render
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@ -48,3 +50,27 @@ func TestReaderRenderDoesNotModifyHeaders(t *testing.T) {
|
||||
require.Equal(t, "public", w.Header().Get("Cache-Control"))
|
||||
require.Equal(t, "hello, world!", w.Body.String())
|
||||
}
|
||||
|
||||
// Run with -race: rendering concurrently with a shared Headers map must not
|
||||
// write to the map.
|
||||
func TestReaderRenderConcurrentSharedHeaders(t *testing.T) {
|
||||
headers := map[string]string{"Cache-Control": "public"}
|
||||
|
||||
const goroutines = 50
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(goroutines)
|
||||
for range goroutines {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
r := Reader{
|
||||
ContentLength: 5,
|
||||
Reader: strings.NewReader("hello"),
|
||||
Headers: headers,
|
||||
}
|
||||
assert.NoError(t, r.Render(httptest.NewRecorder()))
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
require.Equal(t, map[string]string{"Cache-Control": "public"}, headers)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user