prysm-pulse/shared/service_registry_test.go
Victor Farazdagi ba07ccb484
Apply testutils assertions: final cleanup (#7003)
* slasher/beaconclient tests
* slasher/db/kv tests
* Merge branch 'master' into apply-testutils-assertions-to-slasher
* fix build
* slasher/detection tests
* rest of the tests
* misc tests
* tools tests
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge branch 'master' into apply-testutils-assertions-misc
* agg tests
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge branch 'master' into apply-testutils-assertions-misc
* Merge branch 'apply-testutils-assertions-misc' of github.com:prysmaticlabs/prysm into apply-testutils-assertions-misc
* updates aggregated_test
* beacon-chain/operations/attestations/kv/* tests updated
* beacon-chain/operations/attestations tests updated
* beacon-chain/operations/slashings tests updated
* Merge branch 'master' into apply-testutils-assertions-misc
* gazelle
* beacon-chain/core tests updated
* fixes test
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge branch 'apply-testutils-assertions-misc' of github.com:prysmaticlabs/prysm into apply-testutils-assertions-misc
* beacon-chain/rpc tests updated
* beacon-chain/sync/initial-sync tests
* misc tests
* optimizes error message parsing in testutils
* Merge branch 'assertutils-optimize-processing' into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* endtoend tests
* Merge branch 'apply-testutils-assertions-misc' of github.com:prysmaticlabs/prysm into apply-testutils-assertions-misc
* gazelle
* Merge refs/heads/master into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* beacon-chain/blockchain tests updated
* Merge branch 'apply-testutils-assertions-misc' of github.com:prysmaticlabs/prysm into apply-testutils-assertions-misc
* beacon-chain/state/stategen tests updated
* beacon-chain all left-over tests are done
* Merge refs/heads/master into apply-testutils-assertions-misc
* validator tests updated
* slasher tests
* Merge branch 'master' into apply-testutils-assertions-misc
* gofmt
* gazelle
* Merge refs/heads/master into apply-testutils-assertions-misc
* shared upd
* end2end tests deps fixed
* Merge branch 'apply-testutils-assertions-misc' of github.com:prysmaticlabs/prysm into apply-testutils-assertions-misc
* Merge refs/heads/master into apply-testutils-assertions-misc
* misc
* all tests are updated
* Merge branch 'apply-testutils-assertions-misc' of github.com:prysmaticlabs/prysm into apply-testutils-assertions-misc
2020-08-25 15:23:06 +00:00

110 lines
2.8 KiB
Go

package shared
import (
"errors"
"reflect"
"testing"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
type mockService struct {
status error
}
type secondMockService struct {
status error
}
func (m *mockService) Start() {
}
func (m *mockService) Stop() error {
return nil
}
func (m *mockService) Status() error {
return m.status
}
func (s *secondMockService) Start() {
}
func (s *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)])
}