github.com/braveheart12/just@v0.8.7/network/state/switcher_test.go (about)

     1  /*
     2   * The Clear BSD License
     3   *
     4   * Copyright (c) 2019 Insolar Technologies
     5   *
     6   * All rights reserved.
     7   *
     8   * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
     9   *
    10   *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11   *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12   *  Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13   *
    14   * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15   *
    16   */
    17  
    18  package state
    19  
    20  import (
    21  	"context"
    22  	"sync"
    23  	"testing"
    24  
    25  	"github.com/insolar/insolar/component"
    26  	"github.com/insolar/insolar/core"
    27  	"github.com/insolar/insolar/testutils/network"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func mockSwitcherWorkAround(t *testing.T, isBootstrapped bool) *network.SwitcherWorkAroundMock {
    32  	swaMock := network.NewSwitcherWorkAroundMock(t)
    33  	swaMock.IsBootstrappedFunc = func() bool {
    34  		return isBootstrapped
    35  	}
    36  	return swaMock
    37  }
    38  
    39  func mockMessageBusLocker(t *testing.T) *messageBusLockerMock {
    40  	mblMock := NewmessageBusLockerMock(t)
    41  	mblMock.UnlockFunc = func(p context.Context) {}
    42  	return mblMock
    43  }
    44  
    45  func TestNewNetworkSwitcher(t *testing.T) {
    46  	nodeNet := network.NewNodeNetworkMock(t)
    47  	switcherWorkAround := mockSwitcherWorkAround(t, false)
    48  	messageBusLocker := mockMessageBusLocker(t)
    49  
    50  	switcher, err := NewNetworkSwitcher()
    51  	require.NoError(t, err)
    52  
    53  	cm := &component.Manager{}
    54  	cm.Inject(nodeNet, switcherWorkAround, messageBusLocker, switcher)
    55  
    56  	require.Equal(t, nodeNet, switcher.NodeNetwork)
    57  	require.Equal(t, switcherWorkAround, switcher.SwitcherWorkAround)
    58  	require.Equal(t, messageBusLocker, switcher.MBLocker)
    59  	require.Equal(t, core.NoNetworkState, switcher.state)
    60  	require.Equal(t, sync.RWMutex{}, switcher.stateLock)
    61  }
    62  
    63  func TestGetState(t *testing.T) {
    64  	switcher, err := NewNetworkSwitcher()
    65  	require.NoError(t, err)
    66  
    67  	state := switcher.GetState()
    68  	require.Equal(t, core.NoNetworkState, state)
    69  }
    70  
    71  func TestOnPulseNoChange(t *testing.T) {
    72  	switcher, err := NewNetworkSwitcher()
    73  	require.NoError(t, err)
    74  	switcherWorkAround := mockSwitcherWorkAround(t, false)
    75  	nodeNet := network.NewNodeNetworkMock(t)
    76  	messageBusLocker := mockMessageBusLocker(t)
    77  
    78  	cm := &component.Manager{}
    79  	cm.Inject(switcherWorkAround, switcher, nodeNet, messageBusLocker)
    80  
    81  	err = switcher.OnPulse(context.Background(), core.Pulse{})
    82  	require.NoError(t, err)
    83  	require.Equal(t, core.NoNetworkState, switcher.state)
    84  	require.Equal(t, uint64(0), messageBusLocker.UnlockCounter)
    85  }
    86  
    87  func TestOnPulseStateChanged(t *testing.T) {
    88  	switcher, err := NewNetworkSwitcher()
    89  	require.NoError(t, err)
    90  	switcherWorkAround := mockSwitcherWorkAround(t, true)
    91  	nodeNet := network.NewNodeNetworkMock(t)
    92  	messageBusLocker := mockMessageBusLocker(t)
    93  
    94  	cm := &component.Manager{}
    95  	cm.Inject(switcherWorkAround, switcher, nodeNet, messageBusLocker)
    96  
    97  	err = switcher.OnPulse(context.Background(), core.Pulse{})
    98  	require.NoError(t, err)
    99  	require.Equal(t, core.CompleteNetworkState, switcher.state)
   100  	require.Equal(t, uint64(1), messageBusLocker.UnlockCounter)
   101  }
   102  
   103  func TestGetStateAfterStateChanged(t *testing.T) {
   104  	switcher, err := NewNetworkSwitcher()
   105  	require.NoError(t, err)
   106  	switcherWorkAround := mockSwitcherWorkAround(t, true)
   107  	nodeNet := network.NewNodeNetworkMock(t)
   108  	messageBusLocker := mockMessageBusLocker(t)
   109  
   110  	cm := &component.Manager{}
   111  	cm.Inject(switcherWorkAround, switcher, nodeNet, messageBusLocker)
   112  
   113  	err = switcher.OnPulse(context.Background(), core.Pulse{})
   114  	require.NoError(t, err)
   115  	require.Equal(t, core.CompleteNetworkState, switcher.state)
   116  
   117  	state := switcher.GetState()
   118  	require.Equal(t, core.CompleteNetworkState, state)
   119  }