123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package syncx
- import (
- "errors"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- )
- func TestImmutableResource(t *testing.T) {
- var count int
- r := NewImmutableResource(func() (interface{}, error) {
- count++
- return "hello", nil
- })
- res, err := r.Get()
- assert.Equal(t, "hello", res)
- assert.Equal(t, 1, count)
- assert.Nil(t, err)
- // again
- res, err = r.Get()
- assert.Equal(t, "hello", res)
- assert.Equal(t, 1, count)
- assert.Nil(t, err)
- }
- func TestImmutableResourceError(t *testing.T) {
- var count int
- r := NewImmutableResource(func() (interface{}, error) {
- count++
- return nil, errors.New("any")
- })
- res, err := r.Get()
- assert.Nil(t, res)
- assert.NotNil(t, err)
- assert.Equal(t, "any", err.Error())
- assert.Equal(t, 1, count)
- // again
- res, err = r.Get()
- assert.Nil(t, res)
- assert.NotNil(t, err)
- assert.Equal(t, "any", err.Error())
- assert.Equal(t, 1, count)
- r.refreshInterval = 0
- time.Sleep(time.Millisecond)
- res, err = r.Get()
- assert.Nil(t, res)
- assert.NotNil(t, err)
- assert.Equal(t, "any", err.Error())
- assert.Equal(t, 2, count)
- }
- func TestImmutableResourceErrorRefreshAlways(t *testing.T) {
- var count int
- r := NewImmutableResource(func() (interface{}, error) {
- count++
- return nil, errors.New("any")
- }, WithRefreshIntervalOnFailure(0))
- res, err := r.Get()
- assert.Nil(t, res)
- assert.NotNil(t, err)
- assert.Equal(t, "any", err.Error())
- assert.Equal(t, 1, count)
- // again
- res, err = r.Get()
- assert.Nil(t, res)
- assert.NotNil(t, err)
- assert.Equal(t, "any", err.Error())
- assert.Equal(t, 2, count)
- }
|