Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions debounce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ import (
func TestDebounce(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
var (
counter1 uint64
counter2 uint64
counter1 atomic.Uint64
counter2 atomic.Uint64
)

f1 := func() {
atomic.AddUint64(&counter1, 1)
counter1.Add(1)
}

f2 := func() {
atomic.AddUint64(&counter2, 1)
counter2.Add(1)
}

f3 := func() {
atomic.AddUint64(&counter2, 2)
counter2.Add(2)
}

debounced := debounce.New(100 * time.Millisecond)
Expand All @@ -54,8 +54,8 @@ func TestDebounce(t *testing.T) {
time.Sleep(200 * time.Millisecond)
}

c1 := int(atomic.LoadUint64(&counter1))
c2 := int(atomic.LoadUint64(&counter2))
c1 := int(counter1.Load())
c2 := int(counter2.Load())
if c1 != 3 {
t.Error("Expected count 3, was", c1)
}
Expand All @@ -69,21 +69,21 @@ func TestDebounceConcurrentAdd(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
var wg sync.WaitGroup

var flag uint64
var flag atomic.Uint64

debounced := debounce.New(100 * time.Millisecond)

for range 10 {
wg.Go(func() {
debounced(func() {
atomic.CompareAndSwapUint64(&flag, 0, 1)
flag.CompareAndSwap(0, 1)
})
})
}
wg.Wait()

time.Sleep(500 * time.Millisecond)
c := int(atomic.LoadUint64(&flag))
c := int(flag.Load())
if c != 1 {
t.Error("Flag not set")
}
Expand All @@ -94,11 +94,11 @@ func TestDebounceConcurrentAdd(t *testing.T) {
func TestDebounceDelayed(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
var (
counter1 uint64
counter1 atomic.Uint64
)

f1 := func() {
atomic.AddUint64(&counter1, 1)
counter1.Add(1)
}

debounced := debounce.New(100 * time.Millisecond)
Expand All @@ -109,18 +109,18 @@ func TestDebounceDelayed(t *testing.T) {

time.Sleep(200 * time.Millisecond)

c1 := int(atomic.LoadUint64(&counter1))
c1 := int(counter1.Load())
if c1 != 1 {
t.Error("Expected count 1, was", c1)
}
})
}

func BenchmarkDebounce(b *testing.B) {
var counter uint64
var counter atomic.Uint64

f := func() {
atomic.AddUint64(&counter, 1)
counter.Add(1)
}

debounced := debounce.New(100 * time.Millisecond)
Expand All @@ -130,17 +130,17 @@ func BenchmarkDebounce(b *testing.B) {
debounced(f)
}

c := int(atomic.LoadUint64(&counter))
c := int(counter.Load())
if c != 0 {
b.Fatal("Expected count 0, was", c)
}
}

func ExampleNew() {
var counter uint64
var counter atomic.Uint64

f := func() {
atomic.AddUint64(&counter, 1)
counter.Add(1)
}

debounced := debounce.New(100 * time.Millisecond)
Expand All @@ -153,21 +153,21 @@ func ExampleNew() {
time.Sleep(200 * time.Millisecond)
}

c := int(atomic.LoadUint64(&counter))
c := int(counter.Load())

fmt.Println("Counter is", c)
// Output: Counter is 3
}

func TestDebounceCancel(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
var called int32
var called atomic.Int32

debounced, cancel := debounce.NewWithCancel(50 * time.Millisecond)

// Schedule a call that would normally be executed.
debounced(func() {
atomic.StoreInt32(&called, 1)
called.Store(1)
})

// Cancel it before the timer is triggered.
Expand All @@ -177,7 +177,7 @@ func TestDebounceCancel(t *testing.T) {
//the function will execute and the test will fail.
time.Sleep(70 * time.Millisecond)

if atomic.LoadInt32(&called) != 0 {
if called.Load() != 0 {
t.Fatal("expected debounced function NOT to be called after cancel")
}

Expand Down