github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/goid/test/goid_test.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/isyscore/isc-gobase/goid"
     6  	"github.com/isyscore/isc-gobase/test"
     7  	"github.com/magiconair/properties/assert"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestGoid(t *testing.T) {
    13  	t.Log(goid.Goid())
    14  }
    15  
    16  func TestGet(t *testing.T) {
    17  	ch := make(chan *string, 100)
    18  	for i := 0; i < cap(ch); i++ {
    19  		go func(i int) {
    20  			gid := goid.NativeGoid()
    21  			expected := goid.Goid()
    22  			t.Logf("%d: %d", gid, expected)
    23  			if gid == expected {
    24  				ch <- nil
    25  				return
    26  			}
    27  			s := fmt.Sprintf("Expected %d, but got %d", expected, gid)
    28  			ch <- &s
    29  		}(i)
    30  	}
    31  
    32  	for i := 0; i < cap(ch); i++ {
    33  		val := <-ch
    34  		if val != nil {
    35  			t.Fatal(*val)
    36  		}
    37  	}
    38  }
    39  
    40  func TestAllGoid(t *testing.T) {
    41  	const num = 10
    42  	for i := 0; i < num; i++ {
    43  		go func() {
    44  			time.Sleep(time.Second)
    45  		}()
    46  	}
    47  	time.Sleep(time.Millisecond)
    48  
    49  	ids := goid.AllGoids()
    50  	t.Log("all gids: ", len(ids), ids)
    51  }
    52  
    53  func TestGoStorage(t *testing.T) {
    54  	var variable = "hello world"
    55  	stg := goid.NewLocalStorage()
    56  	stg.Set(variable)
    57  	goid.Go(func() {
    58  		v := stg.Get()
    59  		test.True(t, v != nil && v.(string) == variable)
    60  	})
    61  	time.Sleep(time.Millisecond)
    62  	stg.Clear()
    63  }
    64  
    65  // BenchmarkGoid-12    	278801190	         4.586 ns/op	       0 B/op	       0 allocs/op
    66  func BenchmarkGoid(b *testing.B) {
    67  	for i := 0; i < b.N; i++ {
    68  		_ = goid.Goid()
    69  	}
    70  }
    71  
    72  // BenchmarkAllGoid-12    	 5949680	       228.3 ns/op	     896 B/op	       1 allocs/op
    73  func BenchmarkAllGoid(b *testing.B) {
    74  	const num = 16
    75  	for i := 0; i < num; i++ {
    76  		go func() {
    77  			time.Sleep(time.Second)
    78  		}()
    79  	}
    80  
    81  	b.ReportAllocs()
    82  	b.ResetTimer()
    83  	for i := 0; i < b.N; i++ {
    84  		_ = goid.AllGoids()
    85  	}
    86  }
    87  
    88  func BenchmarkNativeGoid(b *testing.B) {
    89  	for i := 0; i < b.N; i++ {
    90  		_ = goid.NativeGoid()
    91  	}
    92  }
    93  
    94  
    95  var h1 goid.LocalStorage
    96  var h2 goid.LocalStorage
    97  var h3 goid.LocalStorage
    98  var h4 goid.LocalStorage
    99  
   100  func init() {
   101  	h1 = goid.NewLocalStorage()
   102  	h2 = goid.NewLocalStorage()
   103  	h3 = goid.NewLocalStorage()
   104  	h4 = goid.NewLocalStorage()
   105  }
   106  
   107  func TestGoidGet(t *testing.T) {
   108  	h1.Set("11")
   109  	h2.Set("12")
   110  	h3.Set("13")
   111  	h4.Set("14")
   112  	h1V := h1.Get()
   113  	h2V := h2.Get()
   114  	h3V := h3.Get()
   115  	h4V := h4.Get()
   116  
   117  	assert.Equal(t, "11", h1V)
   118  	assert.Equal(t, "12", h2V)
   119  	assert.Equal(t, "13", h3V)
   120  	assert.Equal(t, "14", h4V)
   121  }
   122  
   123  func TestGoidClean(t *testing.T) {
   124  	st := goid.NewLocalStorage()
   125  	st.Set("vv")
   126  	goid.Go(func() {
   127  		fmt.Println("sleep")
   128  		time.Sleep(2*time.Second)
   129  		fmt.Println("wake")
   130  
   131  		fmt.Println(st.Get())
   132  	})
   133  	st.Del()
   134  	fmt.Println("clean")
   135  	time.Sleep(4*time.Second)
   136  	fmt.Println("end")
   137  }
   138  
   139  //func TestGoidChange(t *testing.T) {
   140  //	st := goid.NewLocalStorage()
   141  //	st.Set("vv")
   142  //	goid.Go(func() {
   143  //		fmt.Println("sleep")
   144  //		time.Sleep(2*time.Second)
   145  //		fmt.Println("wake")
   146  //
   147  //		fmt.Println(st.Get())
   148  //	})
   149  //	st.Set("vv0")
   150  //	fmt.Println("clean")
   151  //	time.Sleep(4*time.Second)
   152  //	fmt.Println("end")
   153  //}
   154  //
   155  //
   156  //func TestGoidChange2(t *testing.T) {
   157  //	st := goid.NewLocalStorage()
   158  //	st.Set("vv")
   159  //	goid.Go(func() {
   160  //		fmt.Println("sleep")
   161  //		time.Sleep(2*time.Second)
   162  //		fmt.Println("setValue")
   163  //		st.Set("vv-children")
   164  //	})
   165  //	time.Sleep(3*time.Second)
   166  //	fmt.Println("parent-wake")
   167  //	fmt.Println(st.Get())
   168  //}
   169  //
   170  //func TestGoidChange3(t *testing.T) {
   171  //	st := goid.NewLocalStorage()
   172  //	st.Set("vv")
   173  //	goid.Go(func() {
   174  //		fmt.Println("sleep")
   175  //		time.Sleep(2*time.Second)
   176  //		fmt.Println("wake")
   177  //		fmt.Println(st.Get())
   178  //	})
   179  //	st.Del()
   180  //	fmt.Println("clean")
   181  //	time.Sleep(3*time.Second)
   182  //	fmt.Println("parent-wake")
   183  //	fmt.Println(st.Get())
   184  //}