-
Notifications
You must be signed in to change notification settings - Fork 2
/
eager_singleton_test.go
119 lines (86 loc) · 3.11 KB
/
eager_singleton_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
package ore
import (
"context"
"testing"
"github.com/firasdarwish/ore/internal/interfaces"
"github.com/firasdarwish/ore/internal/models"
"github.com/stretchr/testify/assert"
)
func TestRegisterSingleton(t *testing.T) {
clearAll()
RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{})
c, _ := Get[interfaces.SomeCounter](context.Background())
c.AddOne()
c.AddOne()
c.AddOne()
RegisterSingletonToContainer[interfaces.SomeCounter](DefaultContainer, &models.SimpleCounter{})
c, _ = Get[interfaces.SomeCounter](context.Background())
c.AddOne()
c.AddOne()
if got := c.GetCount(); got != 2 {
t.Errorf("got %v, expected %v", got, 2)
}
}
func TestRegisterSingletonNilImplementation(t *testing.T) {
clearAll()
assert.Panics(t, func() {
RegisterSingleton[interfaces.SomeCounter](nil)
})
}
func TestRegisterSingletonMultipleImplementations(t *testing.T) {
clearAll()
RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{})
RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{})
RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{})
counters, _ := GetList[interfaces.SomeCounter](context.Background())
if got := len(counters); got != 3 {
t.Errorf("got %v, expected %v", got, 3)
}
}
func TestRegisterSingletonMultipleImplementationsKeyed(t *testing.T) {
clearAll()
RegisterKeyedSingleton[interfaces.SomeCounter](&models.SimpleCounter{}, "firas")
RegisterKeyedSingletonToContainer[interfaces.SomeCounter](DefaultContainer, &models.SimpleCounter{}, "firas")
RegisterKeyedSingleton[interfaces.SomeCounter](&models.SimpleCounter{}, "firas")
RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{})
counters, _ := GetKeyedList[interfaces.SomeCounter](context.Background(), "firas")
if got := len(counters); got != 3 {
t.Errorf("got %v, expected %v", got, 3)
}
}
func TestRegisterSingletonSingletonState(t *testing.T) {
clearAll()
RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{})
c, _ := Get[interfaces.SomeCounter](context.Background())
c.AddOne()
c.AddOne()
c, _ = Get[interfaces.SomeCounter](context.Background())
c.AddOne()
c, _ = Get[interfaces.SomeCounter](context.Background())
c.AddOne()
c.AddOne()
c.AddOne()
if got := c.GetCount(); got != 6 {
t.Errorf("got %v, expected %v", got, 6)
}
}
func TestRegisterSingletonGeneric(t *testing.T) {
clearAll()
RegisterSingleton[interfaces.SomeCounterGeneric[uint]](&models.CounterGeneric[uint]{})
c, _ := Get[interfaces.SomeCounterGeneric[uint]](context.Background())
c.Add(5)
c.Add(5)
if got := c.GetCount(); got != 10 {
t.Errorf("got %v, expected %v", got, 10)
}
}
func TestRegisterSingletonMultipleGenericImplementations(t *testing.T) {
clearAll()
RegisterSingleton[interfaces.SomeCounterGeneric[uint]](&models.CounterGeneric[uint]{})
RegisterSingleton[interfaces.SomeCounterGeneric[uint]](&models.CounterGeneric[uint]{})
RegisterSingleton[interfaces.SomeCounterGeneric[uint]](&models.CounterGeneric[uint]{})
counters, _ := GetList[interfaces.SomeCounterGeneric[uint]](context.Background())
if got := len(counters); got != 3 {
t.Errorf("got %v, expected %v", got, 3)
}
}