github.com/zhongdalu/gf@v1.0.0/g/container/gchan/gchan_z_unit_test.go (about)

     1  package gchan_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/zhongdalu/gf/g/container/gchan"
     8  	"github.com/zhongdalu/gf/g/test/gtest"
     9  )
    10  
    11  func Test_Gchan(t *testing.T) {
    12  	gtest.Case(t, func() {
    13  		ch := gchan.New(10)
    14  
    15  		gtest.Assert(ch.Cap(), 10)
    16  		gtest.Assert(ch.Push(1), nil)
    17  		gtest.Assert(ch.Len(), 1)
    18  		gtest.Assert(ch.Size(), 1)
    19  		ch.Pop()
    20  		gtest.Assert(ch.Len(), 0)
    21  		gtest.Assert(ch.Size(), 0)
    22  		ch.Close()
    23  		gtest.Assert(ch.Push(1), errors.New("channel is closed"))
    24  
    25  		ch = gchan.New(0)
    26  		ch1 := gchan.New(0)
    27  		go func() {
    28  			var i = 0
    29  			for {
    30  				v := ch.Pop()
    31  				if v == nil {
    32  					ch1.Push(i)
    33  					break
    34  				}
    35  				gtest.Assert(v, i)
    36  				i++
    37  			}
    38  		}()
    39  
    40  		for index := 0; index < 10; index++ {
    41  			ch.Push(index)
    42  		}
    43  		ch.Close()
    44  		gtest.Assert(ch1.Pop(), 10)
    45  		ch1.Close()
    46  	})
    47  }