-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathservice_test.go
More file actions
168 lines (143 loc) · 5.63 KB
/
Copy pathservice_test.go
File metadata and controls
168 lines (143 loc) · 5.63 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package do
import (
"reflect"
"testing"
"time"
"github.com/samber/do/v2/stacktrace"
"github.com/stretchr/testify/assert"
)
func TestInferServiceName(t *testing.T) {
t.Parallel()
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)
// more tests in the package
is.Equal("int", inferServiceName[int]())
is.Equal("github.com/samber/do/v2.eagerTest", inferServiceName[eagerTest]())
is.Equal("*github.com/samber/do/v2.eagerTest", inferServiceName[*eagerTest]())
is.Equal("github.com/samber/do/v2.Healthchecker", inferServiceName[Healthchecker]())
}
func TestInferServiceProviderStacktrace(t *testing.T) {
t.Parallel()
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)
// Test with lazy service
lazyService := newServiceLazy("lazy-service", func(i Injector) (int, error) {
return 42, nil
})
frame, ok := inferServiceProviderStacktrace(lazyService)
is.True(ok)
is.NotNil(frame)
is.Contains(frame.File, "service_test.go")
is.Contains(frame.Function, "TestInferServiceProviderStacktrace")
// Test with eager service
eagerService := newServiceEager("eager-service", func(i Injector) (int, error) {
return 42, nil
})
frame2, ok2 := inferServiceProviderStacktrace(eagerService)
is.True(ok2)
is.NotNil(frame2)
is.Contains(frame2.File, "service_eager.go")
is.Contains(frame2.Function, "newServiceEager")
// Test with transient service (should return false)
transientService := newServiceTransient("transient-service", func(i Injector) (int, error) {
return 42, nil
})
frame3, ok3 := inferServiceProviderStacktrace(transientService)
is.False(ok3)
is.Equal(stacktrace.Frame{}, frame3)
// Test with alias service
i := New()
Provide(i, func(i Injector) (int, error) {
return 42, nil
})
aliasService := newServiceAlias[int, int]("alias-service", i, "int")
frame4, ok4 := inferServiceProviderStacktrace(aliasService)
is.True(ok4)
is.NotNil(frame4)
is.Contains(frame4.File, "service_alias.go") // Provider frame points to the service_alias.go file
is.Contains(frame4.Function, "newServiceAlias")
}
func TestInferServiceInfo(t *testing.T) {
t.Parallel()
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)
// Test with lazy service
i := New()
Provide(i, func(i Injector) (*lazyTestHeathcheckerOK, error) {
return &lazyTestHeathcheckerOK{foobar: "foobar"}, nil
})
info, ok := inferServiceInfo(i, "*github.com/samber/do/v2.lazyTestHeathcheckerOK")
is.True(ok)
is.Equal("*github.com/samber/do/v2.lazyTestHeathcheckerOK", info.name)
is.Equal(ServiceTypeLazy, info.serviceType)
is.Equal(time.Duration(0), info.serviceBuildTime) // Not built yet
is.False(info.healthchecker) // Not built yet
is.False(info.shutdowner) // Not built yet
// Build the service
_, _ = Invoke[*lazyTestHeathcheckerOK](i)
info2, ok2 := inferServiceInfo(i, "*github.com/samber/do/v2.lazyTestHeathcheckerOK")
is.True(ok2)
is.Equal("*github.com/samber/do/v2.lazyTestHeathcheckerOK", info2.name)
is.Equal(ServiceTypeLazy, info2.serviceType)
is.Positive(info2.serviceBuildTime) // Should have build time now
is.True(info2.healthchecker) // Should be healthchecker now
is.False(info2.shutdowner) // Not a shutdowner
// Test with eager service
i2 := New()
ProvideValue(i2, &eagerTest{foobar: "foobar"})
info3, ok3 := inferServiceInfo(i2, "*github.com/samber/do/v2.eagerTest")
is.True(ok3)
is.Equal("*github.com/samber/do/v2.eagerTest", info3.name)
is.Equal(ServiceTypeEager, info3.serviceType)
is.Equal(time.Duration(0), info3.serviceBuildTime) // Eager services don't have build time
is.False(info3.healthchecker)
is.False(info3.shutdowner)
// Test with transient service
i3 := New()
ProvideTransient(i3, func(i Injector) (int, error) {
return 42, nil
})
info4, ok4 := inferServiceInfo(i3, "int")
is.True(ok4)
is.Equal("int", info4.name)
is.Equal(ServiceTypeTransient, info4.serviceType)
is.Equal(time.Duration(0), info4.serviceBuildTime) // Transient services don't have build time
is.False(info4.healthchecker)
is.False(info4.shutdowner)
// Test with service that doesn't exist
info5, ok5 := inferServiceInfo(i, "non-existent")
is.False(ok5)
is.Equal(serviceInfo{}, info5)
}
func TestDoServiceCanCastToGeneric(t *testing.T) {
t.Parallel()
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)
svc1 := newServiceLazy("foobar", func(i Injector) (*lazyTestHeathcheckerOK, error) {
return &lazyTestHeathcheckerOK{foobar: "foobar"}, nil
})
is.True(serviceCanCastToGeneric[*lazyTestHeathcheckerOK](svc1))
is.True(serviceCanCastToGeneric[Healthchecker](svc1))
is.False(serviceCanCastToGeneric[Shutdowner](svc1))
is.False(serviceCanCastToGeneric[*lazyTestHeathcheckerKO](svc1))
svc2 := newServiceLazy("foobar", func(i Injector) (iTestHeathchecker, error) {
return &lazyTestHeathcheckerOK{}, nil
})
is.True(serviceCanCastToGeneric[Healthchecker](svc2))
}
func TestDoServiceCanCastToType(t *testing.T) {
t.Parallel()
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)
svc1 := newServiceLazy("foobar", func(i Injector) (*lazyTestHeathcheckerOK, error) {
return &lazyTestHeathcheckerOK{foobar: "foobar"}, nil
})
is.True(serviceCanCastToType(svc1, reflect.TypeOf((**lazyTestHeathcheckerOK)(nil)).Elem()))
is.True(serviceCanCastToType(svc1, reflect.TypeOf((*Healthchecker)(nil)).Elem()))
is.False(serviceCanCastToType(svc1, reflect.TypeOf((*Shutdowner)(nil)).Elem()))
is.False(serviceCanCastToType(svc1, reflect.TypeOf((**lazyTestHeathcheckerKO)(nil)).Elem()))
svc2 := newServiceLazy("foobar", func(i Injector) (iTestHeathchecker, error) {
return &lazyTestHeathcheckerOK{}, nil
})
is.True(serviceCanCastToType(svc2, reflect.TypeOf((*Healthchecker)(nil)).Elem()))
}