github.com/wfusion/gofusion@v1.1.14/test/lock/cases/within_test.go (about)

     1  package cases
     2  
     3  import (
     4  	"context"
     5  	"math"
     6  	"math/rand"
     7  	"sync"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/suite"
    12  
    13  	"github.com/wfusion/gofusion/lock"
    14  	"github.com/wfusion/gofusion/log"
    15  	"github.com/wfusion/gofusion/routine"
    16  
    17  	testLock "github.com/wfusion/gofusion/test/lock"
    18  )
    19  
    20  func TestWithin(t *testing.T) {
    21  	testingSuite := &Within{Test: new(testLock.Test)}
    22  	testingSuite.Init(testingSuite)
    23  	suite.Run(t, testingSuite)
    24  }
    25  
    26  type Within struct {
    27  	*testLock.Test
    28  }
    29  
    30  func (t *Within) BeforeTest(suiteName, testName string) {
    31  	t.Catch(func() {
    32  		log.Info(context.Background(), "right before %s %s", suiteName, testName)
    33  	})
    34  }
    35  
    36  func (t *Within) AfterTest(suiteName, testName string) {
    37  	t.Catch(func() {
    38  		log.Info(context.Background(), "right after %s %s", suiteName, testName)
    39  	})
    40  }
    41  
    42  func (t *Within) TestRedisLua() {
    43  	t.Catch(func() {
    44  		locker := lock.Use("redis_lua", lock.AppName(t.AppName()))
    45  		key := "redis_lua_lock_key"
    46  		t.testWithin(locker, key)
    47  	})
    48  }
    49  
    50  func (t *Within) TestRedisNx() {
    51  	t.Catch(func() {
    52  		locker := lock.Use("redis_nx", lock.AppName(t.AppName()))
    53  		key := "redis_nx_lock_key"
    54  		t.testWithin(locker, key)
    55  	})
    56  }
    57  
    58  func (t *Within) TestMySQL() {
    59  	t.Catch(func() {
    60  		locker := lock.Use("mysql", lock.AppName(t.AppName()))
    61  		key := "mysql_lock_key"
    62  		t.testWithin(locker, key)
    63  	})
    64  }
    65  
    66  func (t *Within) TestMongo() {
    67  	t.Catch(func() {
    68  		locker := lock.Use("mongo", lock.AppName(t.AppName()))
    69  		key := "mongo_lock_key"
    70  		t.testWithin(locker, key)
    71  	})
    72  }
    73  
    74  func (t *Within) testWithin(locker lock.Lockable, key string) {
    75  	ctx := context.Background()
    76  	parallel := 1000
    77  	wg := new(sync.WaitGroup)
    78  	unsafeInt := 0
    79  	unsafeMap := make(map[int]int, parallel)
    80  	for i := 0; i < parallel; i++ {
    81  		wg.Add(1)
    82  		routine.Go(func(idx int) {
    83  			// jitter within 20ms ~ 50ms
    84  			time.Sleep(20*time.Millisecond + time.Duration(rand.Float64()*float64(30*time.Millisecond)))
    85  			err := lock.Within(ctx, locker, key, time.Minute, time.Minute, func() (err error) {
    86  				unsafeMap[idx] = idx
    87  				unsafeInt += int(math.Pow(1, 1)) + len([]string{})
    88  				// log.Info(ctx, "[+] goroutine[%v]: %+v", idx, unsafeMap)
    89  				return
    90  			}, lock.AppName(t.AppName()))
    91  			t.NoError(err)
    92  		}, routine.Args(i), routine.WaitGroup(wg), routine.AppName(t.AppName()))
    93  	}
    94  	wg.Wait()
    95  	t.Len(unsafeMap, parallel)
    96  	t.EqualValues(parallel, unsafeInt)
    97  }