2015-04-23 17:09:56 +00:00
|
|
|
// Copyright (C) 2015 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 http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/logger"
|
2015-04-23 17:09:56 +00:00
|
|
|
)
|
|
|
|
|
2015-04-24 02:25:24 +00:00
|
|
|
const (
|
|
|
|
logThreshold = 100 * time.Millisecond
|
|
|
|
shortWait = 5 * time.Millisecond
|
|
|
|
longWait = 125 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
2016-03-27 10:41:38 +00:00
|
|
|
var skipTimingTests = false
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Check a few times that a short sleep does not in fact overrun the log
|
|
|
|
// threshold. If it does, the timer accuracy is crap or the host is
|
|
|
|
// overloaded and we can't reliably run the tests in here. In the normal
|
|
|
|
// case this takes just 25*5 = 125 ms.
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
t0 := time.Now()
|
|
|
|
time.Sleep(shortWait)
|
|
|
|
if time.Since(t0) > logThreshold {
|
|
|
|
skipTimingTests = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 17:09:56 +00:00
|
|
|
func TestTypes(t *testing.T) {
|
2015-04-24 00:43:40 +00:00
|
|
|
debug = false
|
2015-10-03 15:25:21 +00:00
|
|
|
l.SetDebug("sync", false)
|
2015-04-24 00:43:40 +00:00
|
|
|
|
2015-04-23 17:09:56 +00:00
|
|
|
if _, ok := NewMutex().(*sync.Mutex); !ok {
|
|
|
|
t.Error("Wrong type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := NewRWMutex().(*sync.RWMutex); !ok {
|
|
|
|
t.Error("Wrong type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := NewWaitGroup().(*sync.WaitGroup); !ok {
|
|
|
|
t.Error("Wrong type")
|
|
|
|
}
|
|
|
|
|
|
|
|
debug = true
|
2015-10-03 15:25:21 +00:00
|
|
|
l.SetDebug("sync", true)
|
2015-04-23 17:09:56 +00:00
|
|
|
|
|
|
|
if _, ok := NewMutex().(*loggedMutex); !ok {
|
|
|
|
t.Error("Wrong type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := NewRWMutex().(*loggedRWMutex); !ok {
|
|
|
|
t.Error("Wrong type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := NewWaitGroup().(*loggedWaitGroup); !ok {
|
|
|
|
t.Error("Wrong type")
|
|
|
|
}
|
|
|
|
|
|
|
|
debug = false
|
2015-10-03 15:25:21 +00:00
|
|
|
l.SetDebug("sync", false)
|
2015-04-23 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMutex(t *testing.T) {
|
2016-03-27 10:41:38 +00:00
|
|
|
if skipTimingTests {
|
|
|
|
t.Skip("insufficient timer accuracy")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-23 17:09:56 +00:00
|
|
|
debug = true
|
2015-10-03 15:25:21 +00:00
|
|
|
l.SetDebug("sync", true)
|
2015-04-24 02:25:24 +00:00
|
|
|
threshold = logThreshold
|
2015-04-23 17:09:56 +00:00
|
|
|
|
|
|
|
msgmut := sync.Mutex{}
|
2015-04-28 20:32:10 +00:00
|
|
|
var messages []string
|
2015-04-23 17:09:56 +00:00
|
|
|
|
|
|
|
l.AddHandler(logger.LevelDebug, func(_ logger.LogLevel, message string) {
|
|
|
|
msgmut.Lock()
|
|
|
|
messages = append(messages, message)
|
|
|
|
msgmut.Unlock()
|
|
|
|
})
|
|
|
|
|
|
|
|
mut := NewMutex()
|
|
|
|
mut.Lock()
|
2015-04-24 02:25:24 +00:00
|
|
|
time.Sleep(shortWait)
|
2015-04-23 17:09:56 +00:00
|
|
|
mut.Unlock()
|
|
|
|
|
|
|
|
if len(messages) > 0 {
|
|
|
|
t.Errorf("Unexpected message count")
|
|
|
|
}
|
|
|
|
|
|
|
|
mut.Lock()
|
2015-04-24 02:25:24 +00:00
|
|
|
time.Sleep(longWait)
|
2015-04-23 17:09:56 +00:00
|
|
|
mut.Unlock()
|
|
|
|
|
|
|
|
if len(messages) != 1 {
|
|
|
|
t.Errorf("Unexpected message count")
|
|
|
|
}
|
|
|
|
|
|
|
|
debug = false
|
2015-10-03 15:25:21 +00:00
|
|
|
l.SetDebug("sync", false)
|
2015-04-23 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRWMutex(t *testing.T) {
|
2016-03-27 10:41:38 +00:00
|
|
|
if skipTimingTests {
|
|
|
|
t.Skip("insufficient timer accuracy")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-23 17:09:56 +00:00
|
|
|
debug = true
|
2015-10-03 15:25:21 +00:00
|
|
|
l.SetDebug("sync", true)
|
2015-04-24 02:25:24 +00:00
|
|
|
threshold = logThreshold
|
2015-04-23 17:09:56 +00:00
|
|
|
|
|
|
|
msgmut := sync.Mutex{}
|
2015-04-28 20:32:10 +00:00
|
|
|
var messages []string
|
2015-04-23 17:09:56 +00:00
|
|
|
|
|
|
|
l.AddHandler(logger.LevelDebug, func(_ logger.LogLevel, message string) {
|
|
|
|
msgmut.Lock()
|
|
|
|
messages = append(messages, message)
|
|
|
|
msgmut.Unlock()
|
|
|
|
})
|
|
|
|
|
|
|
|
mut := NewRWMutex()
|
|
|
|
mut.Lock()
|
2015-04-24 02:25:24 +00:00
|
|
|
time.Sleep(shortWait)
|
2015-04-23 17:09:56 +00:00
|
|
|
mut.Unlock()
|
|
|
|
|
|
|
|
if len(messages) > 0 {
|
|
|
|
t.Errorf("Unexpected message count")
|
|
|
|
}
|
|
|
|
|
|
|
|
mut.Lock()
|
2015-04-24 02:25:24 +00:00
|
|
|
time.Sleep(longWait)
|
2015-04-23 17:09:56 +00:00
|
|
|
mut.Unlock()
|
|
|
|
|
|
|
|
if len(messages) != 1 {
|
|
|
|
t.Errorf("Unexpected message count")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Testing rlocker logging
|
|
|
|
mut.RLock()
|
|
|
|
go func() {
|
2015-04-24 02:25:24 +00:00
|
|
|
time.Sleep(longWait)
|
2015-04-23 17:09:56 +00:00
|
|
|
mut.RUnlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
mut.Lock()
|
|
|
|
mut.Unlock()
|
|
|
|
|
|
|
|
if len(messages) != 2 {
|
|
|
|
t.Errorf("Unexpected message count")
|
|
|
|
}
|
2016-11-03 21:33:33 +00:00
|
|
|
if !strings.Contains(messages[1], "RUnlockers while locking:\nat sync") || !strings.Contains(messages[1], "sync_test.go:") {
|
2015-04-23 17:09:56 +00:00
|
|
|
t.Error("Unexpected message")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Testing multiple rlockers
|
|
|
|
mut.RLock()
|
|
|
|
mut.RLock()
|
|
|
|
mut.RLock()
|
|
|
|
mut.RUnlock()
|
|
|
|
mut.RUnlock()
|
|
|
|
mut.RUnlock()
|
|
|
|
|
|
|
|
debug = false
|
2015-10-03 15:25:21 +00:00
|
|
|
l.SetDebug("sync", false)
|
2015-04-23 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitGroup(t *testing.T) {
|
2016-03-27 10:41:38 +00:00
|
|
|
if skipTimingTests {
|
|
|
|
t.Skip("insufficient timer accuracy")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-23 17:09:56 +00:00
|
|
|
debug = true
|
2015-10-03 15:25:21 +00:00
|
|
|
l.SetDebug("sync", true)
|
2015-04-24 02:25:24 +00:00
|
|
|
threshold = logThreshold
|
2015-04-23 17:09:56 +00:00
|
|
|
|
|
|
|
msgmut := sync.Mutex{}
|
2015-04-28 20:32:10 +00:00
|
|
|
var messages []string
|
2015-04-23 17:09:56 +00:00
|
|
|
|
|
|
|
l.AddHandler(logger.LevelDebug, func(_ logger.LogLevel, message string) {
|
|
|
|
msgmut.Lock()
|
|
|
|
messages = append(messages, message)
|
|
|
|
msgmut.Unlock()
|
|
|
|
})
|
|
|
|
|
|
|
|
wg := NewWaitGroup()
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2015-04-24 02:25:24 +00:00
|
|
|
time.Sleep(shortWait)
|
2015-04-23 17:09:56 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if len(messages) > 0 {
|
|
|
|
t.Errorf("Unexpected message count")
|
|
|
|
}
|
|
|
|
|
|
|
|
wg = NewWaitGroup()
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2015-04-24 02:25:24 +00:00
|
|
|
time.Sleep(longWait)
|
2015-04-23 17:09:56 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if len(messages) != 1 {
|
|
|
|
t.Errorf("Unexpected message count")
|
|
|
|
}
|
|
|
|
|
|
|
|
debug = false
|
2015-10-03 15:25:21 +00:00
|
|
|
l.SetDebug("sync", false)
|
2015-04-23 17:09:56 +00:00
|
|
|
}
|