Skip to content

Commit

Permalink
update test case
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyu committed Jan 8, 2014
1 parent aa319cc commit 31df4a1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
14 changes: 10 additions & 4 deletions pubsub_test.go
Expand Up @@ -25,7 +25,7 @@ func TestSubscribe(t *testing.T) {
return
}
for {
list, err := sub.Recv()
list, err := sub.Receive()
if err != nil {
t.Error(err)
quit <- true
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestPSubscribe(t *testing.T) {
return
}
for {
list, err := psub.Recv()
list, err := psub.Receive()
if err != nil {
t.Error(err)
quit <- true
Expand Down Expand Up @@ -90,7 +90,9 @@ func TestUnSubscribe(t *testing.T) {
defer sub.Close()
go func() {
for {
sub.Recv()
if _, err := sub.Receive(); err != nil {
t.Error(err)
}
ch <- true
}
}()
Expand All @@ -106,6 +108,7 @@ func TestUnSubscribe(t *testing.T) {
}
time.Sleep(100 * time.Millisecond)
<-ch
time.Sleep(100 * time.Millisecond)
if len(sub.Channels) != 0 {
t.Fail()
}
Expand All @@ -120,7 +123,9 @@ func TestPUnSubscribe(t *testing.T) {
defer sub.Close()
go func() {
for {
sub.Recv()
if _, err := sub.Receive(); err != nil {
t.Error(err)
}
ch <- true
}
}()
Expand All @@ -136,6 +141,7 @@ func TestPUnSubscribe(t *testing.T) {
}
time.Sleep(100 * time.Millisecond)
<-ch
time.Sleep(100 * time.Millisecond)
if len(sub.Patterns) != 0 {
t.Fail()
}
Expand Down
13 changes: 4 additions & 9 deletions redis.go
Expand Up @@ -1403,10 +1403,6 @@ func (r *Redis) Publish(channel, message string) (int64, error) {
return rp.IntegerValue()
}

func (r *Redis) PubSub() (*PubSub, error) {
return newPubSub(r)
}

// QUIT
// Ask the server to close the connection.
// The connection is closed as soon as all pending replies have been written to the client.
Expand Down Expand Up @@ -2188,18 +2184,17 @@ type PubSub struct {
Channels map[string]bool
}

func newPubSub(r *Redis) (*PubSub, error) {
func (r *Redis) PubSub() (*PubSub, error) {
c, err := r.openConnection()
if err != nil {
return nil, err
}
p := &PubSub{
return &PubSub{
redis: r,
conn: c,
Patterns: make(map[string]bool),
Channels: make(map[string]bool),
}
return p, nil
}, nil
}

func (p *PubSub) Close() error {
Expand All @@ -2217,7 +2212,7 @@ func (p *PubSub) Close() error {
// and the client can issue any kind of Redis command as we are outside the Pub/Sub state.
// message: it is a message received as result of a PUBLISH command issued by another client.
// The second element is the name of the originating channel, and the third argument is the actual message payload.
func (p *PubSub) Recv() ([]string, error) {
func (p *PubSub) Receive() ([]string, error) {
rp, err := p.conn.RecvReply()
if err != nil {
return nil, err
Expand Down
26 changes: 8 additions & 18 deletions scripting_test.go
Expand Up @@ -8,43 +8,33 @@ func TestEval(t *testing.T) {
rp, err := r.Eval("return {KEYS[1], KEYS[2], ARGV[1], ARGV[2]}", []string{"key1", "key2"}, []string{"arg1", "arg2"})
if err != nil {
t.Error(err)
}
l, err := rp.ListValue()
if err != nil {
} else if l, err := rp.ListValue(); err != nil {
t.Error(err)
}
if l[0] != "key1" || l[3] != "arg2" {
} else if l[0] != "key1" || l[3] != "arg2" {
t.Fail()
}
rp, err = r.Eval("return redis.call('set','foo','bar')", nil, nil)
if err != nil {
t.Error(err)
}
if err := rp.OKValue(); err != nil {
} else if err := rp.OKValue(); err != nil {
t.Error(err)
}
rp, err = r.Eval("return 10", nil, nil)
if err != nil {
t.Error(err)
}
n, err := rp.IntegerValue()
if err != nil {
} else if n, err := rp.IntegerValue(); err != nil {
t.Error(err)
}
if n != 10 {
} else if n != 10 {
t.Fail()
}
rp, err = r.Eval("return {1,2,{3,'Hello World!'}}", nil, nil)
if err != nil {
t.Error(err)
}
if len(rp.Multi) != 3 {
} else if len(rp.Multi) != 3 {
t.Fail()
}
if rp.Multi[2].Multi[0].Integer != 3 {
} else if rp.Multi[2].Multi[0].Integer != 3 {
t.Fail()
}
if s, err := rp.Multi[2].Multi[1].StringValue(); err != nil || s != "Hello World!" {
} else if s, err := rp.Multi[2].Multi[1].StringValue(); err != nil || s != "Hello World!" {
t.Fail()
}
}
Expand Down

0 comments on commit 31df4a1

Please sign in to comment.