Skip to content

Commit

Permalink
BUGFIX / ADD async client connect fix and new asynchclient_test.go - …
Browse files Browse the repository at this point in the history
…TESTED

	- FIX BUG client did not return connect errors
	- ADD test file for async clients (WIP)
	- MOD move common test funcs to 0_test.go
	- MOD use _test prefix for test util funcs
  • Loading branch information
Joubin Houshyar committed Sep 14, 2012
1 parent d94b330 commit c593219
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 64 deletions.
53 changes: 53 additions & 0 deletions 0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ const (

)

// ----------------------------------------------------------------------
// test data setup
// ----------------------------------------------------------------------

// connection spec used in tests, using db 13 and password 'go-redis'.
// db 13 will be repeatedly flushed, as noted elsewhere.
func _test_getDefConnSpec() *ConnectionSpec {

host := "localhost"
port := 6379
db := 13
password := "go-redis"

connspec := DefaultSpec().Host(host).Port(port).Db(db).Password(password)
return connspec
}

// Its a hack to use the testing/quick functions to load up some
// test data. And that is it.
//
Expand Down Expand Up @@ -63,3 +80,39 @@ func _test_getDefaultClient() (Client, error) {
spec.Db(13).Password("go-redis")
return NewSynchClientWithSpec(spec)
}

// ----------------------------------------------------------------------
// test utility functions
// ----------------------------------------------------------------------

func _test_compareStringArrays(got, expected []string) bool {
if len(got) != len(expected) {
return false
}
for i, b := range expected {
if got[i] != b {
return false
}
}
return true
}

