From eb028582806fab9a5f4b2a0903cad390db7da8b2 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 23 Dec 2023 23:41:58 +0100 Subject: [PATCH] [linkcheck] Refactor: Improve wait-code Signed-off-by: Knut Ahlers --- internal/linkcheck/linkcheck.go | 2 -- internal/linkcheck/linkcheck_test.go | 24 ++++++++++++++++++++---- internal/linkcheck/resolver.go | 1 + 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/internal/linkcheck/linkcheck.go b/internal/linkcheck/linkcheck.go index a29efa2..665ca7f 100644 --- a/internal/linkcheck/linkcheck.go +++ b/internal/linkcheck/linkcheck.go @@ -87,7 +87,6 @@ func (c Checker) scanPartsConnected(parts []string, connector string) (links []s for ptJoin := 2; ptJoin < len(parts); ptJoin++ { for i := 0; i <= len(parts)-ptJoin; i++ { - wg.Add(1) c.res.Resolve(resolverQueueEntry{ Link: strings.Join(parts[i:i+ptJoin], connector), Callback: func(link string) { links = str.AppendIfMissing(links, link) }, @@ -108,7 +107,6 @@ func (c Checker) scanPlainNoObfuscate(message string) (links []string) { ) for _, part := range parts { - wg.Add(1) c.res.Resolve(resolverQueueEntry{ Link: part, Callback: func(link string) { links = str.AppendIfMissing(links, link) }, diff --git a/internal/linkcheck/linkcheck_test.go b/internal/linkcheck/linkcheck_test.go index 68c5b28..5f4edc5 100644 --- a/internal/linkcheck/linkcheck_test.go +++ b/internal/linkcheck/linkcheck_test.go @@ -8,6 +8,7 @@ import ( "strconv" "testing" + "github.com/Luzifer/go_helpers/v2/str" "github.com/gorilla/mux" "github.com/stretchr/testify/assert" ) @@ -59,9 +60,10 @@ func TestScanForLinks(t *testing.T) { c := New() for _, testCase := range []struct { - Heuristic bool - Message string - ExpectedLinks []string + Heuristic bool + Message string + ExpectedLinks []string + ExpectedContains bool }{ // Case: full URL is present in the message { @@ -187,7 +189,21 @@ func TestScanForLinks(t *testing.T) { } sort.Strings(linksFound) - assert.Equal(t, testCase.ExpectedLinks, linksFound, "links from message %q", testCase.Message) + if testCase.ExpectedContains { + for _, expLnk := range testCase.ExpectedLinks { + assert.Contains(t, linksFound, expLnk) + } + + var extraLinks []string + for _, link := range linksFound { + if !str.StringInSlice(link, testCase.ExpectedLinks) { + extraLinks = append(extraLinks, link) + } + } + t.Logf("extra links found: %v", extraLinks) + } else { + assert.Equal(t, testCase.ExpectedLinks, linksFound) + } }) } } diff --git a/internal/linkcheck/resolver.go b/internal/linkcheck/resolver.go index 8da0b74..637c8c7 100644 --- a/internal/linkcheck/resolver.go +++ b/internal/linkcheck/resolver.go @@ -74,6 +74,7 @@ func withSkipVerify() func(*resolver) { } func (r resolver) Resolve(qe resolverQueueEntry) { + qe.WaitGroup.Add(1) r.resolverC <- qe }