lib: Apply config changes sequentially (ref #5298) (#7188)

This commit is contained in:
Simon Frei
2021-01-15 15:43:34 +01:00
committed by GitHub
parent b2d82da20d
commit f63cdbfcfa
20 changed files with 1032 additions and 790 deletions
+41 -19
View File
@@ -8,6 +8,7 @@ package connections
import (
"bytes"
"context"
crand "crypto/rand"
"io"
"math/rand"
@@ -16,6 +17,7 @@ import (
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/thejerf/suture/v4"
"golang.org/x/time/rate"
)
@@ -29,24 +31,34 @@ func init() {
device4, _ = protocol.DeviceIDFromString("P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2")
}
func initConfig() config.Wrapper {
cfg := config.Wrap("/dev/null", config.New(device1), device1, events.NoopLogger)
func initConfig() (config.Wrapper, context.CancelFunc) {
wrapper := config.Wrap("/dev/null", config.New(device1), device1, events.NoopLogger)
dev1Conf = config.NewDeviceConfiguration(device1, "device1")
dev2Conf = config.NewDeviceConfiguration(device2, "device2")
dev3Conf = config.NewDeviceConfiguration(device3, "device3")
dev4Conf = config.NewDeviceConfiguration(device4, "device4")
var cancel context.CancelFunc = func() {}
if wrapperService, ok := wrapper.(suture.Service); ok {
var ctx context.Context
ctx, cancel = context.WithCancel(context.Background())
go wrapperService.Serve(ctx)
}
dev2Conf.MaxRecvKbps = rand.Int() % 100000
dev2Conf.MaxSendKbps = rand.Int() % 100000
waiter, _ := cfg.SetDevices([]config.DeviceConfiguration{dev1Conf, dev2Conf, dev3Conf, dev4Conf})
waiter, _ := wrapper.Modify(func(cfg *config.Configuration) {
cfg.SetDevices([]config.DeviceConfiguration{dev1Conf, dev2Conf, dev3Conf, dev4Conf})
})
waiter.Wait()
return cfg
return wrapper, cancel
}
func TestLimiterInit(t *testing.T) {
cfg := initConfig()
lim := newLimiter(device1, cfg)
wrapper, wrapperCancel := initConfig()
defer wrapperCancel()
lim := newLimiter(device1, wrapper)
device2ReadLimit := dev2Conf.MaxRecvKbps
device2WriteLimit := dev2Conf.MaxSendKbps
@@ -70,8 +82,9 @@ func TestLimiterInit(t *testing.T) {
}
func TestSetDeviceLimits(t *testing.T) {
cfg := initConfig()
lim := newLimiter(device1, cfg)
wrapper, wrapperCancel := initConfig()
defer wrapperCancel()
lim := newLimiter(device1, wrapper)
// should still be inf/inf because this is local device
dev1ReadLimit := rand.Int() % 100000
@@ -87,7 +100,9 @@ func TestSetDeviceLimits(t *testing.T) {
dev3ReadLimit := rand.Int() % 10000
dev3Conf.MaxRecvKbps = dev3ReadLimit
waiter, _ := cfg.SetDevices([]config.DeviceConfiguration{dev1Conf, dev2Conf, dev3Conf, dev4Conf})
waiter, _ := wrapper.Modify(func(cfg *config.Configuration) {
cfg.SetDevices([]config.DeviceConfiguration{dev1Conf, dev2Conf, dev3Conf, dev4Conf})
})
waiter.Wait()
expectedR := map[protocol.DeviceID]*rate.Limiter{
@@ -108,10 +123,11 @@ func TestSetDeviceLimits(t *testing.T) {
}
func TestRemoveDevice(t *testing.T) {
cfg := initConfig()
lim := newLimiter(device1, cfg)
wrapper, wrapperCancel := initConfig()
defer wrapperCancel()
lim := newLimiter(device1, wrapper)
waiter, _ := cfg.RemoveDevice(device3)
waiter, _ := wrapper.RemoveDevice(device3)
waiter.Wait()
expectedR := map[protocol.DeviceID]*rate.Limiter{
device2: rate.NewLimiter(rate.Limit(dev2Conf.MaxRecvKbps*1024), limiterBurstSize),
@@ -128,15 +144,18 @@ func TestRemoveDevice(t *testing.T) {
}
func TestAddDevice(t *testing.T) {
cfg := initConfig()
lim := newLimiter(device1, cfg)
wrapper, wrapperCancel := initConfig()
defer wrapperCancel()
lim := newLimiter(device1, wrapper)
addedDevice, _ := protocol.DeviceIDFromString("XZJ4UNS-ENI7QGJ-J45DT6G-QSGML2K-6I4XVOG-NAZ7BF5-2VAOWNT-TFDOMQU")
addDevConf := config.NewDeviceConfiguration(addedDevice, "addedDevice")
addDevConf.MaxRecvKbps = 120
addDevConf.MaxSendKbps = 240
waiter, _ := cfg.SetDevice(addDevConf)
waiter, _ := wrapper.Modify(func(cfg *config.Configuration) {
cfg.SetDevice(addDevConf)
})
waiter.Wait()
expectedR := map[protocol.DeviceID]*rate.Limiter{
@@ -159,17 +178,20 @@ func TestAddDevice(t *testing.T) {
}
func TestAddAndRemove(t *testing.T) {
cfg := initConfig()
lim := newLimiter(device1, cfg)
wrapper, wrapperCancel := initConfig()
defer wrapperCancel()
lim := newLimiter(device1, wrapper)
addedDevice, _ := protocol.DeviceIDFromString("XZJ4UNS-ENI7QGJ-J45DT6G-QSGML2K-6I4XVOG-NAZ7BF5-2VAOWNT-TFDOMQU")
addDevConf := config.NewDeviceConfiguration(addedDevice, "addedDevice")
addDevConf.MaxRecvKbps = 120
addDevConf.MaxSendKbps = 240
waiter, _ := cfg.SetDevice(addDevConf)
waiter, _ := wrapper.Modify(func(cfg *config.Configuration) {
cfg.SetDevice(addDevConf)
})
waiter.Wait()
waiter, _ = cfg.RemoveDevice(device3)
waiter, _ = wrapper.RemoveDevice(device3)
waiter.Wait()
expectedR := map[protocol.DeviceID]*rate.Limiter{