lib/model: Deflake new IndexHandlerTest (#9412)

The new test has a flakiness factor on slow platforms, where the close
on the sending connection races with the last index message, potentially
messing up the count. This adds a wait to ensure that all sent messages
are received, or the test will eventually fail with a timeout.
This commit is contained in:
Jakob Borg 2024-02-11 09:03:12 +01:00 committed by GitHub
parent e1dd36561d
commit 7ebeaefe77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,6 +10,7 @@ import (
"context"
"fmt"
"io"
"sync"
"testing"
"github.com/syncthing/syncthing/lib/db"
@ -47,38 +48,52 @@ func TestIndexhandlerConcurrency(t *testing.T) {
const msgs = 5e2
const files = 1e3
recvd := 0
recvdEntries := 0
recvdBatches := 0
var wg sync.WaitGroup
m2.IndexUpdateCalls(func(_ protocol.Connection, idxUp *protocol.IndexUpdate) error {
for j := 0; j < files; j++ {
if n := idxUp.Files[j].Name; n != fmt.Sprintf("f%d-%d", recvd, j) {
if n := idxUp.Files[j].Name; n != fmt.Sprintf("f%d-%d", recvdBatches, j) {
t.Error("wrong filename", n)
}
recvdEntries++
}
recvd++
recvdBatches++
wg.Done()
return nil
})
b1 := db.NewFileInfoBatch(func(fs []protocol.FileInfo) error {
return c1.IndexUpdate(ctx, "foo", fs)
})
sentEntries := 0
for i := 0; i < msgs; i++ {
for j := 0; j < files; j++ {
b1.Append(protocol.FileInfo{
Name: fmt.Sprintf("f%d-%d", i, j),
Blocks: []protocol.BlockInfo{{Hash: make([]byte, 32)}},
})
sentEntries++
}
wg.Add(1)
if err := b1.Flush(); err != nil {
t.Fatal(err)
}
}
// Every sent IndexUpdate should be matched by a corresponding index
// message on the other side. Use the waitgroup to wait for this to
// complete, as otherwise the Close below can race with the last
// outgoing index message and the count between sent and received is
// wrong.
wg.Wait()
c1.Close(io.EOF)
c2.Close(io.EOF)
<-c1.Closed()
<-c2.Closed()
if recvd != msgs-1 {
t.Error("didn't receive all expected messages")
if recvdEntries != sentEntries {
t.Error("didn't receive all expected messages", recvdEntries, sentEntries)
}
}