Skip to content

Commit 8be2cc4

Browse files
authored
Reduce memory usage in testgit (#15306)
* reduce memory use in rawtest * just use hashsum for diffs Signed-off-by: Andrew Thornton <[email protected]>
1 parent b101fa8 commit 8be2cc4

File tree

2 files changed

+69
-25
lines changed

2 files changed

+69
-25
lines changed

integrations/git_test.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ func rawTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS s
208208

209209
// Request raw paths
210210
req := NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", little))
211-
resp := session.MakeRequest(t, req, http.StatusOK)
212-
assert.Equal(t, littleSize, resp.Body.Len())
211+
resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
212+
assert.Equal(t, littleSize, resp.Length)
213213

214214
setting.CheckLFSVersion()
215215
if setting.LFS.StartServer {
216216
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", littleLFS))
217-
resp = session.MakeRequest(t, req, http.StatusOK)
217+
resp := session.MakeRequest(t, req, http.StatusOK)
218218
assert.NotEqual(t, littleSize, resp.Body.Len())
219219
assert.LessOrEqual(t, resp.Body.Len(), 1024)
220220
if resp.Body.Len() != littleSize && resp.Body.Len() <= 1024 {
@@ -224,12 +224,12 @@ func rawTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS s
224224

225225
if !testing.Short() {
226226
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", big))
227-
resp = session.MakeRequest(t, req, http.StatusOK)
228-
assert.Equal(t, bigSize, resp.Body.Len())
227+
resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
228+
assert.Equal(t, bigSize, resp.Length)
229229

230230
if setting.LFS.StartServer {
231231
req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", bigLFS))
232-
resp = session.MakeRequest(t, req, http.StatusOK)
232+
resp := session.MakeRequest(t, req, http.StatusOK)
233233
assert.NotEqual(t, bigSize, resp.Body.Len())
234234
if resp.Body.Len() != bigSize && resp.Body.Len() <= 1024 {
235235
assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
@@ -450,27 +450,27 @@ func doMergeFork(ctx, baseCtx APITestContext, baseBranch, headBranch string) fun
450450
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
451451

452452
// Then get the diff string
453-
var diffStr string
453+
var diffHash string
454454
t.Run("GetDiff", func(t *testing.T) {
455455
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(baseCtx.Username), url.PathEscape(baseCtx.Reponame), pr.Index))
456-
resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
457-
diffStr = resp.Body.String()
456+
resp := ctx.Session.MakeRequestNilResponseHashSumRecorder(t, req, http.StatusOK)
457+
diffHash = string(resp.Hash.Sum(nil))
458458
})
459459

460460
// Now: Merge the PR & make sure that doesn't break the PR page or change its diff
461461
t.Run("MergePR", doAPIMergePullRequest(baseCtx, baseCtx.Username, baseCtx.Reponame, pr.Index))
462462
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
463-
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffStr))
463+
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash))
464464

465465
// Then: Delete the head branch & make sure that doesn't break the PR page or change its diff
466466
t.Run("DeleteHeadBranch", doBranchDelete(baseCtx, baseCtx.Username, baseCtx.Reponame, headBranch))
467467
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
468-
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffStr))
468+
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash))
469469

470470
// Delete the head repository & make sure that doesn't break the PR page or change its diff
471471
t.Run("DeleteHeadRepository", doAPIDeleteRepository(ctx))
472472
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
473-
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffStr))
473+
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash))
474474
}
475475
}
476476

@@ -514,22 +514,14 @@ func doEnsureCanSeePull(ctx APITestContext, pr api.PullRequest) func(t *testing.
514514
}
515515
}
516516

