github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/common/service/message_test.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package service
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestAction_MsgEvents(t *testing.T) {
    16  	m := Message{}
    17  	require.Nil(t, m.MsgEvents())
    18  }
    19  
    20  func TestAction_RegisterMsgEvent(t *testing.T) {
    21  	m := Message{}
    22  
    23  	// cannot register nil channel
    24  	require.EqualError(t, m.RegisterMsgEvent(nil), ErrNilChannel.Error())
    25  
    26  	ch := make(chan<- StateMsg)
    27  	require.Nil(t, m.RegisterMsgEvent(ch))
    28  	require.Equal(t, 1, len(m.MsgEvents()))
    29  
    30  	// double register
    31  	require.Nil(t, m.RegisterMsgEvent(ch))
    32  	require.Equal(t, 2, len(m.MsgEvents()))
    33  }
    34  
    35  func TestAction_UnregisterMsgEvent(t *testing.T) {
    36  	m := Message{}
    37  
    38  	ch := make(chan<- StateMsg)
    39  	require.Nil(t, m.RegisterMsgEvent(ch))
    40  	require.Equal(t, 1, len(m.MsgEvents()))
    41  	require.Nil(t, m.UnregisterMsgEvent(ch))
    42  	require.Equal(t, 0, len(m.MsgEvents()))
    43  
    44  	// double register
    45  	require.Nil(t, m.RegisterMsgEvent(ch))
    46  	require.Nil(t, m.RegisterMsgEvent(ch))
    47  	require.Equal(t, 2, len(m.MsgEvents()))
    48  	require.Nil(t, m.UnregisterMsgEvent(ch))
    49  	require.Equal(t, 0, len(m.MsgEvents()))
    50  
    51  	// no error if nothing to unregister
    52  	require.Nil(t, m.UnregisterMsgEvent(ch))
    53  }