github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/store/redis/rediaron_test.go (about)

     1  package redis
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/projecteru2/core/engine/factory"
    10  	"github.com/projecteru2/core/types"
    11  	"github.com/projecteru2/core/utils"
    12  
    13  	"github.com/alicebob/miniredis/v2"
    14  	"github.com/go-redis/redis/v8"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/suite"
    17  )
    18  
    19  type RediaronTestSuite struct {
    20  	suite.Suite
    21  
    22  	rediaron   *Rediaron
    23  	rediserver *miniredis.Miniredis
    24  }
    25  
    26  func (s *RediaronTestSuite) SetupTest() {
    27  	s.rediaron.cli.FlushAll(context.Background())
    28  }
    29  
    30  func (s *RediaronTestSuite) TearDownTest() {
    31  	s.rediaron.cli.FlushAll(context.Background())
    32  }
    33  
    34  func (s *RediaronTestSuite) TestIsRedisNoKeyError() {
    35  	_, err := s.rediaron.cli.Get(context.Background(), "thiskeydoesnotexistsofcourseitdoesnt").Result()
    36  	s.True(isRedisNoKeyError(err))
    37  
    38  	s.rediaron.cli.Set(context.Background(), "key1", "value1", 0)
    39  	_, err = s.rediaron.cli.Get(context.Background(), "key1").Result()
    40  	s.False(isRedisNoKeyError(err))
    41  
    42  	s.False(isRedisNoKeyError(fmt.Errorf("i am not redis no key error")))
    43  }
    44  
    45  func (s *RediaronTestSuite) TestKeyNotify() {
    46  	ctx, cancel := context.WithCancel(context.Background())
    47  	ch := s.rediaron.KNotify(ctx, "a*")
    48  	go func() {
    49  		time.Sleep(2 * time.Second)
    50  		cancel()
    51  	}()
    52  
    53  	// trigger manually
    54  	time.Sleep(time.Second)
    55  	s.rediaron.cli.Set(context.Background(), "aaa", 1, 0)
    56  	triggerMockedKeyspaceNotification(s.rediaron.cli, "aaa", actionSet)
    57  	s.rediaron.cli.Set(context.Background(), "aab", 1, 0)
    58  	triggerMockedKeyspaceNotification(s.rediaron.cli, "aab", actionSet)
    59  	s.rediaron.cli.Set(context.Background(), "bab", 1, 0)
    60  	triggerMockedKeyspaceNotification(s.rediaron.cli, "bab", actionSet)
    61  	s.rediaron.cli.Del(context.Background(), "aaa")
    62  	triggerMockedKeyspaceNotification(s.rediaron.cli, "aaa", actionDel)
    63  
    64  	messages := []*KNotifyMessage{}
    65  	for m := range ch {
    66  		messages = append(messages, m)
    67  	}
    68  
    69  	s.Equal(messages[0].Key, "aaa")
    70  	s.Equal(messages[0].Action, "set")
    71  	s.Equal(messages[1].Key, "aab")
    72  	s.Equal(messages[1].Action, "set")
    73  	s.Equal(messages[2].Key, "aaa")
    74  	s.Equal(messages[2].Action, "del")
    75  }
    76  
    77  func TestRediaron(t *testing.T) {
    78  	s, err := miniredis.Run()
    79  	if err != nil {
    80  		t.Fail()
    81  	}
    82  	defer s.Close()
    83  
    84  	config := types.Config{}
    85  	config.LockTimeout = 10 * time.Second
    86  	config.GlobalTimeout = 30 * time.Second
    87  	config.MaxConcurrency = 100000
    88  
    89  	ctx, cancel := context.WithCancel(context.Background())
    90  	defer cancel()
    91  	factory.InitEngineCache(ctx, config, nil)
    92  
    93  	cli := redis.NewClient(&redis.Options{
    94  		Addr: s.Addr(),
    95  		DB:   0,
    96  	})
    97  
    98  	pool, _ := utils.NewPool(20)
    99  
   100  	defer cli.Close()
   101  	suite.Run(t, &RediaronTestSuite{
   102  		rediserver: s,
   103  		rediaron: &Rediaron{
   104  			cli:    cli,
   105  			config: config,
   106  			pool:   pool,
   107  		},
   108  	})
   109  }
   110  
   111  func TestTerminateEmbeddedStorage(t *testing.T) {
   112  	s, err := miniredis.Run()
   113  	if err != nil {
   114  		t.Fail()
   115  	}
   116  	defer s.Close()
   117  
   118  	cli := redis.NewClient(&redis.Options{
   119  		Addr: s.Addr(),
   120  		DB:   0,
   121  	})
   122  	defer cli.Close()
   123  
   124  	rediaron := &Rediaron{
   125  		cli: cli,
   126  	}
   127  
   128  	_, err = rediaron.cli.Ping(context.Background()).Result()
   129  	assert.NoError(t, err)
   130  
   131  	rediaron.TerminateEmbededStorage()
   132  	_, err = rediaron.cli.Ping(context.Background()).Result()
   133  	assert.Error(t, err)
   134  }
   135  
   136  func triggerMockedKeyspaceNotification(cli *redis.Client, key, action string) {
   137  	channel := fmt.Sprintf(keyNotifyPrefix, 0, key)
   138  	cli.Publish(context.Background(), channel, action).Result()
   139  }