github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/msg_test.go (about)

     1  package rony_test
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  
     7  	"github.com/goccy/go-json"
     8  	"github.com/ronaksoft/rony"
     9  	"github.com/ronaksoft/rony/internal/testEnv/pb/service"
    10  	"github.com/ronaksoft/rony/tools"
    11  	. "github.com/smartystreets/goconvey/convey"
    12  	"google.golang.org/protobuf/encoding/protojson"
    13  	"google.golang.org/protobuf/proto"
    14  )
    15  
    16  /*
    17     Creation Time: 2020 - Dec - 16
    18     Created by:  (ehsan)
    19     Maintainers:
    20        1.  Ehsan N. Moosa (E2)
    21     Auditor: Ehsan N. Moosa (E2)
    22     Copyright Ronak Software Group 2020
    23  */
    24  
    25  func TestMessageEnvelope_Clone(t *testing.T) {
    26  	Convey("Clone (MessageEnvelope)", t, func(c C) {
    27  		src := &rony.MessageEnvelope{
    28  			RequestID:   tools.RandomUint64(0),
    29  			Constructor: tools.RandomUint64(0),
    30  			Header: []*rony.KeyValue{
    31  				{
    32  					Key:   "Key1",
    33  					Value: "Value1",
    34  				},
    35  				{
    36  					Key:   "Key2",
    37  					Value: "Value2",
    38  				},
    39  			},
    40  			Auth: tools.StrToByte(tools.RandomID(10)),
    41  		}
    42  
    43  		wg := sync.WaitGroup{}
    44  		for i := 0; i < 100; i++ {
    45  			wg.Add(1)
    46  			go func() {
    47  				dst := src.Clone()
    48  				c.So(dst, ShouldResemble, src)
    49  				wg.Done()
    50  			}()
    51  		}
    52  		wg.Wait()
    53  
    54  		src = &rony.MessageEnvelope{
    55  			RequestID:   tools.RandomUint64(0),
    56  			Constructor: tools.RandomUint64(0),
    57  			Auth:        tools.StrToByte(tools.RandomID(10)),
    58  		}
    59  
    60  		for i := 0; i < 100; i++ {
    61  			wg.Add(1)
    62  			go func() {
    63  				dst := src.Clone()
    64  				c.So(proto.Equal(dst, src), ShouldBeTrue)
    65  				wg.Done()
    66  			}()
    67  		}
    68  		wg.Wait()
    69  	})
    70  }
    71  
    72  func TestMessageEnvelope_Get(t *testing.T) {
    73  	Convey("Get (MessageEnvelope)", t, func(c C) {
    74  		s := &rony.MessageEnvelope{
    75  			Constructor: tools.RandomUint64(0),
    76  			RequestID:   tools.RandomUint64(0),
    77  			Message:     tools.S2B(tools.RandomID(1024)),
    78  			Auth:        nil,
    79  			Header: []*rony.KeyValue{
    80  				{Key: "Key1", Value: "V1"},
    81  				{Key: "Key2", Value: "V2"},
    82  			},
    83  		}
    84  		sb, _ := s.Marshal()
    85  		wg := &sync.WaitGroup{}
    86  		for i := 0; i < 100; i++ {
    87  			wg.Add(1)
    88  			go func() {
    89  				x := rony.PoolMessageEnvelope.Get()
    90  				_ = x.Unmarshal(sb)
    91  				c.So(proto.Equal(x, s), ShouldBeTrue)
    92  				rony.PoolMessageEnvelope.Put(x)
    93  				wg.Done()
    94  			}()
    95  		}
    96  		wg.Wait()
    97  	})
    98  }
    99  
   100  func TestMessageEnvelopeJSON(t *testing.T) {
   101  	Convey("Test MessageEnvelopeJSON", t, func(c C) {
   102  		req := &service.EchoRequest{
   103  			Int:       100,
   104  			Timestamp: 123,
   105  		}
   106  		bb, err := protojson.Marshal(req)
   107  		c.So(err, ShouldBeNil)
   108  		c.Println("ProtoJSON Marshal: ", string(bb))
   109  
   110  		req2 := &service.EchoRequest{}
   111  		err = protojson.Unmarshal(bb, req2)
   112  		c.So(err, ShouldBeNil)
   113  		c.So(req, ShouldResemble, req2)
   114  
   115  		mej := rony.MessageEnvelopeJSON{
   116  			RequestID:   tools.RandomUint64(0),
   117  			Header:      nil,
   118  			Constructor: "EchoRequest",
   119  			Message:     json.RawMessage(`{"Int":"100","Timestamp":"123"}`),
   120  		}
   121  
   122  		mejBytes, err := json.Marshal(mej)
   123  		c.So(err, ShouldBeNil)
   124  		c.Println("JSON Marshal: ", string(mejBytes))
   125  
   126  		req3 := &service.EchoRequest{}
   127  		c.Println("Message:", string(mej.Message))
   128  		err = req3.UnmarshalJSON(mej.Message)
   129  		c.So(err, ShouldBeNil)
   130  		c.So(req3, ShouldResemble, req)
   131  
   132  		me := &rony.MessageEnvelope{}
   133  		err = json.Unmarshal(mejBytes, me)
   134  		c.So(err, ShouldBeNil)
   135  
   136  		m, err := me.Unwrap()
   137  		c.So(err, ShouldBeNil)
   138  		c.So(m.Interface(), ShouldResemble, req)
   139  	})
   140  }
   141  
   142  func BenchmarkMessageEnvelope_GetWithPool(b *testing.B) {
   143  	s := &rony.MessageEnvelope{
   144  		Constructor: tools.RandomUint64(0),
   145  		RequestID:   tools.RandomUint64(0),
   146  		Message:     tools.S2B(tools.RandomID(1024)),
   147  		Auth:        nil,
   148  		Header: []*rony.KeyValue{
   149  			{Key: "Key1", Value: "V1"},
   150  			{Key: "Key2", Value: "V2"},
   151  		},
   152  	}
   153  	sb, _ := s.Marshal()
   154  	b.ResetTimer()
   155  	b.ReportAllocs()
   156  	b.RunParallel(func(pb *testing.PB) {
   157  		for pb.Next() {
   158  			x := rony.PoolMessageEnvelope.Get()
   159  			_ = x.Unmarshal(sb)
   160  			rony.PoolMessageEnvelope.Put(x)
   161  		}
   162  	})
   163  }
   164  
   165  func BenchmarkMessageEnvelope_Get(b *testing.B) {
   166  	s := &rony.MessageEnvelope{
   167  		Constructor: tools.RandomUint64(0),
   168  		RequestID:   tools.RandomUint64(0),
   169  		Message:     tools.S2B(tools.RandomID(1024)),
   170  		Auth:        nil,
   171  		Header: []*rony.KeyValue{
   172  			{Key: "Key1", Value: "V1"},
   173  			{Key: "Key2", Value: "V2"},
   174  		},
   175  	}
   176  	sb, _ := s.Marshal()
   177  	b.ResetTimer()
   178  	b.ReportAllocs()
   179  	b.RunParallel(func(pb *testing.PB) {
   180  		for pb.Next() {
   181  			x := &rony.MessageEnvelope{}
   182  			_ = x.Unmarshal(sb)
   183  		}
   184  	})
   185  }