github.com/wfusion/gofusion@v1.1.14/test/routine/cases/pool_test.go (about)

     1  package cases
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/wfusion/gofusion/log"
    11  	"github.com/wfusion/gofusion/routine"
    12  
    13  	testRoutine "github.com/wfusion/gofusion/test/routine"
    14  )
    15  
    16  func TestPool(t *testing.T) {
    17  	testingSuite := &Pool{Test: new(testRoutine.Test)}
    18  	testingSuite.Init(testingSuite)
    19  	suite.Run(t, testingSuite)
    20  }
    21  
    22  type Pool struct {
    23  	*testRoutine.Test
    24  }
    25  
    26  func (t *Pool) BeforeTest(suiteName, testName string) {
    27  	t.Catch(func() {
    28  		log.Info(context.Background(), "right before %s %s", suiteName, testName)
    29  	})
    30  }
    31  
    32  func (t *Pool) AfterTest(suiteName, testName string) {
    33  	t.Catch(func() {
    34  		log.Info(context.Background(), "right after %s %s", suiteName, testName)
    35  	})
    36  }
    37  
    38  func (t *Pool) TestSubmit() {
    39  	t.Catch(func() {
    40  		wg := new(sync.WaitGroup)
    41  		i := 0
    42  		wg.Add(1)
    43  		pool := routine.NewPool("test_submit", 1,
    44  			routine.WithoutTimeout(), routine.AppName(t.AppName()))
    45  		defer pool.Release()
    46  
    47  		t.NoError(pool.Submit(func() { defer wg.Done(); i += 1 }))
    48  		wg.Wait()
    49  
    50  		t.EqualValues(1, i)
    51  	})
    52  }
    53  
    54  func (t *Pool) TestSubmitWithArgs() {
    55  	t.Catch(func() {
    56  		wg := new(sync.WaitGroup)
    57  		i := 0
    58  		wg.Add(1)
    59  		pool := routine.NewPool("test_submit_with_args", 1,
    60  			routine.WithoutTimeout(), routine.AppName(t.AppName()))
    61  		defer pool.Release()
    62  
    63  		t.NoError(pool.Submit(func(delta int) { defer wg.Done(); i += delta }, routine.Args(2)))
    64  		wg.Wait()
    65  
    66  		t.EqualValues(2, i)
    67  	})
    68  }