resourcemanager_test.go 839 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package syncx
  2. import (
  3. "errors"
  4. "io"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. type dummyResource struct {
  9. age int
  10. }
  11. func (dr *dummyResource) Close() error {
  12. return errors.New("close")
  13. }
  14. func TestResourceManager_GetResource(t *testing.T) {
  15. manager := NewResourceManager()
  16. defer manager.Close()
  17. var age int
  18. for i := 0; i < 10; i++ {
  19. val, err := manager.GetResource("key", func() (io.Closer, error) {
  20. age++
  21. return &dummyResource{
  22. age: age,
  23. }, nil
  24. })
  25. assert.Nil(t, err)
  26. assert.Equal(t, 1, val.(*dummyResource).age)
  27. }
  28. }
  29. func TestResourceManager_GetResourceError(t *testing.T) {
  30. manager := NewResourceManager()
  31. defer manager.Close()
  32. for i := 0; i < 10; i++ {
  33. _, err := manager.GetResource("key", func() (io.Closer, error) {
  34. return nil, errors.New("fail")
  35. })
  36. assert.NotNil(t, err)
  37. }
  38. }