github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/pool/workerpool/example_workerpool_test.go (about)

     1  package workerpool
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  type InParam struct {
    11  	key string
    12  	val int
    13  }
    14  
    15  type OutParam struct {
    16  	res int
    17  
    18  	err error
    19  }
    20  
    21  func MyDo(inParam interface{}, outParam interface{}) bool {
    22  	println("InParam:", inParam.(*InParam).key, inParam.(*InParam).val)
    23  
    24  	time.Sleep(2 * time.Second)
    25  
    26  	outParam.(*OutParam).err = nil
    27  	outParam.(*OutParam).res = 1111
    28  
    29  	return true
    30  }
    31  
    32  func MyDoSlow(inParam interface{}, outParam interface{}) bool {
    33  	println("InParam:", inParam.(*InParam).key, inParam.(*InParam).val)
    34  
    35  	time.Sleep(4 * time.Second)
    36  
    37  	outParam.(*OutParam).err = nil
    38  	outParam.(*OutParam).res = 1111
    39  	//println("MyDoSlow run ok", outParam)
    40  
    41  	return true
    42  }
    43  
    44  func ExampleWorkerPool_RunOk() {
    45  	wp := NewWorkerPool(3, 5, 3)
    46  
    47  	wp.Run()
    48  	defer wp.Stop()
    49  
    50  	inParam := &InParam{
    51  		key: "wo qu",
    52  		val: 110,
    53  	}
    54  	outParam := &OutParam{}
    55  	timeoutCh := make(chan bool, 1)
    56  	task := &Task{
    57  		Do:          MyDo,
    58  		InParam:     inParam,
    59  		OutParam:    outParam,
    60  		ChIsTimeOut: timeoutCh,
    61  		TimeOut:     3 * time.Second,
    62  	}
    63  	wp.AddTask(task)
    64  	isTimeout := <-timeoutCh
    65  	fmt.Println(isTimeout, outParam.err, outParam.res)
    66  
    67  	// Output:
    68  	// false <nil> 1111
    69  }
    70  
    71  func ExampleWorkerPool_RunTimeout() {
    72  	wp := NewWorkerPool(3, 5, 3)
    73  
    74  	wp.Run()
    75  	defer wp.Stop()
    76  
    77  	inParam1 := &InParam{
    78  		key: "wo qu",
    79  		val: 110,
    80  	}
    81  	outParam1 := &OutParam{}
    82  	timeoutCh1 := make(chan bool, 1)
    83  	task1 := &Task{
    84  		Do:          MyDoSlow,
    85  		InParam:     inParam1,
    86  		OutParam:    outParam1,
    87  		ChIsTimeOut: timeoutCh1,
    88  		TimeOut:     3 * time.Second,
    89  	}
    90  	wp.AddTask(task1)
    91  	isTimeout1 := <-timeoutCh1
    92  	fmt.Println(isTimeout1, outParam1.err, outParam1.res)
    93  
    94  	// Output:
    95  	// true <nil> 0
    96  }
    97  
    98  func TestWorkerPool_RunManyOk(t *testing.T) {
    99  	wp := NewWorkerPool(3, 5, 3)
   100  
   101  	wp.Run()
   102  	defer wp.Stop()
   103  
   104  	timeoutChs := make([]chan bool, 10)
   105  	for i := 0; i < 10; i++ {
   106  		inParam := &InParam{
   107  			key: "wo qu",
   108  			val: i + 100000,
   109  		}
   110  		outParam := &OutParam{}
   111  		timeoutChs[i] = make(chan bool, 1)
   112  		task := &Task{
   113  			Do:          MyDo,
   114  			InParam:     inParam,
   115  			OutParam:    outParam,
   116  			ChIsTimeOut: timeoutChs[i],
   117  			TimeOut:     3 * time.Second,
   118  		}
   119  		wp.AddTask(task)
   120  	}
   121  
   122  	batchAsyncDoTimeout(timeoutChs)
   123  }
   124  
   125  func TestWorkerPool_RunManyTimeout(t *testing.T) {
   126  	wp := NewWorkerPool(3, 5, 3)
   127  
   128  	wp.Run()
   129  	defer wp.Stop()
   130  
   131  	timeoutChs := make([]chan bool, 10)
   132  	for i := 0; i < 10; i++ {
   133  		inParam := &InParam{
   134  			key: "wo qu",
   135  			val: i + 1000,
   136  		}
   137  		outParam := &OutParam{}
   138  		timeoutChs[i] = make(chan bool, 1)
   139  		task := &Task{
   140  			Do:          MyDoSlow,
   141  			InParam:     inParam,
   142  			OutParam:    outParam,
   143  			ChIsTimeOut: timeoutChs[i],
   144  			TimeOut:     3 * time.Second,
   145  		}
   146  		wp.AddTask(task)
   147  	}
   148  
   149  	batchAsyncDoTimeout(timeoutChs)
   150  }
   151  
   152  func TestWorkerPool_RunManyOkTimeout(t *testing.T) {
   153  	wp := NewWorkerPool(3, 5, 3)
   154  
   155  	wp.Run()
   156  	defer wp.Stop()
   157  
   158  	timeoutChs := make([]chan bool, 10)
   159  	for i := 0; i < 10; i++ {
   160  		inParam := &InParam{
   161  			key: "wo qu",
   162  			val: i + 1000,
   163  		}
   164  		outParam := &OutParam{}
   165  		timeoutChs[i] = make(chan bool, 1)
   166  		timeOut := 3 * time.Second
   167  		if i%2 == 0 {
   168  			timeOut = 5 * time.Second
   169  		}
   170  		task := &Task{
   171  			Do:          MyDoSlow,
   172  			InParam:     inParam,
   173  			OutParam:    outParam,
   174  			ChIsTimeOut: timeoutChs[i],
   175  			TimeOut:     timeOut,
   176  		}
   177  		wp.AddTask(task)
   178  	}
   179  
   180  	batchAsyncDoTimeout(timeoutChs)
   181  }
   182  
   183  func batchAsyncDoTimeout(timeoutChs []chan bool) {
   184  	wg := &sync.WaitGroup{}
   185  	for i, ch := range timeoutChs {
   186  		wg.Add(1)
   187  		go func(wg *sync.WaitGroup, index int, ch chan bool) {
   188  			defer wg.Done()
   189  			for {
   190  				select {
   191  				case isTimeout, ok := <-ch:
   192  					fmt.Println("timeOutChs--->", index, isTimeout, ok)
   193  					return
   194  				}
   195  			}
   196  		}(wg, i, ch)
   197  	}
   198  	wg.Wait()
   199  }
   200  
   201  func TestWorkerPool_RunManyOkTimeoutWithTimeOutHandle(t *testing.T) {
   202  	wp := NewWorkerPool(3, 5, 3)
   203  
   204  	wp.Run()
   205  	defer wp.Stop()
   206  
   207  	//timeoutChs := make([]chan bool, 10)
   208  	for i := 0; i < 10; i++ {
   209  		inParam := &InParam{
   210  			key: "wo qu",
   211  			val: i + 1000,
   212  		}
   213  		outParam := &OutParam{}
   214  		//timeoutChs[i] = make(chan bool, 1)
   215  		timeOut := 3 * time.Second
   216  		if i%2 == 0 {
   217  			timeOut = 5 * time.Second
   218  		}
   219  		task := &Task{
   220  			Do:          MyDoSlow,
   221  			InParam:     inParam,
   222  			OutParam:    outParam,
   223  			//ChIsTimeOut: timeoutChs[i],
   224  			TimeOut:     timeOut,
   225  			OnTimeOut: func(inParam interface{}, outParam interface{}) {
   226  				key := inParam.(*InParam).key
   227  				val := inParam.(*InParam).val
   228  				println("inParam", key, val, "timeout")
   229  			},
   230  		}
   231  		wp.AddTask(task)
   232  	}
   233  
   234  }