Permalink
Browse files

close once

  • Loading branch information...
imsodin committed Jan 31, 2019
1 parent d8e4756 commit 66d765c8e858766993380afadadc4554fa51a1ea
Showing with 43 additions and 28 deletions.
  1. +43 −28 lib/model/requests_test.go
@@ -14,6 +14,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"

@@ -41,14 +42,17 @@ func TestRequestSimple(t *testing.T) {
// We listen for incoming index updates and trigger when we see one for
// the expected test file.
done := make(chan struct{})
var once sync.Once
fc.mut.Lock()
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
for _, f := range fs {
if f.Name == "testfile" {
close(done)
return
once.Do(func() {
for _, f := range fs {
if f.Name == "testfile" {
close(done)
return
}
}
}
})
}
fc.mut.Unlock()

@@ -84,14 +88,17 @@ func TestSymlinkTraversalRead(t *testing.T) {
// We listen for incoming index updates and trigger when we see one for
// the expected test file.
done := make(chan struct{})
var once sync.Once
fc.mut.Lock()
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
for _, f := range fs {
if f.Name == "symlink" {
close(done)
return
once.Do(func() {
for _, f := range fs {
if f.Name == "symlink" {
close(done)
return
}
}
}
})
}
fc.mut.Unlock()

@@ -648,20 +655,23 @@ func TestRequestSymlinkWindows(t *testing.T) {
}()

first := make(chan struct{})
var once sync.Once
fc.mut.Lock()
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
// expected first index
if len(fs) != 1 {
t.Fatalf("Expected just one file in index, got %v", fs)
}
f := fs[0]
if f.Name != "link" {
t.Fatalf(`Got file info with path "%v", expected "link"`, f.Name)
}
if !f.IsInvalid() {
t.Errorf(`File info was not marked as invalid`)
}
close(first)
once.Do(func() {
// expected first index
if len(fs) != 1 {
t.Fatalf("Expected just one file in index, got %v", fs)
}
f := fs[0]
if f.Name != "link" {
t.Fatalf(`Got file info with path "%v", expected "link"`, f.Name)
}
if !f.IsInvalid() {
t.Errorf(`File info was not marked as invalid`)
}
close(first)
})
}
fc.mut.Unlock()

@@ -791,11 +801,14 @@ func TestRequestRemoteRenameChanged(t *testing.T) {

done := make(chan struct{})
fc.mut.Lock()
var once sync.Once
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
if len(fs) != 2 {
t.Fatalf("Received index with %v indexes instead of 2", len(fs))
}
close(done)
once.Do(func() {
if len(fs) != 2 {
t.Fatalf("Received index with %v indexes instead of 2", len(fs))
}
close(done)
})
}
fc.mut.Unlock()

@@ -813,9 +826,10 @@ func TestRequestRemoteRenameChanged(t *testing.T) {
select {
case <-done:
done = make(chan struct{})
var once sync.Once
fc.mut.Lock()
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
close(done)
once.Do(func() { close(done) })
}
fc.mut.Unlock()
case <-time.After(10 * time.Second):
@@ -973,8 +987,9 @@ func TestRequestDeleteChanged(t *testing.T) {

done := make(chan struct{})
fc.mut.Lock()
var once sync.Once
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
close(done)
once.Do(func() { close(done) })
}
fc.mut.Unlock()

0 comments on commit 66d765c

Please sign in to comment.