aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Borg <jakob@kastelo.net>2024-02-11 09:03:12 +0100
committerGitHub <noreply@github.com>2024-02-11 09:03:12 +0100
commit7ebeaefe77d80df735622e4d8718a8c5614f0ecb (patch)
tree9024695c0309df4fc05d142397ecb2eae345302b
parente1dd36561d51589a76c5db08664a7dbbc343382d (diff)
downloadsyncthing-7ebeaefe77d80df735622e4d8718a8c5614f0ecb.tar.gz
syncthing-7ebeaefe77d80df735622e4d8718a8c5614f0ecb.zip
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.
-rw-r--r--lib/model/indexhandler_test.go25
1 files changed, 20 insertions, 5 deletions
diff --git a/lib/model/indexhandler_test.go b/lib/model/indexhandler_test.go
index ba571028d..bf9156509 100644
--- a/lib/model/indexhandler_test.go
+++ b/lib/model/indexhandler_test.go
@@ -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)
}
}