|
@@ -10,6 +10,7 @@ import (
|
|
"context"
|
|
"context"
|
|
"fmt"
|
|
"fmt"
|
|
"io"
|
|
"io"
|
|
|
|
+ "sync"
|
|
"testing"
|
|
"testing"
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
|
"github.com/syncthing/syncthing/lib/db"
|
|
@@ -47,38 +48,52 @@ func TestIndexhandlerConcurrency(t *testing.T) {
|
|
const msgs = 5e2
|
|
const msgs = 5e2
|
|
const files = 1e3
|
|
const files = 1e3
|
|
|
|
|
|
- recvd := 0
|
|
|
|
|
|
+ recvdEntries := 0
|
|
|
|
+ recvdBatches := 0
|
|
|
|
+ var wg sync.WaitGroup
|
|
m2.IndexUpdateCalls(func(_ protocol.Connection, idxUp *protocol.IndexUpdate) error {
|
|
m2.IndexUpdateCalls(func(_ protocol.Connection, idxUp *protocol.IndexUpdate) error {
|
|
for j := 0; j < files; j++ {
|
|
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)
|
|
t.Error("wrong filename", n)
|
|
}
|
|
}
|
|
|
|
+ recvdEntries++
|
|
}
|
|
}
|
|
- recvd++
|
|
|
|
|
|
+ recvdBatches++
|
|
|
|
+ wg.Done()
|
|
return nil
|
|
return nil
|
|
})
|
|
})
|
|
|
|
|
|
b1 := db.NewFileInfoBatch(func(fs []protocol.FileInfo) error {
|
|
b1 := db.NewFileInfoBatch(func(fs []protocol.FileInfo) error {
|
|
return c1.IndexUpdate(ctx, "foo", fs)
|
|
return c1.IndexUpdate(ctx, "foo", fs)
|
|
})
|
|
})
|
|
|
|
+ sentEntries := 0
|
|
for i := 0; i < msgs; i++ {
|
|
for i := 0; i < msgs; i++ {
|
|
for j := 0; j < files; j++ {
|
|
for j := 0; j < files; j++ {
|
|
b1.Append(protocol.FileInfo{
|
|
b1.Append(protocol.FileInfo{
|
|
Name: fmt.Sprintf("f%d-%d", i, j),
|
|
Name: fmt.Sprintf("f%d-%d", i, j),
|
|
Blocks: []protocol.BlockInfo{{Hash: make([]byte, 32)}},
|
|
Blocks: []protocol.BlockInfo{{Hash: make([]byte, 32)}},
|
|
})
|
|
})
|
|
|
|
+ sentEntries++
|
|
}
|
|
}
|
|
|
|
+ wg.Add(1)
|
|
if err := b1.Flush(); err != nil {
|
|
if err := b1.Flush(); err != nil {
|
|
t.Fatal(err)
|
|
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)
|
|
c1.Close(io.EOF)
|
|
c2.Close(io.EOF)
|
|
c2.Close(io.EOF)
|
|
<-c1.Closed()
|
|
<-c1.Closed()
|
|
<-c2.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)
|
|
}
|
|
}
|
|
}
|
|
}
|