Skip to content

Commit

Permalink
tor: fix TestReconnectSucceed
Browse files Browse the repository at this point in the history
We now make sure the proxy server is running on a unique port. In
addition, we close the old conn before making a new conn.
  • Loading branch information
yyforyongyu committed Mar 5, 2025
1 parent f744a54 commit e7ff4e3
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions tor/controller_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package tor

import (
"fmt"
"net"
"net/textproto"
"testing"

"github.com/lightningnetwork/lnd/lntest/port"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -116,11 +118,9 @@ func (tp *testProxy) cleanUp() {
// creates a server and a client connection, and initializes a testProxy using
// these params.
func createTestProxy(t *testing.T) *testProxy {
// Set up the proxy to listen on given port.
//
// NOTE: we use a port 0 here to indicate we want a free port selected
// by the system.
proxy, err := net.Listen("tcp", ":0")
// Set up the proxy to listen on a unique port.
addr := fmt.Sprintf(":%d", port.NextAvailablePort())
proxy, err := net.Listen("tcp", addr)
require.NoError(t, err, "failed to create proxy")

t.Logf("created proxy server to listen on address: %v", proxy.Addr())
Expand All @@ -145,6 +145,9 @@ func createTestProxy(t *testing.T) *testProxy {
clientConn: client,
}

t.Logf("server listening on %v, client listening on %v",
tc.serverConn.LocalAddr(), tc.serverConn.RemoteAddr())

return tc
}

Expand Down Expand Up @@ -309,6 +312,9 @@ func TestReconnectSucceed(t *testing.T) {
controlAddr: proxy.serverAddr,
}

// Close the old conn before reconnection.
require.NoError(t, proxy.serverConn.Close())

// Accept the connection inside a goroutine. We will also write some
// data so that the reconnection can succeed. We will mock three writes
// and two reads inside our proxy server,
Expand All @@ -322,6 +328,9 @@ func TestReconnectSucceed(t *testing.T) {
server, err := proxy.server.Accept()
require.NoError(t, err, "failed to accept")

t.Logf("server listening on %v, client listening on %v",
server.LocalAddr(), server.RemoteAddr())

// Write the protocol info.
resp := "250-PROTOCOLINFO 1\n" +
"250-AUTH METHODS=NULL\n" +
Expand Down

0 comments on commit e7ff4e3

Please sign in to comment.