github.com/sandwich-go/boost@v1.3.29/xencoding/json/json_test.go (about)

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