github.com/pharosnet/flyline@v1.0.2/queue_test.go (about)

     1  package flyline
     2  
     3  import (
     4  	"runtime"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestQueue_Single(t *testing.T) {
    11  	q := new(queue)
    12  	q.add(1)
    13  	v := q.poll()
    14  	t.Logf("queue poll: %v", v)
    15  }
    16  
    17  func TestQueue_multi(t *testing.T) {
    18  	runtime.GOMAXPROCS(8)
    19  	q := new(queue)
    20  	wg := new(sync.WaitGroup)
    21  	for i := 0; i < 10; i++ {
    22  		go func(q *queue, wg *sync.WaitGroup) {
    23  			q.add(time.Now())
    24  			wg.Add(1)
    25  		}(q, wg)
    26  	}
    27  	wg.Add(2)
    28  	for i := 0; i < 2; i++ {
    29  		go func(q *queue, wg *sync.WaitGroup, i int) {
    30  			for j := 0; j < 5; j++ {
    31  				t.Logf("queue poll %v : %v", i, q.poll())
    32  				wg.Done()
    33  			}
    34  		}(q, wg, i)
    35  		wg.Done()
    36  	}
    37  	wg.Wait()
    38  	time.Sleep(500 * time.Millisecond)
    39  }