github.com/xg0n/routine@v0.0.0-20240119033701-c364deb94aee/routine_test.go (about)

     1  package routine
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestInheritedTask(t *testing.T) {
    10  	tls := NewInheritableThreadLocal[string]()
    11  	it := inheritedTask{context: nil, function: func() {
    12  		assert.Equal(t, "", tls.Get())
    13  	}}
    14  	task := NewFutureTask(it.run)
    15  	go task.Run()
    16  	assert.Nil(t, task.Get())
    17  	assert.True(t, task.IsDone())
    18  	//
    19  	it2 := inheritedTask{context: nil, function: func() {
    20  		assert.Equal(t, "", tls.Get())
    21  	}}
    22  	task2 := NewFutureTask(it2.run)
    23  	go func() {
    24  		tls.Set("hello")
    25  		task2.Run()
    26  	}()
    27  	assert.Nil(t, task2.Get())
    28  	assert.True(t, task2.IsDone())
    29  	//
    30  	tls.Set("world")
    31  	it3 := inheritedTask{context: createInheritedMap(), function: func() {
    32  		assert.Equal(t, "world", tls.Get())
    33  	}}
    34  	task3 := NewFutureTask(it3.run)
    35  	go task3.Run()
    36  	assert.Nil(t, task3.Get())
    37  	assert.True(t, task3.IsDone())
    38  	//
    39  	it4 := inheritedTask{context: createInheritedMap(), function: func() {
    40  		assert.Equal(t, "world", tls.Get())
    41  	}}
    42  	task4 := NewFutureTask(it4.run)
    43  	go func() {
    44  		tls.Set("hello")
    45  		task4.Run()
    46  	}()
    47  	assert.Nil(t, task4.Get())
    48  	assert.True(t, task4.IsDone())
    49  }
    50  
    51  func TestInheritedWaitTask(t *testing.T) {
    52  	tls := NewInheritableThreadLocal[string]()
    53  	it := inheritedWaitTask{context: nil, function: func(token CancelToken) {
    54  		assert.Equal(t, "", tls.Get())
    55  	}}
    56  	task := NewFutureTask(it.run)
    57  	go task.Run()
    58  	assert.Nil(t, task.Get())
    59  	assert.True(t, task.IsDone())
    60  	//
    61  	it2 := inheritedWaitTask{context: nil, function: func(token CancelToken) {
    62  		assert.Equal(t, "", tls.Get())
    63  	}}
    64  	task2 := NewFutureTask(it2.run)
    65  	go func() {
    66  		tls.Set("hello")
    67  		task2.Run()
    68  	}()
    69  	assert.Nil(t, task2.Get())
    70  	assert.True(t, task2.IsDone())
    71  	//
    72  	tls.Set("world")
    73  	it3 := inheritedWaitTask{context: createInheritedMap(), function: func(token CancelToken) {
    74  		assert.Equal(t, "world", tls.Get())
    75  	}}
    76  	task3 := NewFutureTask(it3.run)
    77  	go task3.Run()
    78  	assert.Nil(t, task3.Get())
    79  	assert.True(t, task3.IsDone())
    80  	//
    81  	it4 := inheritedWaitTask{context: createInheritedMap(), function: func(token CancelToken) {
    82  		assert.Equal(t, "world", tls.Get())
    83  	}}
    84  	task4 := NewFutureTask(it4.run)
    85  	go func() {
    86  		tls.Set("hello")
    87  		task4.Run()
    88  	}()
    89  	assert.Nil(t, task4.Get())
    90  	assert.True(t, task4.IsDone())
    91  }
    92  
    93  func TestInheritedWaitResultTask(t *testing.T) {
    94  	tls := NewInheritableThreadLocal[string]()
    95  	it := inheritedWaitResultTask[string]{context: nil, function: func(token CancelToken) string {
    96  		assert.Equal(t, "", tls.Get())
    97  		return tls.Get()
    98  	}}
    99  	task := NewFutureTask(it.run)
   100  	go task.Run()
   101  	assert.Equal(t, "", task.Get())
   102  	assert.True(t, task.IsDone())
   103  	//
   104  	it2 := inheritedWaitResultTask[string]{context: nil, function: func(token CancelToken) string {
   105  		assert.Equal(t, "", tls.Get())
   106  		return tls.Get()
   107  	}}
   108  	task2 := NewFutureTask(it2.run)
   109  	go func() {
   110  		tls.Set("hello")
   111  		task2.Run()
   112  	}()
   113  	assert.Equal(t, "", task2.Get())
   114  	assert.True(t, task2.IsDone())
   115  	//
   116  	tls.Set("world")
   117  	it3 := inheritedWaitResultTask[string]{context: createInheritedMap(), function: func(token CancelToken) string {
   118  		assert.Equal(t, "world", tls.Get())
   119  		return tls.Get()
   120  	}}
   121  	task3 := NewFutureTask(it3.run)
   122  	go task3.Run()
   123  	assert.Equal(t, "world", task3.Get())
   124  	assert.True(t, task3.IsDone())
   125  	//
   126  	it4 := inheritedWaitResultTask[string]{context: createInheritedMap(), function: func(token CancelToken) string {
   127  		assert.Equal(t, "world", tls.Get())
   128  		return tls.Get()
   129  	}}
   130  	task4 := NewFutureTask(it4.run)
   131  	go func() {
   132  		tls.Set("hello")
   133  		task4.Run()
   134  	}()
   135  	assert.Equal(t, "world", task4.Get())
   136  	assert.True(t, task4.IsDone())
   137  }