func _test_reverseBytes(in []byte) (out []byte) {
size := len(in)
out = make([]byte, size)
for i, v := range in {
out[size-i-1] = v
}
return
}
func _test_compareByteArrays(got, expected []byte) bool {
if len(got) != len(expected) {
return false
}
for i, b := range expected {
if got[i] != b {
return false
}
}
return true
}
11 changes: 5 additions & 6 deletions asynchclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,22 @@ type asyncClient struct {
//
func NewAsynchClient() (c AsyncClient, err Error) {
spec := DefaultSpec()
c, err = NewAsynchClientWithSpec(spec)
return
return NewAsynchClientWithSpec(spec)
}

// Create a new asynClient and connects to the Redis server using the
// specified ConnectionSpec.
//
func NewAsynchClientWithSpec(spec *ConnectionSpec) (c AsyncClient, err Error) {
_c := new(asyncClient)
_c.conn, err = NewAsynchConnection(spec)
func NewAsynchClientWithSpec(spec *ConnectionSpec) (client AsyncClient, err Error) {
c := new(asyncClient)
c.conn, err = NewAsynchConnection(spec)
if err != nil {
if debug() {
log.Println("NewAsyncConnection() raised error: ", err)
}
return nil, err
}
return _c, nil
return c, nil
}

// -----------------------------------------------------------------------------
Expand Down
39 changes: 39 additions & 0 deletions asynchclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package redis

import (
// "fmt"
"log"
"testing"
)

// Check that connection is actually passing passwords from spec
// and catching AUTH ERRs.
func TestAsyncClientConnectWithBadSpec(t *testing.T) {
spec := _test_getDefConnSpec()
spec.Password("bad-password")
client, expected := NewAsynchClientWithSpec(spec)
if expected == nil {
t.Error("BUG: Expected a RedisError")
}
if client != nil {
t.Error("BUG: async client reference on error MUST be nil")
}
}

// Check that connection is actually passing passwords from spec
func TestAsyncClientConnectWithSpec(t *testing.T) {
spec := _test_getDefConnSpec()

client, err := NewAsynchClientWithSpec(spec)
if err != nil {
t.Error("failed to create client with spec. Error: ", err.Message())
} else if client == nil {
t.Error("BUG: client is nil")
}
client.Quit()
}

/* --------------- KEEP THIS AS LAST FUNCTION -------------- */
func TestEnd_asct(t *testing.T) {
log.Println("-- asynchclient test completed")
}
8 changes: 5 additions & 3 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,13 +444,15 @@ func newAsyncConnHdl(spec *ConnectionSpec) (async *asyncConnHdl, err Error) {

// Creates and opens a new AsyncConnection and starts the goroutines for
// request and response processing
// TODO: NewXConnection methods need to return redis.Error due to initial connect
// interaction with redis (AUTH &| SELECT)
func NewAsynchConnection(spec *ConnectionSpec) (conn AsyncConnection, err Error) {
var async *asyncConnHdl
if async, err = newAsyncConnHdl(spec); err == nil {
async.connect()
async.startup()
if err = async.connect(); err == nil {
async.startup()
} else {
async = nil // do not return a ref if we could not connect
}
}
return async, err
}
Expand Down
2 changes: 1 addition & 1 deletion error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ func TestNewErrorWithCause(t *testing.T) {
}

func TestEnd_et(t *testing.T) {
log.Println("error test")
log.Println("-- error test completed")
}
2 changes: 1 addition & 1 deletion future_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,5 @@ func TestFutureTimedBlockingGet(t *testing.T) {

func TestEnd_future(t *testing.T) {
// nop
log.Println("future test completed")
log.Println("-- future test completed")
}
2 changes: 1 addition & 1 deletion redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ type AsyncClient interface {

// psuedo inheritance to coerce to RedisClient type
// for the common API (not "algorithm", not "data", but interface ...)
RedisClient() RedisClient
RedisClient

// Redis GET command.
Get(key string) (result FutureBytes, err Error)
Expand Down
61 changes: 9 additions & 52 deletions synchclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ import (
"testing"
)

func _test_getDefConnSpec() *ConnectionSpec {

host := "localhost"
port := 6379
db := 13
password := "go-redis"

connspec := DefaultSpec().Host(host).Port(port).Db(db).Password(password)
return connspec
}

func flushAndQuitOnCompletion(t *testing.T, client Client) {
// flush it
e := client.Flushdb()
Expand All @@ -31,20 +20,20 @@ func flushAndQuitOnCompletion(t *testing.T, client Client) {

// Check that connection is actually passing passwords from spec
// and catching AUTH ERRs.
func TestClientConnectWithBadSpec(t *testing.T) {
func TestSyncClientConnectWithBadSpec(t *testing.T) {
spec := _test_getDefConnSpec()
spec.Password("bad-password")
client, expected := NewSynchClientWithSpec(spec)
if expected == nil {
t.Error("BUG: Expected a RedisError")
}
if client != nil {
t.Error("BUG: client reference on error MUST be nil")
t.Error("BUG: sync client reference on error MUST be nil")
}
}

// Check that connection is actually passing passwords from spec
func TestClientConnectWithSpec(t *testing.T) {
func TestSyncClientConnectWithSpec(t *testing.T) {
spec := _test_getDefConnSpec()

client, err := NewSynchClientWithSpec(spec)
Expand Down Expand Up @@ -149,15 +138,15 @@ func TestGetset(t *testing.T) {
t.Errorf("on Getset(%s, %s) - %s", k, newv, e)
}
// check previous prev value against expected v
if prev == nil || !compareByteArrays(prev, v) {
if prev == nil || !_test_compareByteArrays(prev, v) {
t.Errorf("on Getset(%s, %s) - got: %s", k, newv, prev)
}
// now check that newvalue was correctly set as well
got, e := client.Get(k)
if e != nil {
t.Errorf("on Getset(%s, %s) - %s", k, newv, e)
}
if !compareByteArrays(got, newv) {
if !_test_compareByteArrays(got, newv) {
t.Errorf("on Get(%s) - got: %s expected:%s", k, got, newv)
}

Expand All @@ -183,7 +172,7 @@ func TestSetThenGet(t *testing.T) {
if got == nil {
t.Errorf("on Get(%s) - got nil", k)
}
if !compareByteArrays(got, v) {
if !_test_compareByteArrays(got, v) {
t.Errorf("on Get(%s) - got:%s expected:%s", k, got, v)
}
}
Expand Down Expand Up @@ -212,7 +201,7 @@ func TestMget(t *testing.T) {
if len(vset) != 1 {
t.Errorf("on Mget(%s) - expected len(vset) == 1, got: %d", key, len(vset))
}
if !compareByteArrays(vset[0], []byte(vprefix)) {
if !_test_compareByteArrays(vset[0], []byte(vprefix)) {
t.Errorf("on Mget(%s) - expected %s, got: %s", key, vprefix, vset[0])
}

Expand All @@ -237,7 +226,7 @@ func TestMget(t *testing.T) {
}
for j := 1; j < len(vset); j++ {
expected := []byte(fmt.Sprintf("%s_%03d", vprefix, j-1))
if !compareByteArrays(vset[j], expected) {
if !_test_compareByteArrays(vset[j], expected) {
t.Errorf("on Mget(%s) - expected %s, got: %s", key, expected, vset[j])
}
}
Expand Down Expand Up @@ -467,39 +456,7 @@ func TestRename(t *testing.T) {
flushAndQuitOnCompletion(t, client)
}

func compareStringArrays(got, expected []string) bool {
if len(got) != len(expected) {
return false
}
for i, b := range expected {
if got[i] != b {
return false
}
}
return true
}

func reverseBytes(in []byte) (out []byte) {
size := len(in)
out = make([]byte, size)
for i, v := range in {
out[size-i-1] = v
}
return
}
func compareByteArrays(got, expected []byte) bool {
if len(got) != len(expected) {
return false
}
for i, b := range expected {
if got[i] != b {
return false
}
}
return true
}

/* --------------- KEEP THIS AS LAST FUNCTION -------------- */
func TestEnd_sct(t *testing.T) {
log.Println("synchclient test completed")
log.Println("-- synchclient test completed")
}

0 comments on commit c593219

Please sign in to comment.