github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/ui/discovery/discovery_test.go (about)

     1  /*
     2   * Copyright (C) 2019 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  	"errors"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  type mockDiscovery struct {
    28  	err     error
    29  	started bool
    30  	stopped bool
    31  }
    32  
    33  func (md *mockDiscovery) Start() error {
    34  	md.started = true
    35  	return md.err
    36  }
    37  
    38  func (md *mockDiscovery) Stop() error {
    39  	md.stopped = true
    40  	return md.err
    41  }
    42  
    43  func Test_multiDiscovery_StartsAll(t *testing.T) {
    44  	d1 := &mockDiscovery{}
    45  	d2 := &mockDiscovery{}
    46  
    47  	md := multiDiscovery{
    48  		bonjour: d1,
    49  		ssdp:    d2,
    50  	}
    51  
    52  	err := md.Start()
    53  	assert.NoError(t, err)
    54  
    55  	assert.True(t, d1.started)
    56  	assert.True(t, d2.started)
    57  }
    58  
    59  func Test_multiDiscovery_StartsWithError(t *testing.T) {
    60  	d1 := &mockDiscovery{err: errors.New("error1")}
    61  	d2 := &mockDiscovery{err: errors.New("error2")}
    62  
    63  	md := multiDiscovery{
    64  		bonjour: d1,
    65  		ssdp:    d2,
    66  	}
    67  
    68  	err := md.Start()
    69  	assert.EqualError(t, err, "error1")
    70  
    71  	assert.True(t, d1.started)
    72  	assert.False(t, d2.started)
    73  }
    74  
    75  func Test_multiDiscovery_StopsAll(t *testing.T) {
    76  	d1 := &mockDiscovery{}
    77  	d2 := &mockDiscovery{}
    78  
    79  	md := multiDiscovery{
    80  		bonjour: d1,
    81  		ssdp:    d2,
    82  	}
    83  
    84  	err := md.Stop()
    85  	assert.NoError(t, err)
    86  
    87  	assert.True(t, d1.stopped)
    88  	assert.True(t, d2.stopped)
    89  }
    90  
    91  func Test_multiDiscovery_StopsWithError(t *testing.T) {
    92  	d1 := &mockDiscovery{err: errors.New("error1")}
    93  	d2 := &mockDiscovery{err: errors.New("error2")}
    94  
    95  	md := multiDiscovery{
    96  		bonjour: d1,
    97  		ssdp:    d2,
    98  	}
    99  
   100  	err := md.Stop()
   101  	assert.EqualError(t, err, "error2")
   102  
   103  	assert.True(t, d1.stopped)
   104  	assert.True(t, d2.stopped)
   105  }