diff --git a/network/gateway/gateway_test.go b/network/gateway/gateway_test.go index 7cb2c1796bd157fc4013d71290b9251f929e05fc..67e46a7e0b3a74c57b73c07025760a6018a699b6 100644 --- a/network/gateway/gateway_test.go +++ b/network/gateway/gateway_test.go @@ -22,7 +22,7 @@ import ( // Unit test func TestNewHostPool(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() rng := csprng.NewSystemRNG() testNdf := getTestNdf(t) testStorage := storage.InitTestingSession(t) @@ -56,7 +56,7 @@ func TestNewHostPool(t *testing.T) { // Unit test func TestHostPool_ManageHostPool(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() rng := csprng.NewSystemRNG() testNdf := getTestNdf(t) testStorage := storage.InitTestingSession(t) @@ -131,7 +131,7 @@ func TestHostPool_ManageHostPool(t *testing.T) { // Full happy path test func TestHostPool_ReplaceHost(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() testNdf := getTestNdf(t) newIndex := uint32(20) @@ -238,7 +238,7 @@ func TestHostPool_ReplaceHost(t *testing.T) { // Error path, could not get host func TestHostPool_ReplaceHost_Error(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() // Construct a manager (bypass business logic in constructor) hostPool := &HostPool{ @@ -259,7 +259,7 @@ func TestHostPool_ReplaceHost_Error(t *testing.T) { // Happy path func TestHostPool_PruneHostPool(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() testNdf := getTestNdf(t) newIndex := uint32(20) params := DefaultPoolParams() @@ -329,7 +329,7 @@ func TestHostPool_PruneHostPool(t *testing.T) { // Error path: not enough gateways in ndf compared to // required pool size func TestHostPool_PruneHostPool_Error(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() testNdf := getTestNdf(t) newIndex := uint32(20) params := DefaultPoolParams() @@ -359,7 +359,7 @@ func TestHostPool_PruneHostPool_Error(t *testing.T) { // Unit test func TestHostPool_UpdateNdf(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() testNdf := getTestNdf(t) newIndex := uint32(20) @@ -394,7 +394,7 @@ func TestHostPool_UpdateNdf(t *testing.T) { // Full test func TestHostPool_GetPreferred(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() rng := csprng.NewSystemRNG() testNdf := getTestNdf(t) testStorage := storage.InitTestingSession(t) @@ -466,7 +466,7 @@ func TestHostPool_GetPreferred(t *testing.T) { // Unit test func TestHostPool_GetAny(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() rng := csprng.NewSystemRNG() testNdf := getTestNdf(t) testStorage := storage.InitTestingSession(t) @@ -523,7 +523,7 @@ func TestHostPool_GetAny(t *testing.T) { // Unit test func TestHostPool_ForceAdd(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() rng := csprng.NewSystemRNG() testNdf := getTestNdf(t) testStorage := storage.InitTestingSession(t) @@ -584,7 +584,7 @@ func TestHostPool_ForceAdd(t *testing.T) { // Unit test which only adds information to ndf func TestHostPool_UpdateConns_AddGateways(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() rng := csprng.NewSystemRNG() testNdf := getTestNdf(t) testStorage := storage.InitTestingSession(t) @@ -659,7 +659,7 @@ func TestHostPool_UpdateConns_AddGateways(t *testing.T) { // Unit test which only adds information to ndf func TestHostPool_UpdateConns_RemoveGateways(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() rng := csprng.NewSystemRNG() testNdf := getTestNdf(t) testStorage := storage.InitTestingSession(t) @@ -732,7 +732,7 @@ func TestHostPool_UpdateConns_RemoveGateways(t *testing.T) { // Unit test func TestHostPool_AddGateway(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() testNdf := getTestNdf(t) newIndex := uint32(20) params := DefaultPoolParams() @@ -765,7 +765,7 @@ func TestHostPool_AddGateway(t *testing.T) { // Unit test func TestHostPool_RemoveGateway(t *testing.T) { - manager := newHappyManager() + manager := newMockManager() testNdf := getTestNdf(t) newIndex := uint32(20) params := DefaultPoolParams() diff --git a/network/gateway/utils_test.go b/network/gateway/utils_test.go index 128e6cc2ac76c3d1de058e1580b6b96269e0e35e..9cb76c7834e9c8f51bde01f50e3e42e209ef412e 100644 --- a/network/gateway/utils_test.go +++ b/network/gateway/utils_test.go @@ -1,49 +1,29 @@ package gateway import ( - "errors" "gitlab.com/xx_network/comms/connect" "gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/ndf" ) -// Mock structure adhering to HostManager which returns the error -// path for all it's methods -type MangerErrorPath struct{} - -// Constructor for MangerErrorPath -func newErrorManager() *MangerErrorPath { - return &MangerErrorPath{} -} - -func (mep *MangerErrorPath) GetHost(hostId *id.ID) (*connect.Host, bool) { - return nil, false -} -func (mep *MangerErrorPath) AddHost(hid *id.ID, address string, - cert []byte, params connect.HostParams) (host *connect.Host, err error) { - return nil, errors.New("Failed to add host") -} - -func (mep *MangerErrorPath) RemoveHost(hid *id.ID) {} - // Mock structure adhering to HostManager to be used for happy path -type ManagerHappyPath struct { +type mockManager struct { hosts map[string]*connect.Host } -// Constructor for ManagerHappyPath -func newHappyManager() *ManagerHappyPath { - return &ManagerHappyPath{ +// Constructor for mockManager +func newMockManager() *mockManager { + return &mockManager{ hosts: make(map[string]*connect.Host), } } -func (mhp *ManagerHappyPath) GetHost(hostId *id.ID) (*connect.Host, bool) { +func (mhp *mockManager) GetHost(hostId *id.ID) (*connect.Host, bool) { h, ok := mhp.hosts[hostId.String()] return h, ok } -func (mhp *ManagerHappyPath) AddHost(hid *id.ID, address string, +func (mhp *mockManager) AddHost(hid *id.ID, address string, cert []byte, params connect.HostParams) (host *connect.Host, err error) { host, err = connect.NewHost(hid, address, cert, params) if err != nil { @@ -55,7 +35,7 @@ func (mhp *ManagerHappyPath) AddHost(hid *id.ID, address string, return } -func (mhp *ManagerHappyPath) RemoveHost(hid *id.ID) { +func (mhp *mockManager) RemoveHost(hid *id.ID) { delete(mhp.hosts, hid.String()) }