Skip to content

Commit

Permalink
Add default port to address if missing
Browse files Browse the repository at this point in the history
  • Loading branch information
frosenberg committed Nov 6, 2015
1 parent 5dc15b1 commit 09e58ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 10 additions & 1 deletion transport/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/mediocregopher/radix.v2/pool"
"github.com/mediocregopher/radix.v2/pubsub"
"strings"
"regexp"
)

//
Expand All @@ -28,8 +29,16 @@ type RedisTransport struct {
func NewRedisTransport(address string, inputBinding string, outputBinding string) *RedisTransport {

// set some reasonable defaults
if address == "" {
if address == "" || address == ":6379" {
address = "localhost:6379"
} else { // check if it has a port

match, _ := regexp.MatchString("^.+:\\d+$", address)
if !match {
log.Debugf("Appending default redis port :6379 to %s", address)
address = address + ":6379"
}

}
if inputBinding == "" {
inputBinding = "input"
Expand Down
18 changes: 17 additions & 1 deletion transport/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@ func init() {
defer cleanupRedis(client)
}

func TestNewRedisTransportEmpty(t *testing.T) {
func TestNewRedisTransportEmpty0(t *testing.T) {
r := NewRedisTransport(":6379", "", "")

if r.Address != "localhost:6379" {
t.Fatalf("Unexpected redis address: ", r.Address)
}
}

func TestNewRedisTransportEmpty1(t *testing.T) {
r := NewRedisTransport("localhost", "", "")

if r.Address != "localhost:6379" {
t.Fatalf("Unexpected redis address: ", r.Address)
}
}

func TestNewRedisTransportEmpty2(t *testing.T) {
r := NewRedisTransport("", "", "")

if r.Address != "localhost:6379" {
Expand Down

0 comments on commit 09e58ec

Please sign in to comment.