You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timer_test.go 807B

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