func subscribeDatabase(assert audit.Assertion, options ...redis.Option) (*redis.Subscription, func()) {
	options = append(options, redis.Index(testDatabaseIndex, ""))
	db, err := redis.Open(options...)
	assert.Nil(err)
	sub, err := db.Subscription()
	assert.Nil(err)
	return sub, func() {
		sub.Close()
		db.Close()
	}
}
subscribeDatabase connects to a Redis database with the given options
and returns a subscription and a function for closing. This function
shall be called with a defer.
package redis_test
import (
	"testing"
	"time"
	"github.com/tideland/golib/audit"
	"github.com/tideland/golib/logger"
	"github.com/tideland/golib/redis"
)
func TestUnixSocketConnection(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	conn, restore := connectDatabase(assert, redis.UnixConnection("", 0))
	defer restore()
	result, err := conn.Do("echo", "Hello, World!")
	assert.Nil(err)
	assertEqualString(assert, result, 0, "Hello, World!")
	result, err = conn.Do("ping")
	assert.Nil(err)
	assertEqualString(assert, result, 0, "+PONG")
}
func BenchmarkUnixConnection(b *testing.B) {
	assert := audit.NewTestingAssertion(b, true)
	conn, restore := connectDatabase(assert, redis.UnixConnection("", 0))
	defer restore()
	for i := 0; i < b.N; i++ {
		result, err := conn.Do("ping")
		assert.Nil(err)
		assertEqualString(assert, result, 0, "+PONG")
	}
}
func TestTcpConnection(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	conn, restore := connectDatabase(assert, redis.TcpConnection("", 0))
	defer restore()
	result, err := conn.Do("echo", "Hello, World!")
	assert.Nil(err)
	assertEqualString(assert, result, 0, "Hello, World!")
	result, err = conn.Do("ping")
	assert.Nil(err)
	assertEqualString(assert, result, 0, "+PONG")
}
func BenchmarkTcpConnection(b *testing.B) {
	assert := audit.NewTestingAssertion(b, true)
	conn, restore := connectDatabase(assert, redis.TcpConnection("", 0))
	defer restore()
	for i := 0; i < b.N; i++ {
		result, err := conn.Do("ping")
		assert.Nil(err)
		assertEqualString(assert, result, 0, "+PONG")
	}
}
func TestPipelining(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	ppl, restore := pipelineDatabase(assert)
	defer restore()
	for i := 0; i < 1000; i++ {
		err := ppl.Do("ping")
		assert.Nil(err)
	}
	results, err := ppl.Collect()
	assert.Nil(err)
	assert.Length(results, 1000)
	for _, result := range results {
		assertEqualString(assert, result, 0, "+PONG")
	}
}
func BenchmarkPipelining(b *testing.B) {
	assert := audit.NewTestingAssertion(b, true)
	ppl, restore := pipelineDatabase(assert)
	defer restore()
	for i := 0; i < b.N; i++ {
		err := ppl.Do("ping")
		assert.Nil(err)
	}
	results, err := ppl.Collect()
	assert.Nil(err)
	assert.Length(results, b.N)
	for _, result := range results {
		assertEqualString(assert, result, 0, "+PONG")
	}
}
func TestOptions(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	db, err := redis.Open(redis.UnixConnection("", 0), redis.PoolSize(5))
	assert.Nil(err)
	defer db.Close()
	options := db.Options()
	assert.Equal(options.Address, "/tmp/redis.sock")
	assert.Equal(options.Network, "unix")
	assert.Equal(options.Timeout, 30*time.Second)
	assert.Equal(options.Index, 0)
	assert.Equal(options.Password, "")
	assert.Equal(options.PoolSize, 5)
	assert.Equal(options.Logging, false)
	assert.Equal(options.Monitoring, false)
}
func TestConcurrency(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	db, err := redis.Open(redis.UnixConnection("", 0), redis.PoolSize(5))
	assert.Nil(err)
	defer db.Close()
	for i := 0; i < 500; i++ {
		go func() {
			conn, err := db.Connection()
			assert.Nil(err)
			defer conn.Return()
			result, err := conn.Do("ping")
			assert.Nil(err)
			assertEqualString(assert, result, 0, "+PONG")
			time.Sleep(10 * time.Millisecond)
		}()
	}
}
func init() {
	logger.SetLevel(logger.LevelDebug)
}
const testDatabaseIndex = 0
func connectDatabase(assert audit.Assertion, options ...redis.Option) (*redis.Connection, func()) {
	options = append(options, redis.Index(testDatabaseIndex, ""))
	db, err := redis.Open(options...)
	assert.Nil(err)
	conn, err := db.Connection()
	assert.Nil(err)
	_, err = conn.Do("flushdb")
	assert.Nil(err)
	return conn, func() {
		conn.Return()
		db.Close()
	}
}
func pipelineDatabase(assert audit.Assertion, options ...redis.Option) (*redis.Pipeline, func()) {
	options = append(options, redis.Index(testDatabaseIndex, ""))
	db, err := redis.Open(options...)
	assert.Nil(err)
	ppl, err := db.Pipeline()
	assert.Nil(err)
	return ppl, func() {
		db.Close()
	}
}