Resolve IRC client hostnames off the accept loop (closes #156)
check / check (push) Waiting to run
check / check (push) Waiting to run
acceptLoop called newConn, which did the reverse DNS lookup, with its three-second timeout, before the loop could accept the next connection, so one client whose address had no answering reverse DNS held up every other IRC connect. serve now does the lookup at its start, on the connection's own goroutine, and the accept loop only accepts. The server keeps the lookup function in a field, as it does the ping times, so that a test can give it one that blocks. The new test checks that a second client registers while the first one's lookup has not returned. Model: opus-5-5
This commit was merged in pull request #172.
This commit is contained in:
@@ -2,6 +2,7 @@ package ircserver_test
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"errors"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -90,6 +92,21 @@ func newTestEnvWithPingTimes(
|
||||
) *testEnv {
|
||||
t.Helper()
|
||||
|
||||
return newTestEnvWithResolver(
|
||||
t, cfg, interval, deadline, ircserver.ResolveHost,
|
||||
)
|
||||
}
|
||||
|
||||
// newTestEnvWithResolver is newTestEnvWithPingTimes, except
|
||||
// that the server looks up a client's hostname with resolve.
|
||||
func newTestEnvWithResolver(
|
||||
t *testing.T,
|
||||
cfg *config.Config,
|
||||
interval, deadline time.Duration,
|
||||
resolve func(ctx context.Context, addr string) string,
|
||||
) *testEnv {
|
||||
t.Helper()
|
||||
|
||||
// A random name, so that no other test environment, not
|
||||
// even an earlier run of the same test, can share it.
|
||||
dsn := fmt.Sprintf(
|
||||
@@ -147,7 +164,7 @@ func newTestEnvWithPingTimes(
|
||||
))
|
||||
|
||||
srv := ircserver.NewTestServer(
|
||||
log, cfg, database, brk, interval, deadline,
|
||||
log, cfg, database, brk, interval, deadline, resolve,
|
||||
)
|
||||
|
||||
err = srv.Start(addr)
|
||||
@@ -415,6 +432,49 @@ func TestRegistration(t *testing.T) {
|
||||
assertContains(t, lines, " 001 ", "RPL_WELCOME")
|
||||
}
|
||||
|
||||
// TestRegistrationWhileAnotherLookupIsPending checks that a
|
||||
// client is accepted and registered while the hostname lookup
|
||||
// of an earlier client has not returned, and that it
|
||||
// registers with the hostname its own lookup returned.
|
||||
func TestRegistrationWhileAnotherLookupIsPending(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const lookedUpHost = "client.example"
|
||||
|
||||
var lookups atomic.Int32
|
||||
|
||||
firstLookupStarted := make(chan struct{})
|
||||
|
||||
// The first lookup returns only when the server stops.
|
||||
resolve := func(ctx context.Context, _ string) string {
|
||||
if lookups.Add(1) == 1 {
|
||||
close(firstLookupStarted)
|
||||
<-ctx.Done()
|
||||
}
|
||||
|
||||
return lookedUpHost
|
||||
}
|
||||
|
||||
env := newTestEnvWithResolver(
|
||||
t, &config.Config{ServerName: testServerName},
|
||||
ircserver.PingInterval, ircserver.PongDeadline, resolve,
|
||||
)
|
||||
|
||||
env.dial(t)
|
||||
|
||||
select {
|
||||
case <-firstLookupStarted:
|
||||
case <-time.After(testTimeout):
|
||||
t.Fatal("the first client's lookup did not start")
|
||||
}
|
||||
|
||||
lines := env.dial(t).register("second")
|
||||
assertContains(
|
||||
t, lines, "second!second@"+lookedUpHost,
|
||||
"RPL_WELCOME with the looked-up hostname",
|
||||
)
|
||||
}
|
||||
|
||||
// TestEachTestEnvHasItsOwnDatabase checks that a test
|
||||
// environment starts on an empty database while another one
|
||||
// is still open.
|
||||
|
||||
Reference in New Issue
Block a user