github.com/status-im/status-go@v1.1.0/server/pairing/statecontrol/state_management_test.go (about)

     1  package statecontrol
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  const (
    10  	cs  = "cs2:4FHRnp:Q4:uqnn"
    11  	cs1 = cs + "1234"
    12  	cs2 = cs + "qwer"
    13  )
    14  
    15  func TestProcessStateManager_StartPairing(t *testing.T) {
    16  	psm := new(ProcessStateManager)
    17  
    18  	// A new psm should start with no error
    19  	err := psm.StartPairing(cs)
    20  	require.NoError(t, err)
    21  
    22  	// A started psm should return an ErrProcessStateManagerAlreadyPairing if another start is attempted
    23  	err = psm.StartPairing(cs)
    24  	require.EqualError(t, err, ErrProcessStateManagerAlreadyPairing.Error())
    25  
    26  	// A psm should start without error if the pairing process has been stopped with an error
    27  	psm.StopPairing(cs, err)
    28  	err = psm.StartPairing(cs)
    29  	require.NoError(t, err)
    30  
    31  	// A psm should return an error if starting with a conn string that previously succeeded (a nil error)
    32  	psm.StopPairing(cs, nil)
    33  	err = psm.StartPairing(cs)
    34  	require.EqualError(t, err, ErrProcessStateManagerAlreadyPaired(cs).Error())
    35  
    36  	// A psm should be able to start with a new connection string if the psm has been stopped
    37  	err = psm.StartPairing(cs1)
    38  	require.NoError(t, err)
    39  
    40  	// A started psm should return an ErrProcessStateManagerAlreadyPairing if another start is attempted
    41  	err = psm.StartPairing(cs1)
    42  	require.EqualError(t, err, ErrProcessStateManagerAlreadyPairing.Error())
    43  
    44  	// A started psm should return an ErrProcessStateManagerAlreadyPairing if another start is attempted regardless of
    45  	// the given connection string.
    46  	err = psm.StartPairing(cs2)
    47  	require.EqualError(t, err, ErrProcessStateManagerAlreadyPairing.Error())
    48  
    49  	// A psm should start without error if the pairing process has been stopped with an error
    50  	psm.StopPairing(cs1, err)
    51  	err = psm.StartPairing(cs2)
    52  	require.NoError(t, err)
    53  
    54  	// A psm should return an error if starting with a conn string that previously succeeded (a nil error)
    55  	psm.StopPairing(cs2, nil)
    56  	err = psm.StartPairing(cs2)
    57  	require.EqualError(t, err, ErrProcessStateManagerAlreadyPaired(cs2).Error())
    58  }