times_test.go 958 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package utils
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. const sleepInterval = time.Millisecond * 10
  8. func TestElapsedTimer_Duration(t *testing.T) {
  9. timer := NewElapsedTimer()
  10. time.Sleep(sleepInterval)
  11. assert.True(t, timer.Duration() >= sleepInterval)
  12. }
  13. func TestElapsedTimer_Elapsed(t *testing.T) {
  14. timer := NewElapsedTimer()
  15. time.Sleep(sleepInterval)
  16. duration, err := time.ParseDuration(timer.Elapsed())
  17. assert.Nil(t, err)
  18. assert.True(t, duration >= sleepInterval)
  19. }
  20. func TestElapsedTimer_ElapsedMs(t *testing.T) {
  21. timer := NewElapsedTimer()
  22. time.Sleep(sleepInterval)
  23. duration, err := time.ParseDuration(timer.ElapsedMs())
  24. assert.Nil(t, err)
  25. assert.True(t, duration >= sleepInterval)
  26. }
  27. func TestCurrent(t *testing.T) {
  28. currentMillis := CurrentMillis()
  29. currentMicros := CurrentMicros()
  30. assert.True(t, currentMillis > 0)
  31. assert.True(t, currentMicros > 0)
  32. assert.True(t, currentMillis*1000 <= currentMicros)
  33. }