alicebob / miniredis

Pure Go Redis server for Go unittests
MIT License
3.1k stars 217 forks source link

Allow redis server to connect on specified Port number instead of Random Port #370

Closed amanhasank closed 7 months ago

alicebob commented 7 months ago
// StartAddr runs miniredis with a given addr. Examples: "127.0.0.1:6379",
// ":6379", or "127.0.0.1:0"
func (m *Miniredis) StartAddr(addr string) error {
    s, err := server.NewServer(addr)
    if err != nil {
        return err
    }
    return m.start(s)
}

Should do what you want.

amanhasank commented 7 months ago

Hi, i am trying to use StartAddr, Please see below code snippet

func TestSomething(t *testing.T) {

    // Run the miniredis server
    s := miniredis.NewMiniRedis()
    port := 6370
    s.StartAddr(fmt.Sprintf(":%d", port))
    err := s.Start()
    if err != nil {
        fmt.Println("Error starting Miniredis:", err)
        return
    }
    }

but i am getting === RUN TestSomething Error starting Miniredis: listen tcp 127.0.0.1:6370: bind: address already in use

even though this port is free lsof -i :6370

i have also tried using different port numbers still the same error, Am i using it correctly or something is missed here?

alicebob commented 7 months ago

Thanks, that sounds weird. Can't have a look right now but will try soon(ish).

amanhasank commented 7 months ago

thanks.

alicebob commented 7 months ago
    s.StartAddr(fmt.Sprintf(":%d", port))
    err := s.Start()

you only need one of the Start... calls. So remove the s.Start() call and then it's:

    err := s.StartAddr(fmt.Sprintf(":%d", port))
amanhasank commented 7 months ago

That Works, thanks a lot.