github.com/koko1123/flow-go-1@v0.29.6/engine/common/fifoqueue/fifoqueue_test.go (about)

     1  package fifoqueue
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestPushAndPull(t *testing.T) {
    11  	queue, err := NewFifoQueue(CapacityUnlimited)
    12  	require.NoError(t, err)
    13  	for i := 0; i < 10; i++ {
    14  		queue.Push(i)
    15  	}
    16  
    17  	require.Equal(t, 10, queue.Len())
    18  
    19  	for i := 0; i < 10; i++ {
    20  		n, ok := queue.Pop()
    21  		require.True(t, ok)
    22  		require.Equal(t, i, n)
    23  	}
    24  	require.Equal(t, 0, queue.Len())
    25  
    26  	_, ok := queue.Pop()
    27  	require.False(t, ok)
    28  }
    29  
    30  func TestConcurrentPushPull(t *testing.T) {
    31  	queue, err := NewFifoQueue(CapacityUnlimited)
    32  	require.NoError(t, err)
    33  
    34  	count := 100
    35  	// verify that concurrent push will end up having 100 items in the queue
    36  	var sent sync.WaitGroup
    37  	for i := 0; i < count; i++ {
    38  		sent.Add(1)
    39  		go func(i int) {
    40  			queue.Push(i)
    41  			sent.Done()
    42  		}(i)
    43  	}
    44  	sent.Wait()
    45  
    46  	require.Equal(t, count, queue.Len())
    47  
    48  	// verify that concurrent Pop will always get one, and in the end, the queue
    49  	// is empty
    50  	for i := 0; i < count; i++ {
    51  		sent.Add(1)
    52  		go func(i int) {
    53  			_, ok := queue.Pop()
    54  			sent.Done()
    55  			require.True(t, ok)
    56  		}(i)
    57  	}
    58  	sent.Wait()
    59  
    60  	require.Equal(t, 0, queue.Len())
    61  }