github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/exp/kvutil/sharding_protobuf_test.go (about)

     1  package kvutil
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  	"google.golang.org/protobuf/proto"
    10  
    11  	"github.com/jxskiss/gopkg/v2/easy"
    12  	"github.com/jxskiss/gopkg/v2/exp/kvutil/sharding_protobuf_test"
    13  )
    14  
    15  type TestProtoShardingModel struct {
    16  	Entity *sharding_protobuf_test.TestShardingModel
    17  }
    18  
    19  func (m *TestProtoShardingModel) MarshalBinary() (data []byte, err error) {
    20  	return proto.Marshal(m.Entity)
    21  }
    22  
    23  func (m *TestProtoShardingModel) UnmarshalBinary(data []byte) error {
    24  	entity := &sharding_protobuf_test.TestShardingModel{}
    25  	err := proto.Unmarshal(data, entity)
    26  	if err != nil {
    27  		return err
    28  	}
    29  	m.Entity = entity
    30  	return nil
    31  }
    32  
    33  func (m *TestProtoShardingModel) GetShardingData() (ShardingData, bool) {
    34  	if m.Entity == nil || m.Entity.ShardData == nil {
    35  		return ShardingData{}, false
    36  	}
    37  	shardData := ShardingData{
    38  		TotalNum: m.Entity.ShardData.TotalNum,
    39  		ShardNum: m.Entity.ShardData.ShardNum,
    40  		Digest:   m.Entity.ShardData.Digest,
    41  		Data:     m.Entity.ShardData.Data,
    42  	}
    43  	return shardData, true
    44  }
    45  
    46  func (m *TestProtoShardingModel) SetShardingData(data ShardingData) {
    47  	if m.Entity == nil {
    48  		m.Entity = &sharding_protobuf_test.TestShardingModel{}
    49  	}
    50  	m.Entity.ShardData = &sharding_protobuf_test.ShardingData{
    51  		TotalNum: data.TotalNum,
    52  		ShardNum: data.ShardNum,
    53  		Digest:   data.Digest,
    54  		Data:     data.Data,
    55  	}
    56  }
    57  
    58  var (
    59  	testProtobufShardingModelList = []*TestProtoShardingModel{
    60  		{
    61  			Entity: &sharding_protobuf_test.TestShardingModel{
    62  				Id:      111,
    63  				BizData: []byte("test"),
    64  			},
    65  		},
    66  		{
    67  			Entity: &sharding_protobuf_test.TestShardingModel{
    68  				Id:      112,
    69  				BizData: easy.Repeat([]byte("test "), 10),
    70  			},
    71  		},
    72  		{
    73  			Entity: &sharding_protobuf_test.TestShardingModel{
    74  				Id:      113,
    75  				BizData: easy.Repeat([]byte("test "), 50),
    76  			},
    77  		},
    78  	}
    79  )
    80  
    81  //nolint:dupl
    82  func TestShardingCache_Protobuf(t *testing.T) {
    83  	kf := KeyFactory{}
    84  	cfg := &ShardingCacheConfig[int64, *TestProtoShardingModel]{
    85  		Storage: testClientFunc("testShardingCache_Protobuf"),
    86  		IDFunc: func(model *TestProtoShardingModel) int64 {
    87  			return model.Entity.GetId()
    88  		},
    89  		KeyFunc:         kf.NewKey("testShardingCache:{id}"),
    90  		ShardingSize:    10,
    91  		MGetBatchSize:   2,
    92  		MSetBatchSize:   2,
    93  		DeleteBatchSize: 2,
    94  	}
    95  
    96  	ctx := context.Background()
    97  	sc := NewShardingCache[int64, *TestProtoShardingModel](cfg)
    98  
    99  	t.Run("Get / not found", func(t *testing.T) {
   100  		gotModel, err := sc.Get(ctx, testIntIds[0])
   101  		assert.Equal(t, ErrDataNotFound, err)
   102  		assert.Nil(t, gotModel)
   103  	})
   104  
   105  	t.Run("MGet / not found", func(t *testing.T) {
   106  		modelMap, errMap, err := sc.MGet(ctx, testIntIds)
   107  		assert.Nil(t, err)
   108  		assert.Len(t, errMap, 0)
   109  		assert.Len(t, modelMap, 0)
   110  	})
   111  
   112  	t.Run("Set", func(t *testing.T) {
   113  		clearMemoryStorage(ctx, sc.config.Storage)
   114  		stor := getMemoryStorage(ctx, sc.config.Storage)
   115  		_ = stor
   116  
   117  		err1 := sc.Set(ctx, 111, testProtobufShardingModelList[0], 0)
   118  		assert.Nil(t, err1)
   119  
   120  		err2 := sc.Set(ctx, 112, testProtobufShardingModelList[1], 0)
   121  		assert.Nil(t, err2)
   122  
   123  		err3 := sc.Set(ctx, 113, testProtobufShardingModelList[2], 0)
   124  		assert.Nil(t, err3)
   125  
   126  		got1, err1 := sc.Get(ctx, 111)
   127  		assert.Nil(t, err1)
   128  		assert.NotNil(t, got1)
   129  		assert.Nil(t, got1.Entity.ShardData)
   130  		assert.Equal(t, testProtobufShardingModelList[0].Entity.BizData, got1.Entity.BizData)
   131  
   132  		got2, err2 := sc.Get(ctx, 112)
   133  		assert.Nil(t, err2)
   134  		assert.NotNil(t, got2)
   135  		assert.Nil(t, got2.Entity.ShardData)
   136  		assert.Equal(t, testProtobufShardingModelList[1].Entity.BizData, got2.Entity.BizData)
   137  
   138  		got3, err3 := sc.Get(ctx, 113)
   139  		assert.Nil(t, err3)
   140  		assert.NotNil(t, got3)
   141  		assert.Nil(t, got3.Entity.ShardData)
   142  		assert.Equal(t, testProtobufShardingModelList[2].Entity.BizData, got3.Entity.BizData)
   143  
   144  		mgetRet, errMap, err := sc.MGet(ctx, []int64{111, 112, 113, 114})
   145  		assert.Nil(t, err)
   146  		assert.Len(t, errMap, 0)
   147  		assert.Len(t, mgetRet, 3)
   148  		assert.Equal(t, testProtobufShardingModelList[0].Entity.BizData, mgetRet[111].Entity.BizData)
   149  		assert.Equal(t, testProtobufShardingModelList[1].Entity.BizData, mgetRet[112].Entity.BizData)
   150  		assert.Equal(t, testProtobufShardingModelList[2].Entity.BizData, mgetRet[113].Entity.BizData)
   151  	})
   152  
   153  	t.Run("MSet", func(t *testing.T) {
   154  		clearMemoryStorage(ctx, sc.config.Storage)
   155  		stor := getMemoryStorage(ctx, sc.config.Storage)
   156  		_ = stor
   157  
   158  		err := sc.MSet(ctx, testProtobufShardingModelList, 0)
   159  		require.Nil(t, err)
   160  
   161  		mgetRet, errMap, err := sc.MGet(ctx, []int64{111, 112, 113, 114})
   162  		assert.Nil(t, err)
   163  		assert.Len(t, errMap, 0)
   164  		assert.Len(t, mgetRet, 3)
   165  		assert.Equal(t, testProtobufShardingModelList[0].Entity.BizData, mgetRet[111].Entity.BizData)
   166  		assert.Equal(t, testProtobufShardingModelList[1].Entity.BizData, mgetRet[112].Entity.BizData)
   167  		assert.Equal(t, testProtobufShardingModelList[2].Entity.BizData, mgetRet[113].Entity.BizData)
   168  	})
   169  
   170  	t.Run("Delete", func(t *testing.T) {
   171  		clearMemoryStorage(ctx, sc.config.Storage)
   172  		stor := getMemoryStorage(ctx, sc.config.Storage)
   173  		_ = stor
   174  
   175  		err := sc.MSet(ctx, testProtobufShardingModelList, 0)
   176  		require.Nil(t, err)
   177  
   178  		err = sc.Delete(ctx, false, 111, 112)
   179  		require.Nil(t, err)
   180  
   181  		got1, err1 := sc.Get(ctx, 111)
   182  		assert.Equal(t, ErrDataNotFound, err1)
   183  		assert.Nil(t, got1)
   184  
   185  		got2, err2 := sc.Get(ctx, 112)
   186  		assert.Equal(t, ErrDataNotFound, err2)
   187  		assert.Nil(t, got2)
   188  
   189  		err = sc.Delete(ctx, true, 113)
   190  		require.Nil(t, err)
   191  
   192  		got3, err3 := sc.Get(ctx, 113)
   193  		assert.Equal(t, ErrDataNotFound, err3)
   194  		assert.Nil(t, got3)
   195  
   196  		assert.Nil(t, stor.data[sc.config.KeyFunc(111)])
   197  		assert.Nil(t, stor.data[sc.config.KeyFunc(112)])
   198  		assert.NotNil(t, stor.data[GetShardKey(sc.config.KeyFunc(112), 1)])
   199  		assert.Nil(t, stor.data[sc.config.KeyFunc(113)])
   200  		assert.Nil(t, stor.data[GetShardKey(sc.config.KeyFunc(113), 1)])
   201  		assert.Nil(t, stor.data[GetShardKey(sc.config.KeyFunc(113), 2)])
   202  		assert.Nil(t, stor.data[GetShardKey(sc.config.KeyFunc(113), 3)])
   203  	})
   204  
   205  }