github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/communication/nats/request_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  	"time"
    23  
    24  	"github.com/mysteriumnetwork/node/communication"
    25  	"github.com/nats-io/nats.go"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  type bytesRequestProducer struct {
    30  	Request []byte
    31  }
    32  
    33  func (producer *bytesRequestProducer) GetRequestEndpoint() (communication.RequestEndpoint, error) {
    34  	return communication.RequestEndpoint("bytes-request"), nil
    35  }
    36  
    37  func (producer *bytesRequestProducer) NewResponse() (responsePtr interface{}) {
    38  	var response []byte
    39  	return &response
    40  }
    41  
    42  func (producer *bytesRequestProducer) Produce() (requestPtr interface{}) {
    43  	return producer.Request
    44  }
    45  
    46  func TestBytesRequest(t *testing.T) {
    47  	connection := StartConnectionMock()
    48  	connection.MockResponse("bytes-request", []byte("RESPONSE"))
    49  	defer connection.Close()
    50  
    51  	sender := &senderNATS{
    52  		connection:     connection,
    53  		codec:          communication.NewCodecBytes(),
    54  		timeoutRequest: 100 * time.Millisecond,
    55  	}
    56  
    57  	response, err := sender.Request(&bytesRequestProducer{
    58  		[]byte("REQUEST"),
    59  	})
    60  	assert.NoError(t, err)
    61  	assert.Equal(t, []byte("REQUEST"), connection.GetLastRequest())
    62  	assert.Equal(t, []byte("RESPONSE"), *response.(*[]byte))
    63  }
    64  
    65  type bytesRequestConsumer struct {
    66  	requestReceived interface{}
    67  }
    68  
    69  func (consumer *bytesRequestConsumer) GetRequestEndpoint() (communication.RequestEndpoint, error) {
    70  	return communication.RequestEndpoint("bytes-response"), nil
    71  }
    72  
    73  func (consumer *bytesRequestConsumer) NewRequest() (requestPtr interface{}) {
    74  	var request []byte
    75  	return &request
    76  }
    77  
    78  func (consumer *bytesRequestConsumer) Consume(requestPtr interface{}) (responsePtr interface{}, err error) {
    79  	consumer.requestReceived = requestPtr
    80  	return []byte("RESPONSE"), nil
    81  }
    82  
    83  func TestBytesRespond(t *testing.T) {
    84  	connection := StartConnectionMock()
    85  	defer connection.Close()
    86  
    87  	receiver := &receiverNATS{
    88  		connection: connection,
    89  		codec:      communication.NewCodecBytes(),
    90  		subs:       make(map[string]*nats.Subscription),
    91  	}
    92  
    93  	consumer := &bytesRequestConsumer{}
    94  	err := receiver.Respond(consumer)
    95  	assert.NoError(t, err)
    96  
    97  	response, err := connection.Request("bytes-response", []byte("REQUEST"), 100*time.Millisecond)
    98  	assert.NoError(t, err)
    99  	assert.Equal(t, []byte("REQUEST"), *consumer.requestReceived.(*[]byte))
   100  	assert.Equal(t, []byte("RESPONSE"), response.Data)
   101  }