github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/aeron/command/flyweights_test.go (about) 1 // Copyright 2022 Steven Stern 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package command 16 17 import ( 18 "github.com/lirm/aeron-go/aeron/atomic" 19 "github.com/stretchr/testify/suite" 20 "testing" 21 ) 22 23 type FlyweightsTestSuite struct { 24 suite.Suite 25 26 byteSlice []byte 27 buffer *atomic.Buffer 28 flyweight CounterMessage 29 } 30 31 func (s *FlyweightsTestSuite) SetupTest() { 32 s.byteSlice = make([]byte, 128) 33 s.buffer = atomic.MakeBuffer(s.byteSlice) 34 } 35 36 func (s *FlyweightsTestSuite) TestCounterMessageKeyBuffer() { 37 offset := 24 38 for i := 0; i <= offset; i++ { 39 s.byteSlice[i] = byte(15) 40 } 41 s.flyweight.Wrap(s.buffer, offset) 42 43 newBuffer := atomic.MakeBuffer(make([]byte, 16)) 44 s.flyweight.CopyKeyBuffer(newBuffer, 4, 8) 45 46 s.Assert().EqualValues(8, s.flyweight.key.Length()) 47 } 48 49 func (s *FlyweightsTestSuite) TestCounterMessageLabelBuffer() { 50 offset := 40 51 for i := 0; i <= offset; i++ { 52 s.byteSlice[i] = byte(255) 53 } 54 s.flyweight.Wrap(s.buffer, offset) 55 56 keyBuffer := atomic.MakeBuffer(make([]byte, 16)) 57 keyCopySize := int32(9) 58 s.flyweight.CopyKeyBuffer(keyBuffer, 6, keyCopySize) 59 60 labelBuffer := atomic.MakeBuffer(make([]byte, 32)) 61 labelCopySize := int32(21) 62 s.flyweight.CopyLabelBuffer(labelBuffer, 2, labelCopySize) 63 64 s.Assert().EqualValues(9, s.flyweight.key.Length()) 65 s.Assert().EqualValues(21, s.flyweight.label.Length()) 66 // Expected is sizes of: long, long, int, length field, keyCopySize 67 s.Assert().EqualValues(8+8+4+4+keyCopySize, s.flyweight.getLabelOffset()) 68 // Above, plus label length and size 69 s.Assert().EqualValues(8+8+4+4+keyCopySize+4+labelCopySize, s.flyweight.Size()) 70 } 71 72 func (s *FlyweightsTestSuite) TestCounterMessageLabelString() { 73 offset := 40 74 for i := 0; i <= offset; i++ { 75 s.byteSlice[i] = byte(255) 76 } 77 s.flyweight.Wrap(s.buffer, offset) 78 79 label := "Steve loves Aeron" 80 s.flyweight.CopyLabelString(label) 81 82 s.Assert().EqualValues(0, s.flyweight.key.Length()) 83 s.Assert().EqualValues(len(label), s.flyweight.label.Length()) 84 // Expected is sizes of: long, long, int, length field (key is empty) 85 s.Assert().EqualValues(8+8+4+4, s.flyweight.getLabelOffset()) 86 // Above, plus label length and size 87 s.Assert().EqualValues(8+8+4+4+4+len(label), s.flyweight.Size()) 88 } 89 90 func TestFlyweights(t *testing.T) { 91 suite.Run(t, new(FlyweightsTestSuite)) 92 }