github.com/koko1123/flow-go-1@v0.29.6/engine/consensus/sealing/counters/monotonous_counter_test.go (about)

     1  package counters
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/koko1123/flow-go-1/utils/unittest"
     9  )
    10  
    11  func TestSet(t *testing.T) {
    12  	counter := NewMonotonousCounter(3)
    13  	require.True(t, counter.Set(4))
    14  	require.Equal(t, uint64(4), counter.Value())
    15  	require.False(t, counter.Set(2))
    16  	require.Equal(t, uint64(4), counter.Value())
    17  }
    18  
    19  func TestFuzzy(t *testing.T) {
    20  	counter := NewMonotonousCounter(3)
    21  	require.True(t, counter.Set(4))
    22  	require.False(t, counter.Set(2))
    23  	require.True(t, counter.Set(7))
    24  	require.True(t, counter.Set(9))
    25  	require.True(t, counter.Set(12))
    26  	require.False(t, counter.Set(10))
    27  	require.True(t, counter.Set(18))
    28  
    29  	for i := 20; i < 100; i++ {
    30  		require.True(t, counter.Set(uint64(i)))
    31  	}
    32  
    33  	for i := 20; i < 100; i++ {
    34  		require.False(t, counter.Set(uint64(i)))
    35  	}
    36  }
    37  
    38  func TestConcurrent(t *testing.T) {
    39  	counter := NewMonotonousCounter(3)
    40  
    41  	unittest.Concurrently(100, func(i int) {
    42  		counter.Set(uint64(i))
    43  	})
    44  
    45  	require.Equal(t, uint64(99), counter.Value())
    46  }