github.com/wfusion/gofusion@v1.1.14/test/cron/cases/asynq_test.go (about)

     1  package cases
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/suite"
    10  	"go.uber.org/atomic"
    11  
    12  	"github.com/wfusion/gofusion/config"
    13  	"github.com/wfusion/gofusion/cron"
    14  	"github.com/wfusion/gofusion/log"
    15  	"github.com/wfusion/gofusion/redis"
    16  
    17  	testCron "github.com/wfusion/gofusion/test/cron"
    18  )
    19  
    20  func TestAsynq(t *testing.T) {
    21  	testingSuite := &Asynq{Test: new(testCron.Test)}
    22  	testingSuite.Init(testingSuite)
    23  	suite.Run(t, testingSuite)
    24  }
    25  
    26  type Asynq struct {
    27  	*testCron.Test
    28  }
    29  
    30  func (t *Asynq) 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 *Asynq) 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 *Asynq) TestMultiCronWithoutLock() {
    43  	t.Catch(func() {
    44  		// Given
    45  		expect := time.Duration(5)
    46  		cnt := atomic.NewInt32(0)
    47  		ctx := context.Background()
    48  		t.cleanByQueue(ctx, "gofusion:cron:default")
    49  		defer t.cleanByQueue(ctx, "gofusion:cron:default")
    50  
    51  		r1 := cron.Use(nameDefault, cron.AppName(t.AppName()))
    52  		r1.Handle("test", func(ctx context.Context, task cron.Task) (err error) {
    53  			cnt.Add(1)
    54  			log.Info(ctx, "[1] we get cron task: %s", task.Name())
    55  			return
    56  		})
    57  		r2 := cron.Use(nameDefaultDup, cron.AppName(t.AppName()))
    58  		r2.Handle("test", func(ctx context.Context, task cron.Task) (err error) {
    59  			cnt.Add(1)
    60  			log.Info(ctx, "[2] we get cron task: %s", task.Name())
    61  			return
    62  		})
    63  
    64  		// When
    65  		t.NoError(r1.Start())
    66  		t.NoError(r2.Start())
    67  		time.Sleep(expect * time.Second)
    68  
    69  		// Then
    70  		t.LessOrEqual(cnt.Load(), int32(expect))
    71  	})
    72  }
    73  
    74  func (t *Asynq) TestMultiCronWithLock() {
    75  	t.Catch(func() {
    76  		// Given
    77  		expect := time.Duration(5)
    78  		cnt := atomic.NewInt32(0)
    79  		ctx := context.Background()
    80  		t.cleanByQueue(ctx, "gofusion:cron:with_lock")
    81  		defer t.cleanByQueue(ctx, "gofusion:cron:with_lock")
    82  
    83  		r1 := cron.Use(nameWithLock, cron.AppName(t.AppName()))
    84  		r1.Handle("with_args", handleWithArgsFunc(nameWithLock))
    85  		r1.Handle("test", func(ctx context.Context, task cron.Task) (err error) {
    86  			cnt.Add(1)
    87  			log.Info(ctx, "[%s] we get cron task: %s", nameWithLock, task.Name())
    88  			return
    89  		})
    90  		r2 := cron.Use(nameWithLockDup, cron.AppName(t.AppName()))
    91  		r2.Handle("with_args", handleWithArgsFunc(nameWithLockDup))
    92  		r2.Handle("test", func(ctx context.Context, task cron.Task) (err error) {
    93  			cnt.Add(1)
    94  			log.Info(ctx, "[%s] we get cron task: %s", nameWithLockDup, task.Name())
    95  			return
    96  		})
    97  
    98  		// When
    99  		t.NoError(r1.Start())
   100  		t.NoError(r2.Start())
   101  		time.Sleep(expect * time.Second)
   102  
   103  		// Then
   104  		t.LessOrEqual(cnt.Load(), int32(expect))
   105  	})
   106  }
   107  
   108  func (t *Asynq) cleanByQueue(ctx context.Context, queue string) {
   109  	pattern := fmt.Sprintf("asynq:{%s}:*", queue)
   110  	if queue == "" {
   111  		pattern = fmt.Sprintf("asynq:{%s:cron}:*", config.Use(t.AppName()).AppName())
   112  	}
   113  
   114  	rdsCli := redis.Use(ctx, "default", redis.AppName(t.AppName()))
   115  	keys, err := rdsCli.Keys(ctx, pattern).Result()
   116  	t.NoError(err)
   117  
   118  	if len(keys) > 0 {
   119  		rdsCli.Del(ctx, keys...)
   120  	}
   121  }