github.com/bhojpur/cache@v0.0.4/pkg/engine/ristretto/ring_test.go (about)

     1  package ristretto
     2  
     3  // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved.
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  import (
    24  	"sync"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  type testConsumer struct {
    31  	push func([]uint64)
    32  	save bool
    33  }
    34  
    35  func (c *testConsumer) Push(items []uint64) bool {
    36  	if c.save {
    37  		c.push(items)
    38  		return true
    39  	}
    40  	return false
    41  }
    42  
    43  func TestRingDrain(t *testing.T) {
    44  	drains := 0
    45  	r := newRingBuffer(&testConsumer{
    46  		push: func(items []uint64) {
    47  			drains++
    48  		},
    49  		save: true,
    50  	}, 1)
    51  	for i := 0; i < 100; i++ {
    52  		r.Push(uint64(i))
    53  	}
    54  	require.Equal(t, 100, drains, "buffers shouldn't be dropped with BufferItems == 1")
    55  }
    56  
    57  func TestRingReset(t *testing.T) {
    58  	drains := 0
    59  	r := newRingBuffer(&testConsumer{
    60  		push: func(items []uint64) {
    61  			drains++
    62  		},
    63  		save: false,
    64  	}, 4)
    65  	for i := 0; i < 100; i++ {
    66  		r.Push(uint64(i))
    67  	}
    68  	require.Equal(t, 0, drains, "testConsumer shouldn't be draining")
    69  }
    70  
    71  func TestRingConsumer(t *testing.T) {
    72  	mu := &sync.Mutex{}
    73  	drainItems := make(map[uint64]struct{})
    74  	r := newRingBuffer(&testConsumer{
    75  		push: func(items []uint64) {
    76  			mu.Lock()
    77  			defer mu.Unlock()
    78  			for i := range items {
    79  				drainItems[items[i]] = struct{}{}
    80  			}
    81  		},
    82  		save: true,
    83  	}, 4)
    84  	for i := 0; i < 100; i++ {
    85  		r.Push(uint64(i))
    86  	}
    87  	l := len(drainItems)
    88  	require.NotEqual(t, 0, l)
    89  	require.True(t, l <= 100)
    90  }