github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/discovery/discovery_test.go (about) 1 /* 2 * Copyright (C) 2017 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package discovery 19 20 import ( 21 "sync" 22 "testing" 23 "time" 24 25 "github.com/stretchr/testify/assert" 26 27 "github.com/mysteriumnetwork/node/eventbus" 28 "github.com/mysteriumnetwork/node/identity" 29 identityregistry "github.com/mysteriumnetwork/node/identity/registry" 30 "github.com/mysteriumnetwork/node/market" 31 ) 32 33 var ( 34 providerID = identity.FromAddress("my-identity") 35 serviceProposal = market.ServiceProposal{ 36 ProviderID: providerID.Address, 37 } 38 ) 39 40 func discoveryWithMockedDependencies() *Discovery { 41 return &Discovery{ 42 statusChan: make(chan Status), 43 proposalAnnouncementStopped: &sync.WaitGroup{}, 44 signerCreate: func(id identity.Identity) identity.Signer { 45 return &identity.SignerFake{} 46 }, 47 proposalRegistry: &mockedProposalRegistry{}, 48 proposalPingTTL: 1 * time.Minute, 49 eventBus: eventbus.New(), 50 stop: make(chan struct{}), 51 } 52 } 53 54 func TestStartRegistersProposal(t *testing.T) { 55 d := discoveryWithMockedDependencies() 56 d.identityRegistry = &identityregistry.FakeRegistry{RegistrationStatus: identityregistry.Registered} 57 58 d.Start(providerID, func() market.ServiceProposal { return serviceProposal }) 59 60 actualStatus := observeStatus(d, PingProposal) 61 assert.Equal(t, PingProposal, actualStatus) 62 } 63 64 func TestStartRegistersIdentitySuccessfully(t *testing.T) { 65 d := discoveryWithMockedDependencies() 66 d.identityRegistry = &identityregistry.FakeRegistry{RegistrationStatus: identityregistry.Unregistered} 67 68 d.Start(providerID, func() market.ServiceProposal { return serviceProposal }) 69 70 actualStatus := observeStatus(d, WaitingForRegistration) 71 assert.Equal(t, WaitingForRegistration, actualStatus) 72 73 d.eventBus.Publish(identityregistry.AppTopicIdentityRegistration, identityregistry.AppEventIdentityRegistration{ 74 ID: providerID, 75 Status: identityregistry.Registered, 76 }) 77 78 actualStatus = observeStatus(d, PingProposal) 79 assert.Equal(t, PingProposal, actualStatus) 80 } 81 82 func TestStartRegisterIdentityCancelled(t *testing.T) { 83 d := discoveryWithMockedDependencies() 84 mockRegistry := &identityregistry.FakeRegistry{RegistrationStatus: identityregistry.Unregistered} 85 d.identityRegistry = mockRegistry 86 87 d.Start(providerID, func() market.ServiceProposal { return serviceProposal }) 88 defer d.Stop() 89 90 actualStatus := observeStatus(d, WaitingForRegistration) 91 assert.Equal(t, WaitingForRegistration, actualStatus) 92 93 d.eventBus.Publish(identityregistry.AppTopicIdentityRegistration, identityregistry.AppEventIdentityRegistration{ 94 ID: providerID, 95 Status: identityregistry.RegistrationError, 96 }) 97 98 actualStatus = observeStatus(d, IdentityRegisterFailed) 99 assert.Equal(t, IdentityRegisterFailed, actualStatus) 100 } 101 102 func TestStartStopUnregisterProposal(t *testing.T) { 103 d := discoveryWithMockedDependencies() 104 d.identityRegistry = &identityregistry.FakeRegistry{RegistrationStatus: identityregistry.Registered} 105 106 d.Start(providerID, func() market.ServiceProposal { return serviceProposal }) 107 108 actualStatus := observeStatus(d, PingProposal) 109 assert.Equal(t, PingProposal, actualStatus) 110 111 d.Stop() 112 113 actualStatus = observeStatus(d, ProposalUnregistered) 114 assert.Equal(t, ProposalUnregistered, actualStatus) 115 } 116 117 func observeStatus(d *Discovery, status Status) Status { 118 for { 119 d.mu.RLock() 120 if d.status == status { 121 d.mu.RUnlock() 122 return d.status 123 } 124 d.mu.RUnlock() 125 time.Sleep(10 * time.Millisecond) 126 } 127 } 128 129 type mockedProposalRegistry struct{} 130 131 func (mockedProposalRegistry) RegisterProposal(proposal market.ServiceProposal, signer identity.Signer) error { 132 return nil 133 } 134 135 func (mockedProposalRegistry) PingProposal(proposal market.ServiceProposal, signer identity.Signer) error { 136 return nil 137 } 138 139 func (mockedProposalRegistry) UnregisterProposal(proposal market.ServiceProposal, signer identity.Signer) error { 140 return nil 141 } 142 143 var _ ProposalRegistry = &mockedProposalRegistry{}