github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/storage/redis/redis_test.go (about)

     1  package redis
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sync"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/redis/go-redis/v9"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  var Client *redis.Client
    15  
    16  func TestMain(m *testing.M) {
    17  	Client = InitTestRedis()
    18  	if code := m.Run(); code != 0 {
    19  		panic(code)
    20  	}
    21  }
    22  
    23  func TestInitTestRedis(t *testing.T) {
    24  	err := Client.Ping(context.Background()).Err()
    25  	assert.Nil(t, err)
    26  }
    27  
    28  func TestRedisSetGet(t *testing.T) {
    29  	var key = "test-set"
    30  	var value = "test-content"
    31  	Client.Set(context.Background(), key, value, time.Second*100)
    32  
    33  	expectValue := Client.Get(context.Background(), key).Val()
    34  	assert.Equal(t, expectValue, value)
    35  }
    36  
    37  func BenchmarkGoroutineData(b *testing.B) {
    38  	var wg sync.WaitGroup
    39  	for i := 0; i < b.N; i++ {
    40  		wg.Add(1)
    41  		go func(wg *sync.WaitGroup, i int) {
    42  			Client.Set(context.Background(), fmt.Sprintf("test-%d", i), "test-content", time.Minute)
    43  			wg.Done()
    44  		}(&wg, i)
    45  	}
    46  	wg.Wait()
    47  }