package xfer
import (
"net"
"os/exec"
"strings"
"testing"
"time"
)
// A child that exits before it listens fails at once. Waiting out the
// whole bound for a process that is gone would hold the request — and
// the repository — for nothing.
func TestAChildThatExitsIsNotWaitedFor(t *testing.T) {
l, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
port := l.Addr().(*net.TCPAddr).Port
l.Close() // nothing listens here now
exited := make(chan struct{})
close(exited)
start := time.Now()
err = waitReady(port, childReadyTimeout, exited)
if err == nil || !strings.Contains(err.Error(), "exited") {
t.Fatalf("FAIL: an exited child gave %v", err)
}
if d := time.Since(start); d > 2*time.Second {
t.Errorf("FAIL: an exited child was waited for %s", d)
}
}
// One repository starting its child does not hold up another's sync.
// Children were started under the front's single lock, so a slow start
// — the box runs CI beside the forge — stalled every repository, and
// the bound on it was short enough to answer a slow child with 502.
func TestOneRepositoryStartingDoesNotBlockAnother(t *testing.T) {
if _, err := exec.LookPath("zit"); err != nil {
t.Skip("zit not on PATH")
}
alpha, beta := t.TempDir(), t.TempDir()
zrun(t, alpha, "init")
zrun(t, beta, "init")
f := New()
defer f.Close()
f.Register("alpha", alpha)
f.Register("beta", beta)
// alpha mid-start: its own lock is held, as ensure holds it while
// its child comes up.
f.mu.Lock()
a := f.repos["alpha"]
f.mu.Unlock()
a.mu.Lock()
defer a.mu.Unlock()
done := make(chan error, 1)
go func() {
_, err := f.ensure("beta")
done <- err
}()
select {
case err := <-done:
if err != nil {
t.Fatalf("FAIL: beta did not start: %v", err)
}
case <-time.After(childReadyTimeout):
t.Fatal("FAIL: beta waited on alpha's start")
}
}