github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/communication/nats/message_bytes_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 nats
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/mysteriumnetwork/node/communication"
    24  	"github.com/nats-io/nats.go"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  type bytesMessageProducer struct {
    29  	Message []byte
    30  }
    31  
    32  func (producer *bytesMessageProducer) GetMessageEndpoint() (communication.MessageEndpoint, error) {
    33  	return communication.MessageEndpoint("bytes-message"), nil
    34  }
    35  
    36  func (producer *bytesMessageProducer) Produce() (messagePtr interface{}) {
    37  	return producer.Message
    38  }
    39  
    40  type bytesMessageConsumer struct {
    41  	messageReceived chan interface{}
    42  }
    43  
    44  func (consumer *bytesMessageConsumer) GetMessageEndpoint() (communication.MessageEndpoint, error) {
    45  	return communication.MessageEndpoint("bytes-message"), nil
    46  }
    47  
    48  func (consumer *bytesMessageConsumer) NewMessage() (messagePtr interface{}) {
    49  	var message []byte
    50  	return &message
    51  }
    52  
    53  func (consumer *bytesMessageConsumer) Consume(messagePtr interface{}) error {
    54  	consumer.messageReceived <- messagePtr
    55  	return nil
    56  }
    57  
    58  func TestMessageBytesSend(t *testing.T) {
    59  	connection := StartConnectionMock()
    60  	defer connection.Close()
    61  
    62  	sender := &senderNATS{
    63  		connection: connection,
    64  		codec:      communication.NewCodecBytes(),
    65  	}
    66  
    67  	err := sender.Send(
    68  		&bytesMessageProducer{[]byte("123")},
    69  	)
    70  	assert.NoError(t, err)
    71  	assert.Equal(t, []byte("123"), connection.GetLastMessage())
    72  }
    73  
    74  func TestMessageBytesReceive(t *testing.T) {
    75  	connection := StartConnectionMock()
    76  	defer connection.Close()
    77  
    78  	receiver := &receiverNATS{
    79  		connection: connection,
    80  		codec:      communication.NewCodecBytes(),
    81  		subs:       make(map[string]*nats.Subscription),
    82  	}
    83  
    84  	consumer := &bytesMessageConsumer{messageReceived: make(chan interface{})}
    85  	err := receiver.Receive(consumer)
    86  	assert.NoError(t, err)
    87  
    88  	connection.Publish("bytes-message", []byte("123"))
    89  	message, err := connection.MessageWait(consumer.messageReceived)
    90  	assert.NoError(t, err)
    91  	assert.Equal(t, []byte("123"), *message.(*[]byte))
    92  }