redis_test.go 1.04 KB
Newer Older
李伟@五瓣科技's avatar
李伟@五瓣科技 committed
1 2 3
package multisend

import (
4 5
	"context"
	"encoding/json"
李伟@五瓣科技's avatar
李伟@五瓣科技 committed
6
	"testing"
7 8 9
	"time"

	"github.com/google/uuid"
李伟@五瓣科技's avatar
李伟@五瓣科技 committed
10 11 12 13
)

func TestRateLimit(t *testing.T) {

李伟@五瓣科技's avatar
李伟@五瓣科技 committed
14
	// client:= initClient(10)
李伟@五瓣科技's avatar
李伟@五瓣科技 committed
15

李伟@五瓣科技's avatar
李伟@五瓣科技 committed
16 17
	// ctx := context.Background()
	// limiter := redis_rate.NewLimiter(client)
李伟@五瓣科技's avatar
李伟@五瓣科技 committed
18

李伟@五瓣科技's avatar
李伟@五瓣科技 committed
19 20 21 22 23
	// for i := 0; i < 10; i++ {
	// 	res, err := limiter.Allow(ctx, "project:123", redis_rate.PerSecond(10))
	// 	if err != nil {
	// 		panic(err)
	// 	}
李伟@五瓣科技's avatar
李伟@五瓣科技 committed
24

李伟@五瓣科技's avatar
李伟@五瓣科技 committed
25 26
	// 	client.Set(ctx, "key", i, time.Second*100)
	// 	fmt.Println("allowed", res.Allowed, "remaining", res.Remaining)
李伟@五瓣科技's avatar
李伟@五瓣科技 committed
27

李伟@五瓣科技's avatar
李伟@五瓣科技 committed
28
	// }
李伟@五瓣科技's avatar
李伟@五瓣科技 committed
29 30

}
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

type Person struct {
	Name string `json:"name"`
	Age  int64  `json:"age"`
}

func TestSetGetJson(t *testing.T) {
	client := initClient(10, "13.40.18.11:6379", "redis20220217")

	id := uuid.New()

	p1 := Person{
		Name: "alice",
		Age:  10,
	}

	p1AsJson, err := json.Marshal(p1)
	if err != nil {
		panic(err)
	}

	client.Set(context.Background(), id.String(), p1AsJson, time.Hour*24)

	recordAsJson, err := client.Get(context.Background(), "111").Result()

	if err != nil {
		panic(err)
	}
	t.Logf("recordAsJson: %s \n", recordAsJson)

}