Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty result in HGET operation in TxPipelined #28

Closed
bereal opened this issue Feb 4, 2022 · 1 comment
Closed

Empty result in HGET operation in TxPipelined #28

bereal opened this issue Feb 4, 2022 · 1 comment

Comments

@bereal
Copy link

bereal commented Feb 4, 2022

Hello, I have already asked the question about it on StackOverflow, but I have a strong suspicion that it may be a bug. I'm mocking an HGET operation, but when I call this operation inside a TxPipelined, the result is empty. It doesn't return an error either, like it would do if this was a mock expectation setup error. Here is the code that reproduces my problem:

package test

import (
    "context"
    "testing"
    "github.com/go-redis/redis/v8"
    "github.com/go-redis/redismock/v8"
)

func TestMock(t *testing.T) {
    ctx := context.TODO()

    db, mock := redismock.NewClientMock()
    mock.ExpectWatch("key")
    mock.ExpectTxPipeline()
    mock.ExpectHGet("key", "field").SetVal("VALUE")

    var result string
    db.Watch(ctx, func(tx *redis.Tx) error {
        _, err := tx.TxPipelined(ctx, func(p redis.Pipeliner) error {
            // Here I'd expect r to be "VALUE"
            r, err := p.HGet(ctx, "key", "field").Result()
            t.Logf("Result: '%s', error: %+v", r, err)
            result = r
            return err
        })

        return err
    }, "key")

    if result != "VALUE" {
        t.Errorf("Expected result to be VALUE, got %s", result)
    }
}

The test fails with the error:

--- FAIL: TestMock (0.00s)
    redis_test.go:26: Result: '', error: <nil>
    redis_test.go:36: Expected result to be VALUE, got ''

Without a pipeline, the code works like expected.

@bereal bereal closed this as completed Feb 4, 2022
@bereal
Copy link
Author

bereal commented Feb 4, 2022

I realized that I was calling HGet on p instead of tx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant