make tests faster

master
kevin 4 years ago
parent 41964f9d52
commit 96cb7af728

@ -2,18 +2,18 @@ package bloom
import ( import (
"testing" "testing"
"time"
"github.com/alicebob/miniredis" "github.com/alicebob/miniredis"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/stores/redis" "github.com/tal-tech/go-zero/core/stores/redis"
) )
func TestRedisBitSet_New_Set_Test(t *testing.T) { func TestRedisBitSet_New_Set_Test(t *testing.T) {
s, err := miniredis.Run() s, clean, err := createMiniRedis()
if err != nil { assert.Nil(t, err)
t.Error("Miniredis could not start") defer clean()
}
defer s.Close()
store := redis.NewRedis(s.Addr(), redis.NodeType) store := redis.NewRedis(s.Addr(), redis.NodeType)
bitSet := newRedisBitSet(store, "test_key", 1024) bitSet := newRedisBitSet(store, "test_key", 1024)
@ -46,11 +46,9 @@ func TestRedisBitSet_New_Set_Test(t *testing.T) {
} }
func TestRedisBitSet_Add(t *testing.T) { func TestRedisBitSet_Add(t *testing.T) {
s, err := miniredis.Run() s, clean, err := createMiniRedis()
if err != nil { assert.Nil(t, err)
t.Error("Miniredis could not start") defer clean()
}
defer s.Close()
store := redis.NewRedis(s.Addr(), redis.NodeType) store := redis.NewRedis(s.Addr(), redis.NodeType)
filter := New(store, "test_key", 64) filter := New(store, "test_key", 64)
@ -60,3 +58,22 @@ func TestRedisBitSet_Add(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.True(t, ok) assert.True(t, ok)
} }
func createMiniRedis() (r *miniredis.Miniredis, clean func(), err error) {
r, err = miniredis.Run()
if err != nil {
return nil, nil, err
}
return r, func() {
ch := make(chan lang.PlaceholderType)
go func() {
r.Close()
close(ch)
}()
select {
case <-ch:
case <-time.After(time.Second):
}
}, nil
}

Loading…
Cancel
Save