mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 17:52:49 +08:00
2cdff97934
* wip: backup * wip: backup * wip: backup * backup * backup * backup * add more tests * fix wrong dependency * fix lint errors * remove test due to data race * add tests * fix test error * add mon.Model * add mon.Model unmarshal * add monc * add more tests for monc * add more tests for monc * add docs for mon and monc packages * fix doc errors * chhore: add comment * chore: fix test bug * chore: refine tests * chore: remove primitive.NewObjectID in test code * chore: rename test files for typo
94 lines
1.9 KiB
Go
94 lines
1.9 KiB
Go
package syncx
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type dummyResource struct {
|
|
age int
|
|
}
|
|
|
|
func (dr *dummyResource) Close() error {
|
|
return errors.New("close")
|
|
}
|
|
|
|
func TestResourceManager_GetResource(t *testing.T) {
|
|
manager := NewResourceManager()
|
|
defer manager.Close()
|
|
|
|
var age int
|
|
for i := 0; i < 10; i++ {
|
|
val, err := manager.GetResource("key", func() (io.Closer, error) {
|
|
age++
|
|
return &dummyResource{
|
|
age: age,
|
|
}, nil
|
|
})
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 1, val.(*dummyResource).age)
|
|
}
|
|
}
|
|
|
|
func TestResourceManager_GetResourceError(t *testing.T) {
|
|
manager := NewResourceManager()
|
|
defer manager.Close()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
_, err := manager.GetResource("key", func() (io.Closer, error) {
|
|
return nil, errors.New("fail")
|
|
})
|
|
assert.NotNil(t, err)
|
|
}
|
|
}
|
|
|
|
func TestResourceManager_Close(t *testing.T) {
|
|
manager := NewResourceManager()
|
|
defer manager.Close()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
_, err := manager.GetResource("key", func() (io.Closer, error) {
|
|
return nil, errors.New("fail")
|
|
})
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
if assert.NoError(t, manager.Close()) {
|
|
assert.Equal(t, 0, len(manager.resources))
|
|
}
|
|
}
|
|
|
|
func TestResourceManager_UseAfterClose(t *testing.T) {
|
|
manager := NewResourceManager()
|
|
defer manager.Close()
|
|
|
|
_, err := manager.GetResource("key", func() (io.Closer, error) {
|
|
return nil, errors.New("fail")
|
|
})
|
|
assert.NotNil(t, err)
|
|
if assert.NoError(t, manager.Close()) {
|
|
_, err = manager.GetResource("key", func() (io.Closer, error) {
|
|
return nil, errors.New("fail")
|
|
})
|
|
assert.NotNil(t, err)
|
|
}
|
|
}
|
|
|
|
func TestResourceManager_Inject(t *testing.T) {
|
|
manager := NewResourceManager()
|
|
defer manager.Close()
|
|
|
|
manager.Inject("key", &dummyResource{
|
|
age: 10,
|
|
})
|
|
|
|
val, err := manager.GetResource("key", func() (io.Closer, error) {
|
|
return nil, nil
|
|
})
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 10, val.(*dummyResource).age)
|
|
}
|