| index 1f581c156..753b5842b 100644 |
| --- a/internal/instancectl/demo_build_transfer_test.go |
| +++ b/internal/instancectl/demo_build_transfer_test.go |
| @@ -8,6 +8,7 @@ import ( |
| "io" |
| "net" |
| "net/http/httptest" |
| + "path/filepath" |
| "strings" |
| "sync/atomic" |
| "testing" |
| @@ -22,6 +23,8 @@ import ( |
| type transferBuilder struct { |
| binary []byte |
| turns atomic.Int32 |
| + // grant is how long the signed job has left; zero means a minute. |
| + grant time.Duration |
| } |
| |
| func (b *transferBuilder) Initialize(context.Context) error { return nil } |
| @@ -29,6 +32,9 @@ func (b *transferBuilder) Readiness(context.Context) (demobuild.WorkerReady, err |
| return demobuild.WorkerReady{Capability: "anonymous-demo-v1", GOARCH: "arm64", BundleSHA256: strings.Repeat("b", 64), Slots: 4}, nil |
| } |
| func (b *transferBuilder) GrantDeadline([]byte) (time.Time, error) { |
| + if b.grant != 0 { |
| + return time.Now().Add(b.grant), nil |
| + } |
| return time.Now().Add(time.Minute), nil |
| } |
| func (b *transferBuilder) Build(ctx context.Context, _ []byte, source io.Reader) (demobuild.BuildOutput, error) { |
| @@ -105,3 +111,99 @@ func TestDemoBuildFourFinishedBuildsTakeTurns(t *testing.T) { |
| } |
| free.Release() |
| } |
| + |
| +// stalledConn passes writes through and blocks every read until stall |
| +// closes: a client that sends its build and never reads the answer. |
| +type stalledConn struct { |
| + net.Conn |
| + stall chan struct{} |
| +} |
| + |
| +func (c stalledConn) Read([]byte) (int, error) { |
| + <-c.stall |
| + return 0, net.ErrClosed |
| +} |
| + |
| +// unixControl serves s on a unix socket, as root's control service is |
| +// served, so the kernel buffers little of a response nobody reads. |
| +func unixControl(t *testing.T, s *Server) string { |
| + t.Helper() |
| + sock := filepath.Join(t.TempDir(), "c") |
| + l, err := net.Listen("unix", sock) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + srv := httptest.NewUnstartedServer(s.Handler()) |
| + srv.Listener.Close() |
| + srv.Listener = l |
| + srv.Start() |
| + t.Cleanup(srv.Close) |
| + return sock |
| +} |
| + |
| +// turnTaken waits until b's build holds the output turn. |
| +func turnTaken(t *testing.T, b *transferBuilder) { |
| + t.Helper() |
| + for end := time.Now().Add(5 * time.Second); b.turns.Load() == 0; time.Sleep(5 * time.Millisecond) { |
| + if time.Now().After(end) { |
| + t.Fatal("the build never took the output turn") |
| + } |
| + } |
| +} |
| + |
| +// A client that never reads its response holds the turn only until the |
| +// signed job's own deadline plus five seconds, not the 180 s window: a job |
| +// with little time left cannot keep every other finished build waiting. |
| +func TestDemoBuildStalledReaderReturnsTurnByGrantDeadline(t *testing.T) { |
| + b := &transferBuilder{binary: transferELF(4 << 20), grant: 300 * time.Millisecond} |
| + sock := unixControl(t, &Server{DemoBox: true, DemoBuilder: b}) |
| + stall := make(chan struct{}) |
| + done := make(chan struct{}) |
| + t.Cleanup(func() { close(stall); <-done }) |
| + c := &Client{Path: sock, Dial: func(ctx context.Context, path string) (net.Conn, error) { |
| + conn, err := (&net.Dialer{}).DialContext(ctx, "unix", path) |
| + return stalledConn{conn, stall}, err |
| + }} |
| + go func() { |
| + defer close(done) |
| + c.BuildDemo(context.Background(), []byte("grant"), strings.NewReader("source")) |
| + }() |
| + turnTaken(t, b) |
| + ctx, cancel := context.WithTimeout(t.Context(), 8*time.Second) |
| + defer cancel() |
| + free, err := demobuild.AwaitOutputTransfer(ctx) |
| + if err != nil { |
| + t.Fatal("a stalled reader kept the output turn past its grant deadline plus five seconds") |
| + } |
| + free.Release() |
| +} |
| + |
| +// A client that hangs up before reading gives the turn back at once. |
| +func TestDemoBuildDisconnectedReaderReturnsTurn(t *testing.T) { |
| + b := &transferBuilder{binary: transferELF(4 << 20)} |
| + sock := unixControl(t, &Server{DemoBox: true, DemoBuilder: b}) |
| + stall := make(chan struct{}) |
| + conns := make(chan net.Conn, 1) |
| + done := make(chan struct{}) |
| + t.Cleanup(func() { close(stall); <-done }) |
| + c := &Client{Path: sock, Dial: func(ctx context.Context, path string) (net.Conn, error) { |
| + conn, err := (&net.Dialer{}).DialContext(ctx, "unix", path) |
| + if err == nil { |
| + conns <- conn |
| + } |
| + return stalledConn{conn, stall}, err |
| + }} |
| + go func() { |
| + defer close(done) |
| + c.BuildDemo(context.Background(), []byte("grant"), strings.NewReader("source")) |
| + }() |
| + turnTaken(t, b) |
| + (<-conns).Close() |
| + ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second) |
| + defer cancel() |
| + free, err := demobuild.AwaitOutputTransfer(ctx) |
| + if err != nil { |
| + t.Fatal("the turn was still held after the client hung up") |
| + } |
| + free.Release() |
| +} |