From eec6b33fd6fb628db4b5776c81edfa07bb095d4f Mon Sep 17 00:00:00 2001 From: new-dream <111836360+new-dream@users.noreply.github.com> Date: Wed, 30 Aug 2023 22:53:06 +0800 Subject: [PATCH] test cases Signed-off-by: new-dream <111836360+new-dream@users.noreply.github.com> --- server/storage/backend/tx_buffer_test.go | 71 ++++++++++++++++++++---- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/server/storage/backend/tx_buffer_test.go b/server/storage/backend/tx_buffer_test.go index 452b9e2e284b..745b2a7ec9d5 100644 --- a/server/storage/backend/tx_buffer_test.go +++ b/server/storage/backend/tx_buffer_test.go @@ -21,21 +21,72 @@ import ( "github.com/stretchr/testify/assert" ) -func Test_bucketBuffer_CopyUsed(t *testing.T) { +func Test_bucketBuffer_CopyUsed_After_Add(t *testing.T) { bb := &bucketBuffer{buf: make([]kv, 10), used: 0} - for i := 0; i < 10; i++ { + for i := 0; i < 20; i++ { k := fmt.Sprintf("key%d", i) v := fmt.Sprintf("val%d", i) bb.add([]byte(k), []byte(v)) + bbCopy := bb.CopyUsed() + assert.Equal(t, bb.used, bbCopy.used) + assert.Equal(t, bbCopy.used, len(bbCopy.buf)) + assert.GreaterOrEqual(t, len(bb.buf), len(bbCopy.buf)) } - bbCopy := bb.CopyUsed() - assert.Equal(t, bb.used, bbCopy.used) - assert.Equal(t, bbCopy.used, len(bbCopy.buf)) } -func Test_bucketBuffer_CopyUsed_Panic(t *testing.T) { - bb := &bucketBuffer{buf: make([]kv, 10), used: 20} - assert.Panicsf(t, func() { - bb.CopyUsed() - }, "expected panic when used bigger than the length of buf") +func Test_bucketBuffer_CopyUsed(t *testing.T) { + tests := []struct { + name string + bufLen int + used int + wantPanic bool + wantUsed int + wantBufLen int + }{ + { + name: "used is 0", + bufLen: 10, + used: 0, + wantPanic: false, + wantUsed: 0, + wantBufLen: 0, + }, + { + name: "used is greater than 0 and less than len(buf)", + bufLen: 10, + used: 5, + wantPanic: false, + wantUsed: 5, + wantBufLen: 5, + }, + { + name: "used is equal to len(buf)", + bufLen: 10, + used: 10, + wantPanic: false, + wantUsed: 10, + wantBufLen: 10, + }, + { + name: "used is greater than len(buf)", + bufLen: 10, + used: 11, + wantPanic: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + bb := &bucketBuffer{buf: make([]kv, tt.bufLen), used: tt.used} + if tt.wantPanic { + assert.Panicsf(t, func() { + bb.CopyUsed() + }, "expected panic when used (%d) and the length of buf (%d)", tt.used, tt.bufLen) + } else { + bbCopy := bb.CopyUsed() + assert.Equal(t, tt.wantUsed, bbCopy.used) + assert.Equal(t, tt.wantBufLen, len(bbCopy.buf)) + } + }) + } }