-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_test.go
155 lines (124 loc) · 2.71 KB
/
container_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package simpledi
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestContainer(t *testing.T) {
type sum struct {
a int
b int
}
type mult struct {
a int
b int
}
sumConstructor := func() sum {
fmt.Printf("summation constructor called -->>\n")
s := sum{
a: 1,
b: 2,
}
return s
}
mulConstructor := func() mult {
fmt.Printf("multiplication constructor called -->>\n")
m := mult{
a: 9,
b: 10,
}
return m
}
usage := func(a sum, b mult) {
fmt.Printf("invoker called -->>\nsum is : %d\nmul is : %d\n", a.a+a.b, b.a*b.b)
}
c, err := New(
WithProvider(sumConstructor),
WithProvider(mulConstructor),
WithInvoker(usage, false),
)
require.NoError(t, err, "all dependencies are satisfied !")
err = c.Run()
require.NoError(t, err)
// Output
// summation constructor called -->>
// multiplication constructor called -->>
// invoker called -->>
// sum is : 3
// mul is : 90
}
func TestContainer_Retry(t *testing.T) {
type bar struct {
value string
}
type foo struct {
value string
}
fooConstructor := func(b bar) foo {
fmt.Printf("foo constructor called -->> %s\n", b.value)
f := foo{
value: "foo called !",
}
return f
}
barConstructor := func() bar {
fmt.Printf("bar constructor called -->>\n")
b := bar{
value: "bar called !",
}
return b
}
usage := func(b bar, f foo) {
fmt.Printf("invoker called -->>\nfoo is : %s\nbar is : %s\n", f.value, b.value)
}
c, err := New(
WithProvider(fooConstructor),
WithProvider(barConstructor),
WithInvoker(usage, false),
)
require.NoError(t, err, "all dependencies are satisfied !")
err = c.Run()
require.NoError(t, err)
// Output
// bar constructor called -->>
// foo constructor called -->> bar called !
// invoker called -->>
// foo is : foo called !
// bar is : bar called !
}
func TestConstructor_Cycle(t *testing.T) {
type bar struct {
value string
}
type foo struct {
value string
}
fooConstructor := func(b bar) foo {
fmt.Printf("foo constructor called -->> %s !!!!\n", b.value)
f := foo{
value: "foo called !",
}
return f
}
barConstructor := func(f foo) bar {
fmt.Printf("bar constructor called -->> %s !!!!\n", f.value)
b := bar{
value: "bar called !",
}
return b
}
usage := func(b bar, f foo) {
fmt.Printf("invoker called -->>\nfoo is : %s\nbar is : %s\n", f.value, b.value)
}
c, err := New(
WithProvider(fooConstructor),
WithProvider(barConstructor),
WithInvoker(usage, false),
)
require.NoError(t, err, "all dependencies are satisfied !")
err = c.Run()
require.Error(t, err)
fmt.Printf("got error %v\n", err)
// Output
// got error probably there are a cycle in given dependency tree, container cant't proceed !
}