-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
service_lazy_test.go
137 lines (114 loc) · 2.88 KB
/
service_lazy_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package do
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type lazyTest struct {
idx int
err error
}
func (t *lazyTest) Shutdown() error {
return t.err
}
func TestServiceLazyName(t *testing.T) {
is := assert.New(t)
type test struct {
foobar string
}
_test := test{foobar: "foobar"}
provider1 := func(i *Injector) (int, error) {
return 42, nil
}
provider2 := func(i *Injector) (test, error) {
return _test, nil
}
service1 := newServiceLazy("foobar", provider1)
is.Equal("foobar", service1.getName())
service2 := newServiceLazy("foobar", provider2)
is.Equal("foobar", service2.getName())
}
func TestServiceLazyInstance(t *testing.T) {
is := assert.New(t)
type test struct {
foobar string
}
_test := test{foobar: "foobar"}
provider1 := func(i *Injector) (int, error) {
return 42, nil
}
provider2 := func(i *Injector) (test, error) {
return _test, nil
}
provider3 := func(i *Injector) (int, error) {
panic("error")
}
provider4 := func(i *Injector) (int, error) {
panic(fmt.Errorf("error"))
}
provider5 := func(i *Injector) (int, error) {
return 42, fmt.Errorf("error")
}
i := New()
service1 := newServiceLazy("foobar", provider1)
instance1, err1 := service1.getInstance(i)
is.Nil(err1)
is.Equal(42, instance1)
service2 := newServiceLazy("hello", provider2)
instance2, err2 := service2.getInstance(i)
is.Nil(err2)
is.Equal(_test, instance2)
is.Panics(func() {
service3 := newServiceLazy("baz", provider3)
_, _ = service3.getInstance(i)
})
is.NotPanics(func() {
service4 := newServiceLazy("plop", provider4)
instance4, err4 := service4.getInstance(i)
is.NotNil(err4)
is.Empty(instance4)
expected := fmt.Errorf("error")
is.Equal(expected, err4)
})
is.NotPanics(func() {
service5 := newServiceLazy("plop", provider5)
instance5, err5 := service5.getInstance(i)
is.NotNil(err5)
is.Empty(instance5)
expected := fmt.Errorf("error")
is.Equal(expected, err5)
})
}
func TestServiceLazyInstanceShutDown(t *testing.T) {
is := assert.New(t)
index := 1
provider1 := func(i *Injector) (*lazyTest, error) {
index++
return &lazyTest{index, nil}, nil
}
provider2 := func(i *Injector) (*lazyTest, error) {
index++
return &lazyTest{index, assert.AnError}, nil
}
i := New()
service1 := newServiceLazy("foobar", provider1)
instance1, err := service1.getInstance(i)
is.Nil(err)
is.True(service1.(*ServiceLazy[*lazyTest]).built)
err = service1.shutdown()
is.False(service1.(*ServiceLazy[*lazyTest]).built)
is.Nil(err)
instance2, err := service1.getInstance(i)
is.Nil(err)
is.NotEqual(instance1.idx, instance2.idx)
is.Equal(instance1.idx+1, instance2.idx)
service2 := newServiceLazy("foobar", provider2).(*ServiceLazy[*lazyTest])
is.False(service2.built)
is.Nil(err)
err = service2.build(i)
is.Nil(err)
is.True(service2.built)
err = service2.shutdown()
is.Error(assert.AnError, err)
is.True(service2.built)
}