managedresource_test.go 439 B

12345678910111213141516171819202122
  1. package syncx
  2. import (
  3. "sync/atomic"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestManagedResource(t *testing.T) {
  8. var count int32
  9. resource := NewManagedResource(func() interface{} {
  10. return atomic.AddInt32(&count, 1)
  11. }, func(a, b interface{}) bool {
  12. return a == b
  13. })
  14. assert.Equal(t, resource.Take(), resource.Take())
  15. old := resource.Take()
  16. resource.MarkBroken(old)
  17. assert.NotEqual(t, old, resource.Take())
  18. }