517-
func doEnsureDiffNoChange(ctx APITestContext, pr api.PullRequest, diffStr string) func(t *testing.T) {
517+
func doEnsureDiffNoChange(ctx APITestContext, pr api.PullRequest, diffHash string) func(t *testing.T) {
518518
return func(t *testing.T) {
519519
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index))
520-
resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
521-
expectedMaxLen := len(diffStr)
522-
if expectedMaxLen > 800 {
523-
expectedMaxLen = 800
524-
}
525-
actual := resp.Body.String()
526-
actualMaxLen := len(actual)
527-
if actualMaxLen > 800 {
528-
actualMaxLen = 800
529-
}
520+
resp := ctx.Session.MakeRequestNilResponseHashSumRecorder(t, req, http.StatusOK)
521+
actual := string(resp.Hash.Sum(nil))
530522

531-
equal := diffStr == actual
532-
assert.True(t, equal, "Unexpected change in the diff string: expected: %s but was actually: %s", diffStr[:expectedMaxLen], actual[:actualMaxLen])
523+
equal := diffHash == actual
524+
assert.True(t, equal, "Unexpected change in the diff string: expected hash: %s but was actually: %s", diffHash, actual)
533525
}
534526
}
535527

integrations/integration_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"context"
1010
"database/sql"
1111
"fmt"
12+
"hash"
13+
"hash/fnv"
1214
"io"
1315
"net/http"
1416
"net/http/cookiejar"
@@ -58,6 +60,26 @@ func NewNilResponseRecorder() *NilResponseRecorder {
5860
}
5961
}
6062

63+
type NilResponseHashSumRecorder struct {
64+
httptest.ResponseRecorder
65+
Hash hash.Hash
66+
Length int
67+
}
68+
69+
func (n *NilResponseHashSumRecorder) Write(b []byte) (int, error) {
70+
_, _ = n.Hash.Write(b)
71+
n.Length += len(b)
72+
return len(b), nil
73+
}
74+
75+
// NewRecorder returns an initialized ResponseRecorder.
76+
func NewNilResponseHashSumRecorder() *NilResponseHashSumRecorder {
77+
return &NilResponseHashSumRecorder{
78+
Hash: fnv.New32(),
79+
ResponseRecorder: *httptest.NewRecorder(),
80+
}
81+
}
82+
6183
func TestMain(m *testing.M) {
6284
defer log.Close()
6385

@@ -284,6 +306,23 @@ func (s *TestSession) MakeRequestNilResponseRecorder(t testing.TB, req *http.Req
284306
return resp
285307
}
286308

309+
func (s *TestSession) MakeRequestNilResponseHashSumRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseHashSumRecorder {
310+
t.Helper()
311+
baseURL, err := url.Parse(setting.AppURL)
312+
assert.NoError(t, err)
313+
for _, c := range s.jar.Cookies(baseURL) {
314+
req.AddCookie(c)
315+
}
316+
resp := MakeRequestNilResponseHashSumRecorder(t, req, expectedStatus)
317+
318+
ch := http.Header{}
319+
ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
320+
cr := http.Request{Header: ch}
321+
s.jar.SetCookies(baseURL, cr.Cookies())
322+
323+
return resp
324+
}
325+
287326
const userPassword = "password"
288327

289328
var loginSessionCache = make(map[string]*TestSession, 10)
@@ -429,6 +468,19 @@ func MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedSta
429468
return recorder
430469
}
431470

471+
func MakeRequestNilResponseHashSumRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseHashSumRecorder {
472+
t.Helper()
473+
recorder := NewNilResponseHashSumRecorder()
474+
c.ServeHTTP(recorder, req)
475+
if expectedStatus != NoExpectedStatus {
476+
if !assert.EqualValues(t, expectedStatus, recorder.Code,
477+
"Request: %s %s", req.Method, req.URL.String()) {
478+
logUnexpectedResponse(t, &recorder.ResponseRecorder)
479+
}
480+
}
481+
return recorder
482+
}
483+
432484
// logUnexpectedResponse logs the contents of an unexpected response.
433485
func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
434486
t.Helper()

0 commit comments

Comments
 (0)