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