2024-02-10 18:16:27 +00:00
|
|
|
// Copyright (C) 2024 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package model_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-02-11 08:03:12 +00:00
|
|
|
"sync"
|
2024-02-10 18:16:27 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
|
|
|
"github.com/syncthing/syncthing/lib/model/mocks"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
protomock "github.com/syncthing/syncthing/lib/protocol/mocks"
|
|
|
|
"github.com/syncthing/syncthing/lib/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIndexhandlerConcurrency(t *testing.T) {
|
|
|
|
// Verify that sending a lot of index update messages using the
|
|
|
|
// FileInfoBatch works and doesn't trigger the race detector.
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
ci := &protomock.ConnectionInfo{}
|
|
|
|
|
|
|
|
m1 := &mocks.Model{}
|
|
|
|
c1 := protocol.NewConnection(protocol.EmptyDeviceID, ar, bw, testutil.NoopCloser{}, m1, ci, protocol.CompressionNever, nil, nil)
|
|
|
|
c1.Start()
|
|
|
|
defer c1.Close(io.EOF)
|
|
|
|
|
|
|
|
m2 := &mocks.Model{}
|
|
|
|
c2 := protocol.NewConnection(protocol.EmptyDeviceID, br, aw, testutil.NoopCloser{}, m2, ci, protocol.CompressionNever, nil, nil)
|
|
|
|
c2.Start()
|
|
|
|
defer c2.Close(io.EOF)
|
|
|
|
|
2024-08-24 10:45:10 +00:00
|
|
|
c1.ClusterConfig(&protocol.ClusterConfig{})
|
|
|
|
c2.ClusterConfig(&protocol.ClusterConfig{})
|
|
|
|
c1.Index(ctx, &protocol.Index{Folder: "foo"})
|
|
|
|
c2.Index(ctx, &protocol.Index{Folder: "foo"})
|
2024-02-10 18:16:27 +00:00
|
|
|
|
|
|
|
const msgs = 5e2
|
|
|
|
const files = 1e3
|
|
|
|
|
2024-02-11 08:03:12 +00:00
|
|
|
recvdEntries := 0
|
|
|
|
recvdBatches := 0
|
|
|
|
var wg sync.WaitGroup
|
2024-02-10 18:16:27 +00:00
|
|
|
m2.IndexUpdateCalls(func(_ protocol.Connection, idxUp *protocol.IndexUpdate) error {
|
|
|
|
for j := 0; j < files; j++ {
|
2024-02-11 08:03:12 +00:00
|
|
|
if n := idxUp.Files[j].Name; n != fmt.Sprintf("f%d-%d", recvdBatches, j) {
|
2024-02-10 18:16:27 +00:00
|
|
|
t.Error("wrong filename", n)
|
|
|
|
}
|
2024-02-11 08:03:12 +00:00
|
|
|
recvdEntries++
|
2024-02-10 18:16:27 +00:00
|
|
|
}
|
2024-02-11 08:03:12 +00:00
|
|
|
recvdBatches++
|
|
|
|
wg.Done()
|
2024-02-10 18:16:27 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
b1 := db.NewFileInfoBatch(func(fs []protocol.FileInfo) error {
|
2024-08-24 10:45:10 +00:00
|
|
|
return c1.IndexUpdate(ctx, &protocol.IndexUpdate{Folder: "foo", Files: fs})
|
2024-02-10 18:16:27 +00:00
|
|
|
})
|
2024-02-11 08:03:12 +00:00
|
|
|
sentEntries := 0
|
2024-02-10 18:16:27 +00:00
|
|
|
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)}},
|
|
|
|
})
|
2024-02-11 08:03:12 +00:00
|
|
|
sentEntries++
|
2024-02-10 18:16:27 +00:00
|
|
|
}
|
2024-02-11 08:03:12 +00:00
|
|
|
wg.Add(1)
|
2024-02-10 18:16:27 +00:00
|
|
|
if err := b1.Flush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 08:03:12 +00:00
|
|
|
// 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()
|
|
|
|
|
2024-02-10 18:16:27 +00:00
|
|
|
c1.Close(io.EOF)
|
|
|
|
c2.Close(io.EOF)
|
|
|
|
<-c1.Closed()
|
|
|
|
<-c2.Closed()
|
|
|
|
|
2024-02-11 08:03:12 +00:00
|
|
|
if recvdEntries != sentEntries {
|
|
|
|
t.Error("didn't receive all expected messages", recvdEntries, sentEntries)
|
2024-02-10 18:16:27 +00:00
|
|
|
}
|
|
|
|
}
|