Skip to content

Commit

Permalink
close once
Browse files Browse the repository at this point in the history
  • Loading branch information
imsodin committed Jan 31, 2019
1 parent d8e4756 commit 66d765c
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions lib/model/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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):
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 66d765c

Please sign in to comment.