[linkcheck] Refactor: Improve wait-code

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-12-23 23:41:58 +01:00
parent 61bab3c984
commit eb02858280
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
3 changed files with 21 additions and 6 deletions

View file

@ -87,7 +87,6 @@ func (c Checker) scanPartsConnected(parts []string, connector string) (links []s
for ptJoin := 2; ptJoin < len(parts); ptJoin++ { for ptJoin := 2; ptJoin < len(parts); ptJoin++ {
for i := 0; i <= len(parts)-ptJoin; i++ { for i := 0; i <= len(parts)-ptJoin; i++ {
wg.Add(1)
c.res.Resolve(resolverQueueEntry{ c.res.Resolve(resolverQueueEntry{
Link: strings.Join(parts[i:i+ptJoin], connector), Link: strings.Join(parts[i:i+ptJoin], connector),
Callback: func(link string) { links = str.AppendIfMissing(links, link) }, 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 { for _, part := range parts {
wg.Add(1)
c.res.Resolve(resolverQueueEntry{ c.res.Resolve(resolverQueueEntry{
Link: part, Link: part,
Callback: func(link string) { links = str.AppendIfMissing(links, link) }, Callback: func(link string) { links = str.AppendIfMissing(links, link) },

View file

@ -8,6 +8,7 @@ import (
"strconv" "strconv"
"testing" "testing"
"github.com/Luzifer/go_helpers/v2/str"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -62,6 +63,7 @@ func TestScanForLinks(t *testing.T) {
Heuristic bool Heuristic bool
Message string Message string
ExpectedLinks []string ExpectedLinks []string
ExpectedContains bool
}{ }{
// Case: full URL is present in the message // Case: full URL is present in the message
{ {
@ -187,7 +189,21 @@ func TestScanForLinks(t *testing.T) {
} }
sort.Strings(linksFound) 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)
}
}) })
} }
} }

View file

@ -74,6 +74,7 @@ func withSkipVerify() func(*resolver) {
} }
func (r resolver) Resolve(qe resolverQueueEntry) { func (r resolver) Resolve(qe resolverQueueEntry) {
qe.WaitGroup.Add(1)
r.resolverC <- qe r.resolverC <- qe
} }