github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/msg/producer/ref_counted_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package producer 22 23 import ( 24 "sync" 25 "testing" 26 "time" 27 28 "github.com/golang/mock/gomock" 29 "github.com/stretchr/testify/require" 30 ) 31 32 func TestRefCountedMessageConsume(t *testing.T) { 33 ctrl := gomock.NewController(t) 34 defer ctrl.Finish() 35 36 mm := NewMockMessage(ctrl) 37 mm.EXPECT().Size().Return(100).AnyTimes() 38 mm.EXPECT().Finalize(Consumed) 39 40 rm := NewRefCountedMessage(mm, nil) 41 require.Equal(t, mm.Size(), int(rm.Size())) 42 require.False(t, rm.IsDroppedOrConsumed()) 43 44 rm.IncRef() 45 rm.DecRef() 46 require.True(t, rm.IsDroppedOrConsumed()) 47 48 rm.IncRef() 49 rm.DecRef() 50 require.True(t, rm.IsDroppedOrConsumed()) 51 52 rm.Drop() 53 require.True(t, rm.IsDroppedOrConsumed()) 54 } 55 56 func TestRefCountedMessageDrop(t *testing.T) { 57 ctrl := gomock.NewController(t) 58 defer ctrl.Finish() 59 60 mm := NewMockMessage(ctrl) 61 mm.EXPECT().Size().Return(100).AnyTimes() 62 mm.EXPECT().Finalize(Dropped) 63 64 rm := NewRefCountedMessage(mm, nil) 65 require.Equal(t, mm.Size(), int(rm.Size())) 66 require.False(t, rm.IsDroppedOrConsumed()) 67 68 rm.Drop() 69 require.True(t, rm.IsDroppedOrConsumed()) 70 71 rm.IncRef() 72 rm.DecRef() 73 require.True(t, rm.IsDroppedOrConsumed()) 74 75 rm.Drop() 76 require.True(t, rm.IsDroppedOrConsumed()) 77 } 78 79 func TestRefCountedMessageBytesReadBlocking(t *testing.T) { 80 ctrl := gomock.NewController(t) 81 defer ctrl.Finish() 82 83 mm := NewMockMessage(ctrl) 84 mockBytes := []byte("foo") 85 mm.EXPECT().Size().Return(3) 86 mm.EXPECT().Bytes().Return(mockBytes) 87 88 rm := NewRefCountedMessage(mm, nil) 89 rm.IncReads() 90 b := rm.Bytes() 91 require.Equal(t, mockBytes, b) 92 require.False(t, rm.IsDroppedOrConsumed()) 93 94 doneCh := make(chan struct{}) 95 go func() { 96 mm.EXPECT().Finalize(Dropped) 97 rm.Drop() 98 close(doneCh) 99 }() 100 101 select { 102 case <-doneCh: 103 require.FailNow(t, "not expected") 104 case <-time.After(time.Second): 105 } 106 rm.DecReads() 107 <-doneCh 108 require.True(t, rm.IsDroppedOrConsumed()) 109 } 110 111 func TestRefCountedMessageDecPanic(t *testing.T) { 112 ctrl := gomock.NewController(t) 113 defer ctrl.Finish() 114 115 mm := NewMockMessage(ctrl) 116 mm.EXPECT().Size().Return(0) 117 rm := NewRefCountedMessage(mm, nil) 118 require.Panics(t, rm.DecRef) 119 } 120 121 func TestRefCountedMessageFilter(t *testing.T) { 122 ctrl := gomock.NewController(t) 123 defer ctrl.Finish() 124 125 var called int 126 filter := func(m Message) bool { 127 called++ 128 return m.Shard() == 0 129 } 130 131 sizeFilter := func(m Message) bool { 132 called++ 133 return m.Size() == 0 134 } 135 136 mm := NewMockMessage(ctrl) 137 mm.EXPECT().Size().Return(0) 138 rm := NewRefCountedMessage(mm, nil) 139 140 mm.EXPECT().Shard().Return(uint32(0)) 141 require.True(t, rm.Accept([]FilterFunc{filter})) 142 143 mm.EXPECT().Shard().Return(uint32(1)) 144 require.False(t, rm.Accept([]FilterFunc{filter})) 145 146 mm.EXPECT().Shard().Return(uint32(0)) 147 mm.EXPECT().Size().Return(0) 148 require.True(t, rm.Accept([]FilterFunc{filter, sizeFilter})) 149 150 require.False(t, rm.Accept([]FilterFunc{})) 151 } 152 153 func TestRefCountedMessageOnDropFn(t *testing.T) { 154 ctrl := gomock.NewController(t) 155 defer ctrl.Finish() 156 157 mm := NewMockMessage(ctrl) 158 mm.EXPECT().Size().Return(0) 159 mm.EXPECT().Finalize(Dropped) 160 161 var called int 162 fn := func(rm *RefCountedMessage) { 163 called++ 164 } 165 166 rm := NewRefCountedMessage(mm, fn) 167 require.True(t, rm.Drop()) 168 require.Equal(t, 1, called) 169 170 require.False(t, rm.Drop()) 171 } 172 173 func TestRefCountedMessageNoBlocking(t *testing.T) { 174 ctrl := gomock.NewController(t) 175 defer ctrl.Finish() 176 177 mm := NewMockMessage(ctrl) 178 mm.EXPECT().Size().Return(0).AnyTimes() 179 for i := 0; i < 10000; i++ { 180 rm := NewRefCountedMessage(mm, nil) 181 var wg sync.WaitGroup 182 wg.Add(2) 183 go func() { 184 rm.IncReads() 185 rm.IsDroppedOrConsumed() 186 rm.DecReads() 187 wg.Done() 188 }() 189 go func() { 190 mm.EXPECT().Finalize(Dropped) 191 rm.Drop() 192 wg.Done() 193 }() 194 wg.Wait() 195 } 196 }