2021-09-17 19:20:50 +00:00
|
|
|
package runtime
|
2018-07-14 02:15:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-12-30 21:20:43 +00:00
|
|
|
"errors"
|
2018-07-14 02:15:37 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-08-25 10:18:29 +00:00
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
2018-07-14 02:15:37 +00:00
|
|
|
)
|
|
|
|
|
2018-12-30 21:20:43 +00:00
|
|
|
type mockService struct {
|
|
|
|
status error
|
|
|
|
}
|
|
|
|
type secondMockService struct {
|
|
|
|
status error
|
|
|
|
}
|
2018-07-14 02:15:37 +00:00
|
|
|
|
2021-12-07 17:52:39 +00:00
|
|
|
func (_ *mockService) Start() {
|
2018-07-14 02:15:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:52:39 +00:00
|
|
|
func (_ *mockService) Stop() error {
|
2018-07-14 02:15:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-30 21:20:43 +00:00
|
|
|
func (m *mockService) Status() error {
|
|
|
|
return m.status
|
|
|
|
}
|
|
|
|
|
2021-12-07 17:52:39 +00:00
|
|
|
func (_ *secondMockService) Start() {
|
2018-07-14 02:15:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:52:39 +00:00
|
|
|
func (_ *secondMockService) Stop() error {
|
2018-07-14 02:15:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-30 21:20:43 +00:00
|
|
|
func (s *secondMockService) Status() error {
|
|
|
|
return s.status
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestRegisterService_Twice(t *testing.T) {
|
2018-07-14 02:15:37 +00:00
|
|
|
registry := &ServiceRegistry{
|
|
|
|
services: make(map[reflect.Type]Service),
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &mockService{}
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, registry.RegisterService(m), "Failed to register first service")
|
2018-07-14 02:15:37 +00:00
|
|
|
|
|
|
|
// Checks if first service was indeed registered.
|
2020-08-25 10:18:29 +00:00
|
|
|
require.Equal(t, 1, len(registry.serviceTypes))
|
2020-08-25 15:23:06 +00:00
|
|
|
assert.ErrorContains(t, "service already exists", registry.RegisterService(m))
|
2018-07-14 02:15:37 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestRegisterService_Different(t *testing.T) {
|
2018-07-14 02:15:37 +00:00
|
|
|
registry := &ServiceRegistry{
|
|
|
|
services: make(map[reflect.Type]Service),
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &mockService{}
|
|
|
|
s := &secondMockService{}
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, registry.RegisterService(m), "Failed to register first service")
|
|
|
|
require.NoError(t, registry.RegisterService(s), "Failed to register second service")
|
2018-07-14 02:15:37 +00:00
|
|
|
|
2020-08-25 10:18:29 +00:00
|
|
|
require.Equal(t, 2, len(registry.serviceTypes))
|
2018-07-14 02:15:37 +00:00
|
|
|
|
2020-08-25 15:23:06 +00:00
|
|
|
_, exists := registry.services[reflect.TypeOf(m)]
|
|
|
|
assert.Equal(t, true, exists, "service of type %v not registered", reflect.TypeOf(m))
|
2018-07-14 02:15:37 +00:00
|
|
|
|
2020-08-25 15:23:06 +00:00
|
|
|
_, exists = registry.services[reflect.TypeOf(s)]
|
|
|
|
assert.Equal(t, true, exists, "service of type %v not registered", reflect.TypeOf(s))
|
2018-07-14 02:15:37 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestFetchService_OK(t *testing.T) {
|
2018-07-14 02:15:37 +00:00
|
|
|
registry := &ServiceRegistry{
|
|
|
|
services: make(map[reflect.Type]Service),
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &mockService{}
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, registry.RegisterService(m), "Failed to register first service")
|
2018-07-14 02:15:37 +00:00
|
|
|
|
2020-08-25 15:23:06 +00:00
|
|
|
assert.ErrorContains(t, "input must be of pointer type, received value type instead", registry.FetchService(*m))
|
2018-07-14 02:15:37 +00:00
|
|
|
|
|
|
|
var s *secondMockService
|
2020-08-25 15:23:06 +00:00
|
|
|
assert.ErrorContains(t, "unknown service", registry.FetchService(&s))
|
2018-07-14 02:15:37 +00:00
|
|
|
|
|
|
|
var m2 *mockService
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, registry.FetchService(&m2), "Failed to fetch service")
|
2020-08-25 10:18:29 +00:00
|
|
|
require.Equal(t, m, m2)
|
2018-07-14 02:15:37 +00:00
|
|
|
}
|
2018-12-30 21:20:43 +00:00
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestServiceStatus_OK(t *testing.T) {
|
2018-12-30 21:20:43 +00:00
|
|
|
registry := &ServiceRegistry{
|
|
|
|
services: make(map[reflect.Type]Service),
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &mockService{}
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, registry.RegisterService(m), "Failed to register first service")
|
2018-12-30 21:20:43 +00:00
|
|
|
|
|
|
|
s := &secondMockService{}
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, registry.RegisterService(s), "Failed to register first service")
|
2018-12-30 21:20:43 +00:00
|
|
|
|
|
|
|
m.status = errors.New("something bad has happened")
|
|
|
|
s.status = errors.New("woah, horsee")
|
|
|
|
|
|
|
|
statuses := registry.Statuses()
|
|
|
|
|
2020-08-25 15:23:06 +00:00
|
|
|
assert.ErrorContains(t, "something bad has happened", statuses[reflect.TypeOf(m)])
|
|
|
|
assert.ErrorContains(t, "woah, horsee", statuses[reflect.TypeOf(s)])
|
2018-12-30 21:20:43 +00:00
|
|
|
}
|