-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from pyrocat101/reuseportTests
add some tests for SO_REUSEPORT
- Loading branch information
Showing
3 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package so_reuseport | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"net" | ||
) | ||
|
||
func TestNoSoResusePort(t *testing.T) { | ||
portNames := []string{ | ||
"@@//so_reuseport:reuseport_service", | ||
"@@//so_reuseport:reuseport_service:named_port1", | ||
} | ||
|
||
t.Logf("ASSIGNED_PORTS: %v", os.Getenv("ASSIGNED_PORTS")) | ||
|
||
for _, portName := range portNames { | ||
t.Logf("Testing port: %s", portName) | ||
cmd := exec.Command(os.Getenv("GET_ASSIGNED_PORT_BIN"), portName) | ||
port, err := cmd.CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("Failed to get port: %v", err) | ||
} | ||
|
||
if len(port) == 0 { | ||
t.Fatalf("Cannot get port") | ||
} | ||
t.Logf("Port assigned: %s", port) | ||
|
||
// Should fail because we didn't set SO_REUSEPORT | ||
_, err = net.Listen("tcp", "127.0.0.1:"+string(port)) | ||
if err == nil { | ||
t.Fatalf("Expected error, got nil") | ||
} | ||
t.Logf("Expected error: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import assert from 'assert/strict'; | ||
import http from 'http'; | ||
import process from 'process'; | ||
|
||
const ports = JSON.parse(process.env.ASSIGNED_PORTS) | ||
|
||
for (const portName of [ | ||
"@@//so_reuseport:reuseport_service", | ||
"@@//so_reuseport:reuseport_service:named_port1", | ||
]) { | ||
const port = ports[portName]; | ||
assert(port); | ||
|
||
const server = http.createServer(() => {}); | ||
server.on('error', (err) => { | ||
assert.equal(err.code, 'EADDRINUSE'); | ||
}); | ||
server.listen({ | ||
host: '127.0.0.1', | ||
port: parseInt(port, 10), | ||
}); | ||
} |