code.gitea.io/gitea@v1.22.3/modules/util/timer_test.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package util 5 6 import ( 7 "sync/atomic" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestDebounce(t *testing.T) { 15 var c int64 16 d := Debounce(50 * time.Millisecond) 17 d(func() { atomic.AddInt64(&c, 1) }) 18 assert.EqualValues(t, 0, atomic.LoadInt64(&c)) 19 d(func() { atomic.AddInt64(&c, 1) }) 20 d(func() { atomic.AddInt64(&c, 1) }) 21 time.Sleep(100 * time.Millisecond) 22 assert.EqualValues(t, 1, atomic.LoadInt64(&c)) 23 d(func() { atomic.AddInt64(&c, 1) }) 24 assert.EqualValues(t, 1, atomic.LoadInt64(&c)) 25 d(func() { atomic.AddInt64(&c, 1) }) 26 d(func() { atomic.AddInt64(&c, 1) }) 27 d(func() { atomic.AddInt64(&c, 1) }) 28 time.Sleep(100 * time.Millisecond) 29 assert.EqualValues(t, 2, atomic.LoadInt64(&c)) 30 }