github.com/sandwich-go/boost@v1.3.29/xencoding/pbjson/pbjson_test.go (about)

     1  package pbjson
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"github.com/sandwich-go/boost/xencoding"
     7  	"sync"
     8  	"testing"
     9  
    10  	"github.com/sandwich-go/boost/xencoding/protobuf/test_perf"
    11  )
    12  
    13  func TestEmitUnpopulated(t *testing.T) {
    14  	p := &test_perf.Buffer{}
    15  	EmitUnpopulated(true)
    16  	marshalledBytes, err := Codec.Marshal(context.Background(), p)
    17  	if err != nil {
    18  		t.Errorf("codec.Marshal(_) returned an error:%v", err)
    19  	}
    20  	if string(marshalledBytes) != `{"body":null}` {
    21  		t.Errorf("codec.Marshal(_) returned empty on emit unpopulated")
    22  	}
    23  }
    24  
    25  func marshalAndUnmarshal(t *testing.T, codec xencoding.Codec, expectedBody []byte) {
    26  	p := &test_perf.Buffer{}
    27  	p.Body = expectedBody
    28  
    29  	marshalledBytes, err := codec.Marshal(context.Background(), p)
    30  	if err != nil {
    31  		t.Errorf("codec.Marshal(_) returned an error:%v", err)
    32  	}
    33  	if err = codec.Unmarshal(context.Background(), marshalledBytes, p); err != nil {
    34  		t.Errorf("codec.Unmarshal(_) returned an error:%v", err)
    35  	}
    36  
    37  	if !bytes.Equal(p.GetBody(), expectedBody) {
    38  		t.Errorf("Unexpected body; got %v; want %v", p.GetBody(), expectedBody)
    39  	}
    40  }
    41  
    42  func TestBasicJsonCodecMarshalAndUnmarshal(t *testing.T) {
    43  	marshalAndUnmarshal(t, codec{}, []byte{1, 2, 3})
    44  }
    45  
    46  // Try to catch possible race conditions around use of pools
    47  func TestConcurrentUsage(t *testing.T) {
    48  	const (
    49  		numGoRoutines     = 100
    50  		numMarshUnmarshal = 1000
    51  	)
    52  
    53  	// small, arbitrary byte slices
    54  	protoBodies := [][]byte{
    55  		[]byte("one"),
    56  		[]byte("two"),
    57  		[]byte("three"),
    58  		[]byte("four"),
    59  		[]byte("five"),
    60  	}
    61  
    62  	var wg sync.WaitGroup
    63  	codec := codec{}
    64  
    65  	for i := 0; i < numGoRoutines; i++ {
    66  		wg.Add(1)
    67  		go func() {
    68  			defer wg.Done()
    69  			for k := 0; k < numMarshUnmarshal; k++ {
    70  				marshalAndUnmarshal(t, codec, protoBodies[k%len(protoBodies)])
    71  			}
    72  		}()
    73  	}
    74  
    75  	wg.Wait()
    76  }
    77  
    78  // TestStaggeredMarshalAndUnmarshalUsingSamePool tries to catch potential errors in which slices get
    79  // stomped on during reuse of a proto.Buffer.
    80  func TestStaggeredMarshalAndUnmarshalUsingSamePool(t *testing.T) {
    81  	codec1 := codec{}
    82  	codec2 := codec{}
    83  
    84  	expectedBody1 := []byte{1, 2, 3}
    85  	expectedBody2 := []byte{4, 5, 6}
    86  
    87  	proto1 := test_perf.Buffer{Body: expectedBody1}
    88  	proto2 := test_perf.Buffer{Body: expectedBody2}
    89  
    90  	var m1, m2 []byte
    91  	var err error
    92  
    93  	if m1, err = codec1.Marshal(context.Background(), &proto1); err != nil {
    94  		t.Errorf("codec.Marshal proto1 failed")
    95  	}
    96  
    97  	if m2, err = codec2.Marshal(context.Background(), &proto2); err != nil {
    98  		t.Errorf("codec.Marshal proto2 failed")
    99  	}
   100  
   101  	if err = codec1.Unmarshal(context.Background(), m1, &proto1); err != nil {
   102  		t.Errorf("codec.Unmarshal(%v) failed", m1)
   103  	}
   104  
   105  	if err = codec2.Unmarshal(context.Background(), m2, &proto2); err != nil {
   106  		t.Errorf("codec.Unmarshal(%v) failed", m2)
   107  	}
   108  
   109  	b1 := proto1.GetBody()
   110  	b2 := proto2.GetBody()
   111  
   112  	for i, v := range b1 {
   113  		if expectedBody1[i] != v {
   114  			t.Errorf("expected %v at index %v but got %v", i, expectedBody1[i], v)
   115  		}
   116  	}
   117  
   118  	for i, v := range b2 {
   119  		if expectedBody2[i] != v {
   120  			t.Errorf("expected %v at index %v but got %v", i, expectedBody2[i], v)
   121  		}
   122  	}
   123  }