Skip to content

Commit

Permalink
Merge pull request #22 from pyrocat101/reuseportTests
Browse files Browse the repository at this point in the history
add some tests for SO_REUSEPORT
  • Loading branch information
dzbarsky authored Aug 4, 2024
2 parents 314181a + a3f389f commit 44236d6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
31 changes: 30 additions & 1 deletion tests/so_reuseport/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
load("@rules_itest//:itest.bzl", "itest_service")
load("@aspect_rules_js//js:defs.bzl", "js_test")
load("@rules_itest//:itest.bzl", "itest_service", "service_test")
load("@rules_go//go:def.bzl", "go_test")
load("//:must_fail.bzl", "must_fail")

NOT_WINDOWS = select({
Expand All @@ -17,6 +19,9 @@ itest_service(
exe = "//go_service",
health_check_timeout = "5s",
http_health_check_address = "http://127.0.0.1:$${PORT}",
named_ports = [
"named_port1",
],
so_reuseport_aware = True,
target_compatible_with = NOT_WINDOWS,
)
Expand All @@ -43,3 +48,27 @@ must_fail(
target_compatible_with = NOT_WINDOWS,
test = "_no_reuseport_service_hygiene_test",
)

go_test(
name = "_so_reuseport_test",
srcs = ["so_reuseport_test.go"],
tags = ["manual"],
)

service_test(
name = "so_reuseport_test",
services = [":reuseport_service"],
test = ":_so_reuseport_test",
)

js_test(
name = "_so_reuseport_js_test",
entry_point = "so_reuseport_test.mjs",
tags = ["manual"],
)

service_test(
name = "so_reuseport_js_test",
services = [":reuseport_service"],
test = ":_so_reuseport_js_test",
)
39 changes: 39 additions & 0 deletions tests/so_reuseport/so_reuseport_test.go
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)
}
}
22 changes: 22 additions & 0 deletions tests/so_reuseport/so_reuseport_test.mjs
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),
});
}

0 comments on commit 44236d6

Please sign in to comment.