go-redis / redismock

Redis client Mock
https://redis.uptrace.dev
BSD 2-Clause "Simplified" License
272 stars 59 forks source link

Cannot properly test pipeline #30

Open patrick-janeiro opened 2 years ago

patrick-janeiro commented 2 years ago

I am writing unit tests to test our pipeline code. We ran into an issue that a BeforePipelineProcess hook is propagating the first error to all CMDs.

Example code:

` redisDB, redisMock := redismock.NewClientMock() redisMock.ExpectGet("key1").SetErr(Errors.New("error")) redisMock.ExpectGet("key2").SetVal("{\"key\":\"key2\",\"value\":\"value2\"}")

`

when using this setup for my mock client with the following code:

pipeline := c.client.Pipeline() defer pipeline.Close() redisResults := make([]*redis.StringCmd, len(keys)) for i, key := range keys { redisResults[i] = pipeline.Get(ctx, key) } _, _ = pipeline.Exec(ctx)

I see that redisResults has "error" for all stringCmd results, instead of just the first CMD for key1.

We tested the same code with a redis instance and got the expected result of the first cmd returning an error but the second being nil.

Maimonator commented 2 years ago

I experience the same error

korprulu commented 1 year ago

this problem is made by the go-redis, take a look at code here