Categorygithub.com/olomix/go-test-redis/v2
modulepackage
2.0.0
Repository: https://github.com/olomix/go-test-redis.git
Documentation: pkg.go.dev

# README

go-test-redis

Helper tool to test Go programs against Redis

go-test-redis expects the redis server address in REDISADDR environment variable. If it is empty, default address is :6379.

Example:

import go_test_redis "github.com/olomix/go-test-redis"

func TestCache(t *testing.T) {
	rdb := go_test_redis.WithRedis(t)
	if err := rdb.Ping(context.Background()).Err(); err != nil {
		t.Fatal(err)
	}
}

WithRedis looking for the empty database and locks it to prevent other parallel tests to use the same database. If all databases are busy, we are waiting for free one.

When we need to wait for redis to be available in CI if it starting in paralle container, we can use WaitForRedis helper. Example:

func TestMain(m *testing.M) {
	err :=
		go_test_redis.WaitForRedis(go_test_redis.WithTimeout(30 * time.Second))
	if err != nil {
		panic(err)
	}

	os.Exit(m.Run())
}

If we skip WithTimeout option, 5 seconds is the default one.

# Functions

WaitForRedis is useful to use in TestMain function to wait until redis would be available.
WithRedis return redis client connected to empty redis database.
WithTimeout overwrite default timeout to wait for redis availability in WaitForRedis function.

# Type aliases

No description provided by the author