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

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

Closed
amanhasank opened this issue Apr 2, 2024 · 6 comments

Comments

@amanhasank
Copy link

No description provided.

@alicebob
Copy link
Owner

alicebob commented Apr 2, 2024

// 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
Copy link
Author

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
Copy link
Owner

alicebob commented Apr 4, 2024 via email

@amanhasank
Copy link
Author

thanks.

@alicebob
Copy link
Owner

alicebob commented Apr 4, 2024

	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
Copy link
Author

That Works, thanks a lot.

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

2 participants