gitee.com/quant1x/gox@v1.21.2/gls/gls_test.go (about)

     1  package gls
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  )
     7  
     8  func TestGls(t *testing.T) {
     9  	wg := sync.WaitGroup{}
    10  	wg.Add(1)
    11  	go WithGls(func() {
    12  		if nil != Get("hello") {
    13  			t.Errorf("1...\n")
    14  			return
    15  			//t.Fail()
    16  		}
    17  		Set("hello", "world")
    18  		if "world" != Get("hello") {
    19  			t.Errorf("2...\n")
    20  			return
    21  			//t.Fail()
    22  		}
    23  		if !IsGlsEnabled(GoID()) {
    24  			t.Errorf("3...\n")
    25  			return
    26  			//t.Fail()
    27  		}
    28  		wg.Done()
    29  	})()
    30  	wg.Wait()
    31  	if IsGlsEnabled(GoID()) {
    32  		t.Fatalf("4...\n")
    33  		//t.Fail()
    34  	}
    35  	if nil != Get("hello") {
    36  		t.Fatalf("5...\n")
    37  		//t.Fail()
    38  	}
    39  	//SetIndex("hello", "world") // will panic
    40  }
    41  
    42  func TestNestedGls(t *testing.T) {
    43  	wg := sync.WaitGroup{}
    44  	wg.Add(1)
    45  	go WithGls(func() {
    46  		Set("hello", "world")
    47  		go WithGls(func() {
    48  			if "world" != Get("hello") {
    49  				t.Errorf("6...\n")
    50  				return
    51  				//t.Fail()
    52  			}
    53  			wg.Done()
    54  		})()
    55  	})()
    56  	wg.Wait()
    57  }