Refactor tests to use local test listeners instead of external I2P sites for improved stability and reliability; add TestListener helper for managing local I2P listeners in tests.

This commit is contained in:
eyedeekay
2025-10-06 13:57:38 -04:00
parent e185fe208c
commit 79fffdde66
5 changed files with 459 additions and 50 deletions

View File

@@ -24,6 +24,11 @@ func Test_StreamingDial(t *testing.T) {
return
}
fmt.Println("Test_StreamingDial")
// Set up a local test listener instead of using external site
testListener := SetupTestListenerWithHTTP(t, generateUniqueSessionID("streaming_dial_listener"))
defer testListener.Close()
sam, err := NewSAM(SAMDefaultAddr(""))
if err != nil {
fmt.Println(err.Error())
@@ -44,16 +49,9 @@ func Test_StreamingDial(t *testing.T) {
t.Fail()
return
}
fmt.Println("\tNotice: This may fail if your I2P node is not well integrated in the I2P network.")
fmt.Println("\tLooking up i2p-projekt.i2p")
forumAddr, err := sam.Lookup("i2p-projekt.i2p")
if err != nil {
fmt.Println(err.Error())
t.Fail()
return
}
fmt.Println("\tDialing i2p-projekt.i2p(", forumAddr.Base32(), forumAddr.DestHash().Hash(), ")")
conn, err := ss.DialI2P(forumAddr)
fmt.Println("\tNotice: Using local test listener instead of external I2P site for improved test stability.")
fmt.Printf("\tDialing test listener (%s)\n", testListener.AddrString())
conn, err := ss.DialI2P(testListener.Addr())
if err != nil {
fmt.Println(err.Error())
t.Fail()
@@ -69,9 +67,9 @@ func Test_StreamingDial(t *testing.T) {
buf := make([]byte, 4096)
n, err := conn.Read(buf)
if !strings.Contains(strings.ToLower(string(buf[:n])), "http") && !strings.Contains(strings.ToLower(string(buf[:n])), "html") {
fmt.Printf("\tProbably failed to StreamSession.DialI2P(i2p-projekt.i2p)? It replied %d bytes, but nothing that looked like http/html", n)
fmt.Printf("\tProbably failed to StreamSession.DialI2P(test listener)? It replied %d bytes, but nothing that looked like http/html", n)
} else {
fmt.Println("\tRead HTTP/HTML from i2p-projekt.i2p")
fmt.Println("\tRead HTTP/HTML from test listener")
}
}
@@ -158,7 +156,7 @@ func Test_StreamingServerClient(t *testing.T) {
}
func ExampleStreamSession() {
// Creates a new StreamingSession, dials to idk.i2p and gets a SAMConn
// Creates a new StreamingSession, dials to a local test listener and gets a SAMConn
// which behaves just like a normal net.Conn.
//
// Requirements: This example requires a running I2P router with SAM bridge enabled.
@@ -182,15 +180,18 @@ func ExampleStreamSession() {
fmt.Printf("Failed to create stream session: %v", err)
return
}
someone, err := sam.Lookup("idk.i2p")
// Note: In a real example, you would set up a test listener here
// For demonstration purposes, we'll use a placeholder destination
someone, err := sam.Lookup("test.i2p")
if err != nil {
fmt.Printf("Failed to lookup idk.i2p: %v", err)
fmt.Printf("Failed to lookup test destination: %v", err)
return
}
conn, err := ss.DialI2P(someone)
if err != nil {
fmt.Printf("Failed to dial idk.i2p: %v", err)
fmt.Printf("Failed to dial test destination: %v", err)
return
}
defer conn.Close()
@@ -206,17 +207,17 @@ func ExampleStreamSession() {
return
}
if !strings.Contains(strings.ToLower(string(buf[:n])), "http") && !strings.Contains(strings.ToLower(string(buf[:n])), "html") {
fmt.Printf("Failed to get HTTP/HTML response from idk.i2p (got %d bytes)", n)
fmt.Printf("Failed to get HTTP/HTML response from test destination (got %d bytes)", n)
return
} else {
fmt.Println("Read HTTP/HTML from idk.i2p")
log.Println("Read HTTP/HTML from idk.i2p")
fmt.Println("Read HTTP/HTML from test destination")
log.Println("Read HTTP/HTML from test destination")
}
return
// Output:
// Sending HTTP GET /
// Read HTTP/HTML from idk.i2p
// Read HTTP/HTML from test destination
}
func ExampleStreamListener() {