github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/communication/nats/message_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 23 "github.com/mysteriumnetwork/node/communication" 24 "github.com/nats-io/nats.go" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 type customMessage struct { 29 Field int 30 } 31 32 type customMessageProducer struct { 33 Message *customMessage 34 } 35 36 func (producer *customMessageProducer) GetMessageEndpoint() (communication.MessageEndpoint, error) { 37 return communication.MessageEndpoint("custom-message"), nil 38 } 39 40 func (producer *customMessageProducer) Produce() (messagePtr interface{}) { 41 return producer.Message 42 } 43 44 func TestMessageCustomSend(t *testing.T) { 45 connection := StartConnectionMock() 46 defer connection.Close() 47 48 sender := &senderNATS{ 49 connection: connection, 50 codec: communication.NewCodecJSON(), 51 } 52 53 err := sender.Send(&customMessageProducer{&customMessage{123}}) 54 assert.NoError(t, err) 55 assert.JSONEq(t, `{"Field": 123}`, string(connection.GetLastMessage())) 56 } 57 58 func TestMessageCustomSendNull(t *testing.T) { 59 connection := StartConnectionMock() 60 defer connection.Close() 61 62 sender := &senderNATS{ 63 connection: connection, 64 codec: communication.NewCodecJSON(), 65 } 66 67 err := sender.Send(&customMessageProducer{}) 68 assert.NoError(t, err) 69 assert.JSONEq(t, `null`, string(connection.GetLastMessage())) 70 } 71 72 type customMessageConsumer struct { 73 messageReceived chan interface{} 74 } 75 76 func (consumer *customMessageConsumer) GetMessageEndpoint() (communication.MessageEndpoint, error) { 77 return communication.MessageEndpoint("custom-message"), nil 78 } 79 80 func (consumer *customMessageConsumer) NewMessage() (messagePtr interface{}) { 81 return &customMessage{} 82 } 83 84 func (consumer *customMessageConsumer) Consume(messagePtr interface{}) error { 85 consumer.messageReceived <- messagePtr 86 return nil 87 } 88 89 func TestMessageCustomReceive(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 := &customMessageConsumer{messageReceived: make(chan interface{})} 100 err := receiver.Receive(consumer) 101 assert.NoError(t, err) 102 103 connection.Publish("custom-message", []byte(`{"Field":123}`)) 104 message, err := connection.MessageWait(consumer.messageReceived) 105 assert.NoError(t, err) 106 assert.Exactly(t, customMessage{123}, *message.(*customMessage)) 107 }