forked from prysmaticlabs/prysm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_registry_test.go
109 lines (81 loc) · 2.78 KB
/
service_registry_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
package runtime
import (
"errors"
"reflect"
"testing"
"github.com/prysmaticlabs/prysm/v5/testing/assert"
"github.com/prysmaticlabs/prysm/v5/testing/require"
)
type mockService struct {
status error
}
type secondMockService struct {
status error
}
func (_ *mockService) Start() {
}
func (_ *mockService) Stop() error {
return nil
}
func (m *mockService) Status() error {
return m.status
}
func (_ *secondMockService) Start() {
}
func (_ *secondMockService) Stop() error {
return nil
}
func (s *secondMockService) Status() error {
return s.status
}
func TestRegisterService_Twice(t *testing.T) {
registry := &ServiceRegistry{
services: make(map[reflect.Type]Service),
}
m := &mockService{}
require.NoError(t, registry.RegisterService(m), "Failed to register first service")
// Checks if first service was indeed registered.
require.Equal(t, 1, len(registry.serviceTypes))
assert.ErrorContains(t, "service already exists", registry.RegisterService(m))
}
func TestRegisterService_Different(t *testing.T) {
registry := &ServiceRegistry{
services: make(map[reflect.Type]Service),
}
m := &mockService{}
s := &secondMockService{}
require.NoError(t, registry.RegisterService(m), "Failed to register first service")
require.NoError(t, registry.RegisterService(s), "Failed to register second service")
require.Equal(t, 2, len(registry.serviceTypes))
_, exists := registry.services[reflect.TypeOf(m)]
assert.Equal(t, true, exists, "service of type %v not registered", reflect.TypeOf(m))
_, exists = registry.services[reflect.TypeOf(s)]
assert.Equal(t, true, exists, "service of type %v not registered", reflect.TypeOf(s))
}
func TestFetchService_OK(t *testing.T) {
registry := &ServiceRegistry{
services: make(map[reflect.Type]Service),
}
m := &mockService{}
require.NoError(t, registry.RegisterService(m), "Failed to register first service")
assert.ErrorContains(t, "input must be of pointer type, received value type instead", registry.FetchService(*m))
var s *secondMockService
assert.ErrorContains(t, "unknown service", registry.FetchService(&s))
var m2 *mockService
require.NoError(t, registry.FetchService(&m2), "Failed to fetch service")
require.Equal(t, m, m2)
}
func TestServiceStatus_OK(t *testing.T) {
registry := &ServiceRegistry{
services: make(map[reflect.Type]Service),
}
m := &mockService{}
require.NoError(t, registry.RegisterService(m), "Failed to register first service")
s := &secondMockService{}
require.NoError(t, registry.RegisterService(s), "Failed to register first service")
m.status = errors.New("something bad has happened")
s.status = errors.New("woah, horsee")
statuses := registry.Statuses()
assert.ErrorContains(t, "something bad has happened", statuses[reflect.TypeOf(m)])
assert.ErrorContains(t, "woah, horsee", statuses[reflect.TypeOf(s)])
}