lib/model: Fix removal of paused folders, improve tests (fixes #4405)
This commit is contained in:
committed by
Jakob Borg
parent
74c8d34805
commit
9d3f3847ed
@@ -335,25 +335,15 @@ func (m *Model) addFolderLocked(cfg config.FolderConfiguration) {
|
||||
m.folderIgnores[cfg.ID] = ignores
|
||||
}
|
||||
|
||||
func (m *Model) RemoveFolder(folder string) {
|
||||
func (m *Model) RemoveFolder(cfg config.FolderConfiguration) {
|
||||
m.fmut.Lock()
|
||||
m.pmut.Lock()
|
||||
|
||||
// Delete syncthing specific files
|
||||
folderCfg, ok := m.folderCfgs[folder]
|
||||
if !ok {
|
||||
// Folder might be paused
|
||||
folderCfg, ok = m.cfg.Folder(folder)
|
||||
}
|
||||
if !ok {
|
||||
panic("bug: remove non existing folder")
|
||||
}
|
||||
fs := folderCfg.Filesystem()
|
||||
fs.RemoveAll(".stfolder")
|
||||
cfg.Filesystem().RemoveAll(".stfolder")
|
||||
|
||||
m.tearDownFolderLocked(folder)
|
||||
m.tearDownFolderLocked(cfg.ID)
|
||||
// Remove it from the database
|
||||
db.DropFolder(m.db, folder)
|
||||
db.DropFolder(m.db, cfg.ID)
|
||||
|
||||
m.pmut.Unlock()
|
||||
m.fmut.Unlock()
|
||||
@@ -2496,7 +2486,7 @@ func (m *Model) CommitConfiguration(from, to config.Configuration) bool {
|
||||
toCfg, ok := toFolders[folderID]
|
||||
if !ok {
|
||||
// The folder was removed.
|
||||
m.RemoveFolder(folderID)
|
||||
m.RemoveFolder(fromCfg)
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -1907,48 +1907,69 @@ func TestIssue3164(t *testing.T) {
|
||||
|
||||
func TestIssue4357(t *testing.T) {
|
||||
db := db.OpenMemory()
|
||||
m := NewModel(defaultConfig, protocol.LocalDeviceID, "syncthing", "dev", db, nil)
|
||||
cfg := defaultConfig.RawCopy()
|
||||
// Create a separate wrapper not to polute other tests.
|
||||
wrapper := config.Wrap("/tmp/test", config.Configuration{})
|
||||
m := NewModel(wrapper, protocol.LocalDeviceID, "syncthing", "dev", db, nil)
|
||||
m.ServeBackground()
|
||||
defer m.Stop()
|
||||
cfg := m.cfg.RawCopy()
|
||||
m.CommitConfiguration(config.Configuration{}, cfg)
|
||||
|
||||
// Replace kicks off CommitConfiguration in multiple routines which we
|
||||
// have no idea when they get called, so sleep a bit :(
|
||||
// We could make replace blocking for all routines to come back without
|
||||
// holding the lock, but that's a bit much just to make a test.
|
||||
replaceCfg := func(cfg config.Configuration) {
|
||||
t.Helper()
|
||||
if err := wrapper.ReplaceBlocking(cfg); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Force the model to wire itself and add the folders
|
||||
replaceCfg(cfg)
|
||||
|
||||
if _, ok := m.folderCfgs["default"]; !ok {
|
||||
t.Error("Folder should be running")
|
||||
}
|
||||
|
||||
newCfg := m.cfg.RawCopy()
|
||||
newCfg := wrapper.RawCopy()
|
||||
newCfg.Folders[0].Paused = true
|
||||
m.CommitConfiguration(cfg, newCfg)
|
||||
|
||||
replaceCfg(newCfg)
|
||||
|
||||
if _, ok := m.folderCfgs["default"]; ok {
|
||||
t.Error("Folder should not be running")
|
||||
}
|
||||
|
||||
// Should not panic when removing a paused folder.
|
||||
m.RemoveFolder("default")
|
||||
if _, ok := m.cfg.Folder("default"); !ok {
|
||||
t.Error("should still have folder in config")
|
||||
}
|
||||
|
||||
replaceCfg(config.Configuration{})
|
||||
|
||||
if _, ok := m.cfg.Folder("default"); ok {
|
||||
t.Error("should not have folder in config")
|
||||
}
|
||||
|
||||
// Add the folder back, should be running
|
||||
m.CommitConfiguration(config.Configuration{}, cfg)
|
||||
replaceCfg(cfg)
|
||||
|
||||
if _, ok := m.folderCfgs["default"]; !ok {
|
||||
t.Error("Folder should be running")
|
||||
}
|
||||
if _, ok := m.cfg.Folder("default"); !ok {
|
||||
t.Error("should still have folder in config")
|
||||
}
|
||||
|
||||
// Should not panic when removing a running folder.
|
||||
m.RemoveFolder("default")
|
||||
replaceCfg(config.Configuration{})
|
||||
|
||||
if _, ok := m.folderCfgs["default"]; ok {
|
||||
t.Error("Folder should not be running")
|
||||
}
|
||||
|
||||
// Should panic when removing a non-existing folder
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Error("expected a panic")
|
||||
}
|
||||
}()
|
||||
m.RemoveFolder("non-existing")
|
||||
|
||||
if _, ok := m.cfg.Folder("default"); ok {
|
||||
t.Error("should not have folder in config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanNoDatabaseWrite(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user