From 383f8a1f67c7172ab0606ef1605631e33408f4d3 Mon Sep 17 00:00:00 2001 From: ahrav Date: Mon, 22 Jan 2024 09:40:32 -0800 Subject: [PATCH] [chore] - reduce test time (#2321) * reduce test time * remove commented out code --- pkg/gitparse/gitparse_test.go | 60 +++++++++++------------------------ 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/pkg/gitparse/gitparse_test.go b/pkg/gitparse/gitparse_test.go index f343aead0011..fa35ebf711da 100644 --- a/pkg/gitparse/gitparse_test.go +++ b/pkg/gitparse/gitparse_test.go @@ -741,16 +741,6 @@ func TestIndividualCommitParsing(t *testing.T) { } j++ } - //for _, pass := range test.passes { - // if !test.function(false, pass.latestState, pass.line) { - // t.Errorf("%s: Parser did not recognize correct line. (%s)", name, string(pass.line)) - // } - //} - //for _, fail := range test.fails { - // if test.function(false, fail.latestState, fail.line) { - // t.Errorf("%s: Parser did not recognize incorrect line. (%s)", name, string(fail.line)) - // } - //} } } @@ -802,24 +792,6 @@ func TestStagedDiffParsing(t *testing.T) { Content: *bytes.NewBuffer([]byte("The Nameless is the origin of Heaven and Earth;\nThe named is the mother of all things.\n\nTherefore let there always be non-being,\n so we may see their subtlety,\nAnd let there always be being,\n so we may see their outcome.\nThe two are the same,\nBut after they are produced,\n they have different names.\nThey both may be called deep and profound.\nDeeper and more profound,\nThe door of all subtleties!\n")), IsBinary: false, }, - //{ - // PathB: "", - // LineStart: 0, - // Content: *bytes.NewBuffer([]byte("\n")), - // IsBinary: false, - //}, - //{ - // PathB: "", - // LineStart: 0, - // Content: *bytes.NewBuffer([]byte("\n")), - // IsBinary: false, - //}, - //{ - // PathB: "", - // LineStart: 0, - // Content: *bytes.NewBuffer([]byte("\n")), - // IsBinary: false, - //}, }, }, } @@ -1112,27 +1084,31 @@ index 0000000..5af88a8 func TestMaxDiffSize(t *testing.T) { parser := NewParser() - bigBytes := bytes.Buffer{} - bigBytes.WriteString(singleCommitSingleDiff) + builder := strings.Builder{} + builder.WriteString(singleCommitSingleDiff) + + // Generate a diff that is larger than the maxDiffSize. for i := 0; i <= parser.maxDiffSize/1024+10; i++ { - bigBytes.WriteString("+") - for n := 0; n < 1024; n++ { - bigBytes.Write([]byte("0")) - } - bigBytes.WriteString("\n") + builder.WriteString("+" + strings.Repeat("0", 1024) + "\n") } - bigReader := bytes.NewReader(bigBytes.Bytes()) + bigReader := strings.NewReader(builder.String()) + + commitChan := make(chan Commit, 1) // Buffer to prevent blocking + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // Timeout to prevent long wait + defer cancel() - commitChan := make(chan Commit) go func() { - parser.FromReader(context.Background(), bigReader, commitChan, false) + parser.FromReader(ctx, bigReader, commitChan, false) }() - commit := <-commitChan - if commit.Diffs[0].Content.Len() > parser.maxDiffSize+1024 { - t.Errorf("diff did not match MaxDiffSize. Got: %d, expected (max): %d", commit.Diffs[0].Content.Len(), parser.maxDiffSize+1024) + select { + case commit := <-commitChan: + if commit.Diffs[0].Content.Len() > parser.maxDiffSize+1024 { + t.Errorf("diff did not match MaxDiffSize. Got: %d, expected (max): %d", commit.Diffs[0].Content.Len(), parser.maxDiffSize+1024) + } + case <-ctx.Done(): + t.Fatal("Test timed out") } - } func TestMaxCommitSize(t *testing.T) {