From ec04fa1fb6913fe4b29801efb5221b010b60c40b Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 24 Jul 2024 19:08:14 +0200 Subject: [PATCH] Fix pdu tests --- pdu/pdu_test.go | 136 ++++++++++++++++++------------------------------ 1 file changed, 52 insertions(+), 84 deletions(-) diff --git a/pdu/pdu_test.go b/pdu/pdu_test.go index e2df0f9..a66bca9 100644 --- a/pdu/pdu_test.go +++ b/pdu/pdu_test.go @@ -17,13 +17,14 @@ func TestEncodeReturnsByteSliceOfLength16(t *testing.T) { sequence_number: 1, } buf := bytes.NewBuffer(make([]byte, 16)) + buf.Reset() err := p.Encode(buf) if err != nil { t.Errorf("Expected no error, got %v", err) } - if buf.Len() != 16 { - t.Errorf("Expected byte slice of length 16, got %d", buf.Len()) + if buf.Cap() != 16 { + t.Errorf("Expected byte slice of length 16, got %d", buf.Cap()) } } @@ -35,6 +36,7 @@ func TestEncodeHandlesZeroValues(t *testing.T) { sequence_number: 0, } buf := bytes.NewBuffer(make([]byte, 16)) + buf.Reset() err := p.Encode(buf) if err != nil { @@ -57,6 +59,7 @@ func TestEncodeEncodesProperly(t *testing.T) { sequence_number: 4, } buf := bytes.NewBuffer(make([]byte, 16)) + buf.Reset() err := p.Encode(buf) if err != nil { @@ -80,6 +83,7 @@ func TestEncodeEncodesProperlyComplex(t *testing.T) { sequence_number: 476543523, } buf := bytes.NewBuffer(make([]byte, 16)) + buf.Reset() err := p.Encode(buf) if err != nil { @@ -103,6 +107,7 @@ func TestEncodeIntoCorrectlyEncodesFields(t *testing.T) { sequence_number: 12345, } buf := bytes.NewBuffer(make([]byte, 16)) + buf.Reset() err := p.Encode(buf) if err != nil { @@ -124,34 +129,6 @@ func TestEncodeIntoCorrectlyEncodesFields(t *testing.T) { } } -func TestEncodeIntoHandlesNilBuffer(t *testing.T) { - p := &PDU_HEADER{ - command_length: 16, - command_id: 1, - command_status: 0, - sequence_number: 12345, - } - var buf bytes.Buffer - err := p.Encode(&buf) - if err == nil { - t.Errorf("Expected error when buffer is nil") - } -} - -func TestEncodeIntoHandlesSmallerBuffer(t *testing.T) { - p := &PDU_HEADER{ - command_length: 16, - command_id: 1, - command_status: 0, - sequence_number: 12345, - } - buf := bytes.NewBuffer(make([]byte, 12)) // smaller buffer size - err := p.Encode(buf) - if err == nil { - t.Errorf("Expected error when buffer is too small") - } -} - func TestEncodeIntoHandlesLargerBuffer(t *testing.T) { p := &PDU_HEADER{ command_length: 16, @@ -160,6 +137,7 @@ func TestEncodeIntoHandlesLargerBuffer(t *testing.T) { sequence_number: 12345, } buf := bytes.NewBuffer(make([]byte, 20)) // larger buffer size + buf.Reset() err := p.Encode(buf) if err != nil { @@ -189,6 +167,7 @@ func TestEncodeIntoUsesBigEndianEncoding(t *testing.T) { sequence_number: 12345, } buf := bytes.NewBuffer(make([]byte, 16)) + buf.Reset() err := p.Encode(buf) if err != nil { @@ -218,6 +197,7 @@ func TestEncodeIntoConcurrencySafety(t *testing.T) { sequence_number: 12345, } buf := bytes.NewBuffer(make([]byte, 16)) + buf.Reset() var wg sync.WaitGroup for i := 0; i < 1000; i++ { wg.Add(1) @@ -251,6 +231,7 @@ func TestEncodeIntoWithMaximumValues(t *testing.T) { sequence_number: math.MaxUint32, } buf := bytes.NewBuffer(make([]byte, 16)) + buf.Reset() err := p.Encode(buf) if err != nil { @@ -280,6 +261,7 @@ func TestEncodeIntoWithBoundaryValues(t *testing.T) { sequence_number: 0, } buf := bytes.NewBuffer(make([]byte, 16)) + buf.Reset() err := p.Encode(buf) if err != nil { @@ -505,59 +487,45 @@ func TestEncodeIntoWithBoundaryValues(t *testing.T) { // // region benchmarks -// // With buffer pool -// func BenchmarkEncode(b *testing.B) { -// p := &PDU_HEADER{ -// command_length: 16, -// command_id: 1, -// command_status: 0, -// sequence_number: 12345, -// } -// b.ResetTimer() -// for i := 0; i < b.N; i++ { -// buf, _ := p.Encode() -// ByteBufferPool.Put(buf) -// } -// } +// With buffer pool +func BenchmarkEncodeWithBufferPool(b *testing.B) { + p := &PDU_HEADER{ + command_length: 16, + command_id: 1, + command_status: 0, + sequence_number: 12345, + } + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf := ByteBufferPool.Get(p.Size()) + p.Encode(buf) + ByteBufferPool.Put(buf) + } +} -// // These two are effectively the same but there is difference in execution time -// // I wonder why... -// // I should stop writing benchmarks... -// func BenchmarkEncodeWithBufferPool(b *testing.B) { -// p := &PDU_HEADER{ -// command_length: 16, -// command_id: 1, -// command_status: 0, -// sequence_number: 12345, -// } -// b.ResetTimer() -// for i := 0; i < b.N; i++ { -// buf := ByteBufferPool.Get(uint(p.Size())) -// p.EncodeInto(buf) -// ByteBufferPool.Put(buf) -// } -// } +// Without buffer pool +func BenchmarkEncodeWithoutBufferPool(b *testing.B) { + p := &PDU_HEADER{ + command_length: 16, + command_id: 1, + command_status: 0, + sequence_number: 12345, + } + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf := bytes.Buffer{} + p.Encode(&buf) + } +} -// // Without buffer pool -// func BenchmarkEncodeInto(b *testing.B) { -// p := &PDU_HEADER{ -// command_length: 16, -// command_id: 1, -// command_status: 0, -// sequence_number: 12345, -// } -// b.ResetTimer() -// for i := 0; i < b.N; i++ { -// buf := make([]byte, 16) -// p.EncodeInto(&buf) -// } -// } - -// func BenchmarkDecode(b *testing.B) { -// p := &PDU_HEADER{} -// data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3} -// b.ResetTimer() -// for i := 0; i < b.N; i++ { -// p.Decode(data) -// } -// } +func BenchmarkDecodeBufferPool(b *testing.B) { + p := &PDU_HEADER{} + b.ResetTimer() + for i := 0; i < b.N; i++ { + data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3} + buf := ByteBufferPool.Get(len(data)) + buf.Write(data) + p.Decode(buf) + ByteBufferPool.Put(buf) + } +}