util_test.go 750 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package cache
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/alicebob/miniredis"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tal-tech/go-zero/core/lang"
  8. )
  9. func TestFormatKeys(t *testing.T) {
  10. assert.Equal(t, "a,b", formatKeys([]string{"a", "b"}))
  11. }
  12. func TestTotalWeights(t *testing.T) {
  13. val := TotalWeights([]NodeConf{
  14. {
  15. Weight: -1,
  16. },
  17. {
  18. Weight: 0,
  19. },
  20. {
  21. Weight: 1,
  22. },
  23. })
  24. assert.Equal(t, 1, val)
  25. }
  26. func createMiniRedis() (r *miniredis.Miniredis, clean func(), err error) {
  27. r, err = miniredis.Run()
  28. if err != nil {
  29. return nil, nil, err
  30. }
  31. return r, func() {
  32. ch := make(chan lang.PlaceholderType)
  33. go func() {
  34. r.Close()
  35. close(ch)
  36. }()
  37. select {
  38. case <-ch:
  39. case <-time.After(time.Second):
  40. }
  41. }, nil
  42. }