github.com/wfusion/gofusion@v1.1.14/test/redis/cases/redis_test.go (about)

     1  package cases
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/wfusion/gofusion/log"
    11  	"github.com/wfusion/gofusion/redis"
    12  
    13  	testRedis "github.com/wfusion/gofusion/test/redis"
    14  )
    15  
    16  func TestRedis(t *testing.T) {
    17  	testingSuite := &Redis{Test: new(testRedis.Test)}
    18  	testingSuite.Init(testingSuite)
    19  	suite.Run(t, testingSuite)
    20  }
    21  
    22  type Redis struct {
    23  	*testRedis.Test
    24  }
    25  
    26  func (t *Redis) 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 *Redis) 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 *Redis) TestGetSet() {
    39  	t.Catch(func() {
    40  		// Given
    41  		key := "test:getset:key"
    42  		val := "this is a value"
    43  		ctx := context.Background()
    44  		rdsCli := redis.Use(ctx, nameDefault, redis.AppName(t.AppName()))
    45  
    46  		// When
    47  		t.NoError(rdsCli.Set(ctx, key, val, time.Second).Err())
    48  		defer rdsCli.Del(ctx, key)
    49  
    50  		// Then
    51  		actual, err := rdsCli.Get(ctx, key).Result()
    52  		t.NoError(err)
    53  		t.Equal(val, actual)
    54  	})
    55  }
    56  
    57  func (t *Redis) TestSubscribe() {
    58  	t.Catch(func() {
    59  		// Given
    60  		ctx := context.Background()
    61  		rdsCli := redis.Use(ctx, nameDefault, redis.AppName(t.AppName()))
    62  
    63  		// When
    64  		pubsub := rdsCli.Subscribe(ctx, "asynq:cancel")
    65  
    66  		// Then
    67  		_, err := pubsub.ReceiveTimeout(ctx, 500*time.Millisecond)
    68  		t.NoError(err)
    69  	})
    70  }