github.com/prysmaticlabs/prysm@v1.4.4/shared/service_registry_test.go (about)

     1  package shared
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    10  )
    11  
    12  type mockService struct {
    13  	status error
    14  }
    15  type secondMockService struct {
    16  	status error
    17  }
    18  
    19  func (m *mockService) Start() {
    20  }
    21  
    22  func (m *mockService) Stop() error {
    23  	return nil
    24  }
    25  
    26  func (m *mockService) Status() error {
    27  	return m.status
    28  }
    29  
    30  func (s *secondMockService) Start() {
    31  }
    32  
    33  func (s *secondMockService) Stop() error {
    34  	return nil
    35  }
    36  
    37  func (s *secondMockService) Status() error {
    38  	return s.status
    39  }
    40  
    41  func TestRegisterService_Twice(t *testing.T) {
    42  	registry := &ServiceRegistry{
    43  		services: make(map[reflect.Type]Service),
    44  	}
    45  
    46  	m := &mockService{}
    47  	require.NoError(t, registry.RegisterService(m), "Failed to register first service")
    48  
    49  	// Checks if first service was indeed registered.
    50  	require.Equal(t, 1, len(registry.serviceTypes))
    51  	assert.ErrorContains(t, "service already exists", registry.RegisterService(m))
    52  }
    53  
    54  func TestRegisterService_Different(t *testing.T) {
    55  	registry := &ServiceRegistry{
    56  		services: make(map[reflect.Type]Service),
    57  	}
    58  
    59  	m := &mockService{}
    60  	s := &secondMockService{}
    61  	require.NoError(t, registry.RegisterService(m), "Failed to register first service")
    62  	require.NoError(t, registry.RegisterService(s), "Failed to register second service")
    63  
    64  	require.Equal(t, 2, len(registry.serviceTypes))
    65  
    66  	_, exists := registry.services[reflect.TypeOf(m)]
    67  	assert.Equal(t, true, exists, "service of type %v not registered", reflect.TypeOf(m))
    68  
    69  	_, exists = registry.services[reflect.TypeOf(s)]
    70  	assert.Equal(t, true, exists, "service of type %v not registered", reflect.TypeOf(s))
    71  }
    72  
    73  func TestFetchService_OK(t *testing.T) {
    74  	registry := &ServiceRegistry{
    75  		services: make(map[reflect.Type]Service),
    76  	}
    77  
    78  	m := &mockService{}
    79  	require.NoError(t, registry.RegisterService(m), "Failed to register first service")
    80  
    81  	assert.ErrorContains(t, "input must be of pointer type, received value type instead", registry.FetchService(*m))
    82  
    83  	var s *secondMockService
    84  	assert.ErrorContains(t, "unknown service", registry.FetchService(&s))
    85  
    86  	var m2 *mockService
    87  	require.NoError(t, registry.FetchService(&m2), "Failed to fetch service")
    88  	require.Equal(t, m, m2)
    89  }
    90  
    91  func TestServiceStatus_OK(t *testing.T) {
    92  	registry := &ServiceRegistry{
    93  		services: make(map[reflect.Type]Service),
    94  	}
    95  
    96  	m := &mockService{}
    97  	require.NoError(t, registry.RegisterService(m), "Failed to register first service")
    98  
    99  	s := &secondMockService{}
   100  	require.NoError(t, registry.RegisterService(s), "Failed to register first service")
   101  
   102  	m.status = errors.New("something bad has happened")
   103  	s.status = errors.New("woah, horsee")
   104  
   105  	statuses := registry.Statuses()
   106  
   107  	assert.ErrorContains(t, "something bad has happened", statuses[reflect.TypeOf(m)])
   108  	assert.ErrorContains(t, "woah, horsee", statuses[reflect.TypeOf(s)])
   109  }