Files
gocryptotrader/common/timedmutex/timed_mutex_test.go
Scott 63257ce4ca Improvement: Speeding up slow tests (#707)
* Speeds up tests

* Reduces time.Sleeps, lowers CreateTestBot complexity. Breaks things

* Removal of unecessary config reads. Parallel tests. Lower times

* Speeds up recent trades results

* mini update

* zoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooom

* Removes the dupes

* Lint

* post cherrypick

* Fix rare kraken data race

* Fixes banking global issues. Fixes postgres trades

* rmline for appveyor test

* Expands timeout in event that channel is closed before send

* Fix data race

* No rows, no bows and definitely no shows

* Removes parallel from createsnapshot tests

* Extends timedmutext test a smidge. Exchange fatality

* Shorter end timeframe and bigger candle
2021-07-07 12:42:03 +10:00

86 lines
2.1 KiB
Go

package timedmutex
import (
"testing"
"time"
)
func BenchmarkTimedMutexTime(b *testing.B) {
tm := NewTimedMutex(20 * time.Millisecond)
for i := 0; i < b.N; i++ {
tm.LockForDuration()
}
}
func TestConsistencyOfPanicFreeUnlock(t *testing.T) {
t.Parallel()
duration := 20 * time.Microsecond
tm := NewTimedMutex(duration)
for i := 1; i <= 50; i++ {
testUnlockTime := time.Duration(i) * time.Microsecond
tm.LockForDuration()
time.Sleep(testUnlockTime)
tm.UnlockIfLocked()
}
}
func TestUnlockAfterTimeout(t *testing.T) {
t.Parallel()
tm := NewTimedMutex(time.Nanosecond)
tm.LockForDuration()
time.Sleep(time.Millisecond * 200)
wasUnlocked := tm.UnlockIfLocked()
if wasUnlocked {
t.Error("Mutex should have been unlocked by timeout, not command")
}
}
func TestUnlockBeforeTimeout(t *testing.T) {
t.Parallel()
tm := NewTimedMutex(20 * time.Millisecond)
tm.LockForDuration()
wasUnlocked := tm.UnlockIfLocked()
if !wasUnlocked {
t.Error("Mutex should have been unlocked by command, not timeout")
}
}
// TestUnlockAtSameTimeAsTimeout this test ensures
// that even if the timeout and the command occur at
// the same time, no panics occur. The result of the
// 'who' unlocking this doesn't matter, so long as
// the unlock occurs without this test panicking
func TestUnlockAtSameTimeAsTimeout(t *testing.T) {
t.Parallel()
duration := time.Millisecond
tm := NewTimedMutex(duration)
tm.LockForDuration()
time.Sleep(duration)
tm.UnlockIfLocked()
}
func TestMultipleUnlocks(t *testing.T) {
t.Parallel()
tm := NewTimedMutex(10 * time.Second)
tm.LockForDuration()
wasUnlocked := tm.UnlockIfLocked()
if !wasUnlocked {
t.Error("Mutex should have been unlocked by command, not timeout")
}
wasUnlocked = tm.UnlockIfLocked()
if wasUnlocked {
t.Error("Mutex should have been already unlocked by command")
}
wasUnlocked = tm.UnlockIfLocked()
if wasUnlocked {
t.Error("Mutex should have been already unlocked by command")
}
}
func TestJustWaitItOut(t *testing.T) {
t.Parallel()
tm := NewTimedMutex(1 * time.Millisecond)
tm.LockForDuration()
time.Sleep(2 * time.Millisecond)
}