-
Notifications
You must be signed in to change notification settings - Fork 151
/
factory_test.go
75 lines (65 loc) · 1.96 KB
/
factory_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package pool
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type TestFactoryObject struct {
status string
}
func TestDefaultPooledObjectFactorySimple(t *testing.T) {
t.Parallel()
ctx := context.Background()
factory := NewPooledObjectFactorySimple(
func(context.Context) (interface{}, error) {
return &TestFactoryObject{status: "make"}, nil
})
assert.NotNil(t, factory)
o, _ := factory.MakeObject(ctx)
if debugTest {
fmt.Println("object:", o.Object)
}
assert.NotNil(t, o)
assert.Nil(t, factory.ActivateObject(ctx, o))
assert.Nil(t, factory.PassivateObject(ctx, o))
assert.True(t, factory.ValidateObject(ctx, o))
assert.Nil(t, factory.DestroyObject(ctx, o))
}
func TestDefaultPooledObjectFactory(t *testing.T) {
t.Parallel()
factory := NewPooledObjectFactory(
func(context.Context) (interface{}, error) {
return &TestFactoryObject{status: "make"}, nil
},
func(ctx context.Context, object *PooledObject) error {
object.Object.(*TestFactoryObject).status = "destroy"
return nil
},
func(ctx context.Context, object *PooledObject) bool {
object.Object.(*TestFactoryObject).status = "validate"
return true
},
func(ctx context.Context, object *PooledObject) error {
object.Object.(*TestFactoryObject).status = "activate"
return nil
},
func(ctx context.Context, object *PooledObject) error {
object.Object.(*TestFactoryObject).status = "passivate"
return nil
})
assert.NotNil(t, factory)
ctx := context.Background()
o, _ := factory.MakeObject(ctx)
assert.NotNil(t, o)
obj := o.Object.(*TestFactoryObject)
assert.Equal(t, "make", obj.status)
assert.Nil(t, factory.ActivateObject(ctx, o))
assert.Equal(t, "activate", obj.status)
assert.Nil(t, factory.PassivateObject(ctx, o))
assert.Equal(t, "passivate", obj.status)
assert.True(t, factory.ValidateObject(ctx, o))
assert.Equal(t, "validate", obj.status)
assert.Nil(t, factory.DestroyObject(ctx, o))
assert.Equal(t, "destroy", obj.status)
}