alicebob / miniredis

Pure Go Redis server for Go unittests
MIT License
3.09k stars 215 forks source link

Special lastId=$ not working with XREAD #322

Closed gpt900 closed 1 year ago

gpt900 commented 1 year ago

How to reproduce:

package main

import (
    "context"
    "testing"
    "time"

    "github.com/alicebob/miniredis/v2"
    "github.com/go-redis/redis/v8"
)

func TestXreadFromFirst(t *testing.T) {
    s := miniredis.RunT(t)
    defer s.Close()

    client := redis.NewClusterClient(&redis.ClusterOptions{
        Addrs:     []string{s.Addr()},
    })

    client.XRead(context.Background(), &redis.XReadArgs{
        Streams: []string{"testStream", "0"},
        Count:   1,
        Block:   100*time.Millisecond,
    })
}

func TestXreadFromLast(t *testing.T) {
    s := miniredis.RunT(t)
    defer s.Close()

    client := redis.NewClusterClient(&redis.ClusterOptions{
        Addrs:     []string{s.Addr()},
    })

    client.XRead(context.Background(), &redis.XReadArgs{
        Streams: []string{"testStream", "$"},
        Count:   1,
        Block:   100*time.Millisecond,
    })
}

First case works as expected, but second fails with:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0xdabe19]

goroutine 22 [running]:
github.com/alicebob/miniredis/v2.(*streamKey).lastID(0xc0000d0080?)
    <local-path>/github.com/alicebob/miniredis/v2@v2.30.0/stream.go:79 +0x39
github.com/alicebob/miniredis/v2.(*Miniredis).cmdXread(0xc0000d0080, 0xc0002e6140, {0xc0001344b7, 0x5}, {0xc00011c490, 0x7, 0x7})
    <local-path>/github.com/alicebob/miniredis/v2@v2.30.0/cmd_stream.go:935 +0x76f
github.com/alicebob/miniredis/v2/server.(*Server).Dispatch(0xc0003db400, 0x0?, {0xc00011c480, 0x8, 0x8})
    <local-path>/github.com/alicebob/miniredis/v2@v2.30.0/server/server.go:217 +0x2ee
github.com/alicebob/miniredis/v2/server.(*Server).servePeer(0x0?, {0x11a5268?, 0xc00048e008})
    <local-path>/github.com/alicebob/miniredis/v2@v2.30.0/server/server.go:185 +0x3d1
github.com/alicebob/miniredis/v2/server.(*Server).ServeConn.func1()
    <local-path>/github.com/alicebob/miniredis/v2@v2.30.0/server/server.go:112 +0xb6
created by github.com/alicebob/miniredis/v2/server.(*Server).ServeConn
    <local-path>/github.com/alicebob/miniredis/v2@v2.30.0/server/server.go:108 +0x12a
alicebob commented 1 year ago

On Wed, Apr 12, 2023 at 01:32:38AM -0700, gpt900 wrote:

How to reproduce:

Thanks for the clear report, will check soon (just not today).

alicebob commented 1 year ago

Thanks, should be fixed now.