github.com/davecheney/badidea@v1.0.0/badidea_test.go (about)

     1  package badidea_test
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/davecheney/badidea"
     9  )
    10  
    11  func TestGoroutineID(t *testing.T) {
    12  	got := badidea.GoroutineID()
    13  	if got < 1 {
    14  		t.Fatalf("expected goroutine id > 0, got: %v", got)
    15  	}
    16  	want, got := got, badidea.GoroutineID()
    17  	if got != want {
    18  		t.Fatalf("expected same goroutine id: %v, got: %v", want, got)
    19  	}
    20  
    21  	c := make(chan int64)
    22  	go func() {
    23  		c <- badidea.GoroutineID()
    24  	}()
    25  	got = <-c
    26  	if got < want {
    27  		t.Fatalf("expected new goroutine to have higher id than: %v, got %v", want, got)
    28  	}
    29  }
    30  
    31  func ExampleGoroutineID() {
    32  	var wg sync.WaitGroup
    33  	wg.Add(10)
    34  	for i := 0; i < 10; i++ {
    35  		go func() {
    36  			defer wg.Done()
    37  			fmt.Println("goroutine id:", badidea.GoroutineID())
    38  		}()
    39  	}
    40  	wg.Wait()
    41  }