Documentation ¶
Overview ¶
Package rediqueue is a pure Go Redis test server, for use in Go unittests. There are no dependencies on system binaries, and every server you start will be empty.
Start a server with `s, err := rediqueue.Run()`. Stop it with `defer s.Close()`.
Point your Redis client to `s.Addr()` or `s.Host(), s.Port()`.
Set keys directly via s.Set(...) and similar commands, or use a Redis client.
For direct use you can select a Redis database with either `s.Select(12); s.Get("foo")` or `s.DB(12).Get("foo")`.
Example ¶
package main import ( "github.com/chinahdkj/rediqueue" "github.com/garyburd/redigo/redis" ) func main() { s, err := rediqueue.Run() if err != nil { panic(err) } defer s.Close() // Configure you application to connect to redis at s.Addr() // Any redis client should work, as long as you use redis commands which // rediqueue implements. c, err := redis.Dial("tcp", s.Addr()) if err != nil { panic(err) } if _, err = c.Do("LPUSH", "foo", "bar"); err != nil { panic(err) } // You can ask rediqueue about keys directly, without going over the network. if got, err := s.Lpop("foo"); err != nil || got != "bar" { panic("Didn't get 'bar' back") } // Or with a DB id if _, err := s.DB(42).Lpop("foo"); err != rediqueue.ErrKeyNotFound { panic("didn't use a different database") } // Or use a Check* function which Fail()s if the key is not what we expect // (checks for existence, key type and the value) // s.CheckGet(t, "foo", "bar") // Check if there really was only one connection. if s.TotalConnectionCount() != 1 { panic("too many connections made") } }
Output:
Index ¶
- Variables
- type RediQueue
- func (m *RediQueue) Addr() string
- func (m *RediQueue) CheckList(t T, key string, expected ...string)
- func (m *RediQueue) CheckSet(t T, key string, expected ...string)
- func (m *RediQueue) Close()
- func (m *RediQueue) CommandCount() int
- func (m *RediQueue) CurrentConnectionCount() int
- func (m *RediQueue) DB(i int) *RedisDB
- func (m *RediQueue) Del(k string) bool
- func (m *RediQueue) Dump() string
- func (m *RediQueue) Exists(k string) bool
- func (m *RediQueue) FlushAll()
- func (m *RediQueue) FlushDB()
- func (m *RediQueue) Host() string
- func (m *RediQueue) IsMember(k, v string) (bool, error)
- func (m *RediQueue) Keys() []string
- func (m *RediQueue) List(k string) ([]string, error)
- func (m *RediQueue) Load()
- func (m *RediQueue) Lpop(k string) (string, error)
- func (m *RediQueue) Lpush(k, v string) (int, error)
- func (m *RediQueue) Members(k string) ([]string, error)
- func (m *RediQueue) Pop(k string) (string, error)
- func (m *RediQueue) Port() string
- func (m *RediQueue) Push(k string, v ...string) (int, error)
- func (m *RediQueue) RequireAuth(pw string)
- func (m *RediQueue) Restart() error
- func (m *RediQueue) SRem(k string, fields ...string) (int, error)
- func (m *RediQueue) Save()
- func (m *RediQueue) Select(i int)
- func (m *RediQueue) SetAdd(k string, elems ...string) (int, error)
- func (m *RediQueue) SetTime(t time.Time)
- func (m *RediQueue) Start() error
- func (m *RediQueue) StartAddr(addr string) error
- func (m *RediQueue) TotalConnectionCount() int
- func (m *RediQueue) Type(k string) string
- type RedisDB
- func (db *RedisDB) Del(k string) bool
- func (db *RedisDB) Exists(k string) bool
- func (db *RedisDB) FlushDB()
- func (db *RedisDB) IsMember(k, v string) (bool, error)
- func (db *RedisDB) Keys() []string
- func (db *RedisDB) List(k string) ([]string, error)
- func (db *RedisDB) Lpop(k string) (string, error)
- func (db *RedisDB) Lpush(k, v string) (int, error)
- func (db *RedisDB) Members(k string) ([]string, error)
- func (db *RedisDB) Pop(k string) (string, error)
- func (db *RedisDB) Push(k string, v ...string) (int, error)
- func (db *RedisDB) SRem(k string, fields ...string) (int, error)
- func (db *RedisDB) SetAdd(k string, elems ...string) (int, error)
- func (db *RedisDB) Type(k string) string
- type T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrKeyNotFound is returned when a key doesn't exist. ErrKeyNotFound = errors.New(msgKeyNotFound) // ErrWrongType when a key is not the right type. ErrWrongType = errors.New(msgWrongType) // ErrIntValueError can returned by INCRBY ErrIntValueError = errors.New(msgInvalidInt) // ErrFloatValueError can returned by INCRBYFLOAT ErrFloatValueError = errors.New(msgInvalidFloat) )
Functions ¶
This section is empty.
Types ¶
type RediQueue ¶
RediQueue is a Redis server implementation.
func NewRediQueue ¶
func NewRediQueue() *RediQueue
NewRediQueue makes a new, non-started, RediQueue object.
func (*RediQueue) Addr ¶
Addr returns '127.0.0.1:12345'. Can be given to a Dial(). See also Host() and Port(), which return the same things.
func (*RediQueue) CheckList ¶
CheckList does not call Errorf() iff there is a list key with the expected values. Normal use case is `m.CheckGet(t, "favorite_colors", "red", "green", "infrared")`.
func (*RediQueue) CheckSet ¶
CheckSet does not call Errorf() iff there is a set key with the expected values. Normal use case is `m.CheckSet(t, "visited", "Rome", "Stockholm", "Dublin")`.
func (*RediQueue) CommandCount ¶
CommandCount returns the number of processed commands.
func (*RediQueue) CurrentConnectionCount ¶
CurrentConnectionCount returns the number of currently connected clients.
func (*RediQueue) Del ¶
Del deletes a key and any expiration value. Returns whether there was a key.
func (*RediQueue) FlushAll ¶
func (m *RediQueue) FlushAll()
FlushAll removes all keys from all databases.
func (*RediQueue) FlushDB ¶
func (m *RediQueue) FlushDB()
FlushDB removes all keys from the selected database.
func (*RediQueue) List ¶
List returns the list k, or an error if it's not there or something else. This is the same as the Redis command `LRANGE 0 -1`, but you can do your own range-ing.
func (*RediQueue) Push ¶
Push add element at the end. Is called RPUSH in redis. Returns the new length.
func (*RediQueue) RequireAuth ¶
RequireAuth makes every connection need to AUTH first. Disable again by setting an empty string.
func (*RediQueue) Restart ¶
Restart restarts a Close()d server on the same port. Values will be preserved.
func (*RediQueue) SetTime ¶
SetTime sets the time against which EXPIREAT values are compared. EXPIREAT will use time.Now() if this is not set.
func (*RediQueue) Start ¶
Start starts a server. It listens on a random port on localhost. See also Addr().
func (*RediQueue) StartAddr ¶
StartAddr runs rediqueue with a given addr. Examples: "127.0.0.1:6379", ":6379", or "127.0.0.1:0"
func (*RediQueue) TotalConnectionCount ¶
TotalConnectionCount returns the number of client connections since server start.
type RedisDB ¶
type RedisDB struct {
// contains filtered or unexported fields
}
RedisDB holds a single (numbered) Redis database.
func (*RedisDB) List ¶
List returns the list k, or an error if it's not there or something else. This is the same as the Redis command `LRANGE 0 -1`, but you can do your own range-ing.
func (*RedisDB) Push ¶
Push add element at the end. Is called RPUSH in redis. Returns the new length.