Skip to content

Commit

Permalink
fix #5 support client.Watch
Browse files Browse the repository at this point in the history
Signed-off-by: monkey <monkey92t@gmail.com>
  • Loading branch information
monkey92t committed Jan 5, 2021
1 parent f79eb0b commit 8f1a0fe
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
12 changes: 12 additions & 0 deletions example/example.go
Expand Up @@ -103,4 +103,16 @@ func example() {
_ = pipe.Get("key")
_ = pipe.Set("key", "value", 1)
_, _ = pipe.Exec()

//Watch
mock.ExpectWatch("key1", "key2").SetErr(errors.New("watch error"))
mock.ExpectGet("key1").SetVal("1")
mock.ExpectSet("key2", "2", 1*time.Second).SetVal("OK")

//err := db.Watch(func(tx *redis.Tx) error {
// tx.Get("key1")
// tx.Set("key2", "2", 1 * time.Second)
// return nil
//}, "key1", "key2")
//reflect.DeepEqual(err, errors.New("watch error"))
}
10 changes: 10 additions & 0 deletions expect.go
Expand Up @@ -30,6 +30,8 @@ type ClientMock interface {
ExpectTxPipeline()
ExpectTxPipelineExec() *ExpectedSlice

ExpectWatch(keys ...string) *ExpectedError

ExpectCommand() *ExpectedCommandsInfo
ExpectClientGetName() *ExpectedString
ExpectEcho(message interface{}) *ExpectedString
Expand Down Expand Up @@ -885,3 +887,11 @@ func (cmd *ExpectedGeoLocation) SetVal(val []redis.GeoLocation) {
func (cmd *ExpectedGeoLocation) inflow(c redis.Cmder) {
inflow(c, "locations", cmd.locations)
}

//----------------------

type ExpectedError struct {
expectedBase
}

func (cmd *ExpectedError) inflow(_ redis.Cmder) {}
13 changes: 13 additions & 0 deletions mock.go
Expand Up @@ -261,6 +261,19 @@ func (m *mock) ExpectTxPipelineExec() *ExpectedSlice {
return e
}

func (m *mock) ExpectWatch(keys ...string) *ExpectedError {
e := &ExpectedError{}
args := make([]interface{}, 1+len(keys))
args[0] = "watch"
for i, key := range keys {
args[1+i] = key
}
e.cmd = redis.NewStatusCmd(args...)
e.setVal = true
m.pushExpect(e)
return e
}

func (m *mock) ExpectCommand() *ExpectedCommandsInfo {
e := &ExpectedCommandsInfo{}
e.cmd = m.factory.Command()
Expand Down
66 changes: 66 additions & 0 deletions mock_test.go
Expand Up @@ -135,6 +135,72 @@ var _ = Describe("RedisMock", func() {
})
})

Describe("watch", func() {
BeforeEach(func() {
mock.ExpectWatch("key1", "key2")
mock.ExpectGet("key1").SetVal("1")
mock.ExpectSet("key2", "2", 1*time.Second).SetVal("OK")
})

It("watch error", func() {
mock.MatchExpectationsInOrder(false)
txf := func(tx *redis.Tx) error {
_ = tx.Get("key1")
_ = tx.Set("key2", "2", 1*time.Second)
return errors.New("watch tx error")
}

err := client.Watch(txf, "key1", "key2")
Expect(err).To(Equal(errors.New("watch tx error")))

mock.ExpectWatch("key3", "key4").SetErr(errors.New("watch error"))
txf = func(tx *redis.Tx) error {
return nil
}

err = client.Watch(txf, "key3", "key4")
Expect(err).To(Equal(errors.New("watch error")))
})

It("watch in order", func() {
mock.MatchExpectationsInOrder(true)
txf := func(tx *redis.Tx) error {
val, err := tx.Get("key1").Int64()
if err != nil {
return err
}
Expect(val).To(Equal(int64(1)))
err = tx.Set("key2", "2", 1*time.Second).Err()
if err != nil {
return err
}
return nil
}

err := client.Watch(txf, "key1", "key2")
Expect(err).NotTo(HaveOccurred())
})

It("watch out of order", func() {
mock.MatchExpectationsInOrder(false)
txf := func(tx *redis.Tx) error {
err := tx.Set("key2", "2", 1*time.Second).Err()
if err != nil {
return err
}
val, err := tx.Get("key1").Int64()
if err != nil {
return err
}
Expect(val).To(Equal(int64(1)))
return nil
}

err := client.Watch(txf, "key1", "key2")
Expect(err).NotTo(HaveOccurred())
})
})

Describe("work order", func() {

BeforeEach(func() {
Expand Down

0 comments on commit 8f1a0fe

Please sign in to comment.