github.com/m3db/m3@v1.5.0/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  	mm := NewMockMessage(ctrl)
   132  	mm.EXPECT().Size().Return(0)
   133  	rm := NewRefCountedMessage(mm, nil)
   134  
   135  	mm.EXPECT().Shard().Return(uint32(0))
   136  	require.True(t, rm.Accept(filter))
   137  
   138  	mm.EXPECT().Shard().Return(uint32(1))
   139  	require.False(t, rm.Accept(filter))
   140  }
   141  
   142  func TestRefCountedMessageOnDropFn(t *testing.T) {
   143  	ctrl := gomock.NewController(t)
   144  	defer ctrl.Finish()
   145  
   146  	mm := NewMockMessage(ctrl)
   147  	mm.EXPECT().Size().Return(0)
   148  	mm.EXPECT().Finalize(Dropped)
   149  
   150  	var called int
   151  	fn := func(rm *RefCountedMessage) {
   152  		called++
   153  	}
   154  
   155  	rm := NewRefCountedMessage(mm, fn)
   156  	require.True(t, rm.Drop())
   157  	require.Equal(t, 1, called)
   158  
   159  	require.False(t, rm.Drop())
   160  }
   161  
   162  func TestRefCountedMessageNoBlocking(t *testing.T) {
   163  	ctrl := gomock.NewController(t)
   164  	defer ctrl.Finish()
   165  
   166  	mm := NewMockMessage(ctrl)
   167  	mm.EXPECT().Size().Return(0).AnyTimes()
   168  	for i := 0; i < 10000; i++ {
   169  		rm := NewRefCountedMessage(mm, nil)
   170  		var wg sync.WaitGroup
   171  		wg.Add(2)
   172  		go func() {
   173  			rm.IncReads()
   174  			rm.IsDroppedOrConsumed()
   175  			rm.DecReads()
   176  			wg.Done()
   177  		}()
   178  		go func() {
   179  			mm.EXPECT().Finalize(Dropped)
   180  			rm.Drop()
   181  			wg.Done()
   182  		}()
   183  		wg.Wait()
   184  	}
   185  }