github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/gls/gls_test.go (about)

     1  package gls
     2  
     3  import (
     4  	"testing"
     5  	"sync"
     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.Fail()
    14  		}
    15  		Set("hello", "world")
    16  		if "world" != Get("hello") {
    17  			t.Fail()
    18  		}
    19  		if !IsGlsEnabled(GoID()) {
    20  			t.Fail()
    21  		}
    22  		wg.Done()
    23  	})()
    24  	wg.Wait()
    25  	if IsGlsEnabled(GoID()) {
    26  		t.Fail()
    27  	}
    28  	if nil != Get("hello") {
    29  		t.Fail()
    30  	}
    31  	//SetIndex("hello", "world") // will panic
    32  }
    33  
    34  func TestNestedGls(t *testing.T) {
    35  	wg := sync.WaitGroup{}
    36  	wg.Add(1)
    37  	go WithGls(func() {
    38  		Set("hello", "world")
    39  		go WithGls(func() {
    40  			if "world" != Get("hello") {
    41  				t.Fail()
    42  			}
    43  			wg.Done()
    44  		})()
    45  	})()
    46  	wg.Wait()
    47  }