refresource_test.go 454 B

123456789101112131415161718192021222324252627
  1. package syncx
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestRefCleaner(t *testing.T) {
  7. var count int
  8. clean := func() {
  9. count += 1
  10. }
  11. cleaner := NewRefResource(clean)
  12. err := cleaner.Use()
  13. assert.Nil(t, err)
  14. err = cleaner.Use()
  15. assert.Nil(t, err)
  16. cleaner.Clean()
  17. cleaner.Clean()
  18. assert.Equal(t, 1, count)
  19. cleaner.Clean()
  20. cleaner.Clean()
  21. assert.Equal(t, 1, count)
  22. assert.Equal(t, ErrUseOfCleaned, cleaner.Use())
  23. }