github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xcontainer/queue/queue_test.go (about)

     1  package queue
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestBlockingQueue(t *testing.T) {
    11  	t.Run("put", func(t *testing.T) {
    12  		q := NewBlockingQueue(1)
    13  		require.Equal(t, true, q.Push(1, time.Second), "should not block when putting first element")
    14  		require.Equal(t, false, q.Push(2, time.Second), "should block when putting second element")
    15  	})
    16  	t.Run("get", func(t *testing.T) {
    17  		q := NewBlockingQueue(1)
    18  		require.Equal(t, true, q.Push(1, time.Second), "should not block when putting first element")
    19  		x, ok := q.Pop(time.Second)
    20  		require.Equal(t, true, ok, "should get first element")
    21  		require.Equal(t, 1, x, "first element should be 1")
    22  		_, ok = q.Pop(time.Second)
    23  		require.Equal(t, false, ok, "should get nothing when get again")
    24  	})
    25  }