DEV Community

A test that hangs if my layouts ever run in sequence

I wrote this with help from Claude and edited it myself. The code and the test it describes are in the Coldforge repo.

Coldforge is a server-first Rust web framework I'm building on Axum. Its app/ folder is the router: app/layout.rs wraps every page, app/rooms/layout.rs wraps everything under /rooms, and app/rooms/[id]/page.rs is the page. Each of those is an async function that can load its own data.

That raises a question every nested-layout framework has to answer. If the root layout loads the signed-in user, the rooms layout loads a sidebar, and the page loads the room, in what order do they run?

The obvious way to write it is one after the other. Render the root layout, find the spot where its child goes, render the child, and so on down. With three segments that each wait 50 ms on a database, the page takes 150 ms. Add a layout and it takes 200. This is a request waterfall, and it happens on the server where you can't see it in the browser's network tab.

I wanted the page to take as long as its slowest segment, not the sum of all of them. Here is how that works, and the test that keeps it true.

A layout doesn't get its child

The trick is that a layout never sees the rendered page. It gets a placeholder:

#[layout]
async fn layout(children: Children, user: CurrentUser) -> View {
    view! {
        <header>{user.name}</header>
        <main>{children}</main>
    }
}

Children is a unit struct. Rendering it writes a slot marker into the output, nothing more. So the layout can finish before the page has even started, and the page doesn't need anything from the layout either. They are independent futures.

The router that Coldforge generates from app/ then runs every segment of the route at once with tokio::join!, and composes the results from the inside out:

let view = layouts.into_iter().rev().fold(page, |child, layout| layout.fill_slot(child));

Each layout's slot is filled with the view inside it, and the outermost layout ends up holding the whole page.

One detail: it's join!, not try_join!. With try_join!, a failing page would cancel its layouts. But when a page fails, Coldforge shows the nearest error.rs inside the layouts above it, so it needs those layouts to finish.

The test

Code like this is easy to break by accident. Someone adds a feature that needs the layout's output before rendering the page, writes a quick .await in the generated code, and every page in every app quietly gets slower. Nothing fails.

A timing test wouldn't catch it reliably either, because CI machines are noisy. So the test doesn't measure time. It uses a tokio::sync::Barrier with two parties, shared through app state:

// tests/pipeline/app/concurrent/layout.rs
#[layout]
async fn layout(children: Children, State(state): State<AppState>) -> View {
    state.barrier.wait().await;
    view! {
        <section>{children}</section>
    }
}
// tests/pipeline/app/concurrent/page.rs
#[page]
async fn page(State(state): State<AppState>) -> View {
    state.barrier.wait().await;
    view! {
        <p>"both ran"</p>
    }
}

A two-party barrier opens only when two tasks are waiting on it at the same time. If the layout and the page run concurrently, both reach the barrier, it opens, and the request finishes. If they ever run one after the other, the first one waits forever for a partner that can't arrive until it's done.

#[tokio::test]
async fn layouts_and_page_run_concurrently() {
    let (status, html) = tokio::time::timeout(Duration::from_secs(10), get("/concurrent"))
        .await
        .expect("layout and page ran one after the other: the barrier never opened");
    assert_eq!(status, StatusCode::OK);
    assert_eq!(html, "<body><section><p>both ran</p></section></body>");
}

The timeout is only there to turn a hang into a readable failure. The test doesn't depend on how fast the machine is: it either passes right away or can never pass.

What it costs

A layout can't look at what its page rendered. It can't count the page's headings to build a table of contents, or change its markup based on the page's content. In practice I haven't missed it: a layout that needs data from the page usually needs the same data the page loads, and can load it itself, at the same time.

Try it

Coldforge is pre-alpha. Routing, server rendering, server actions with forms that work without JavaScript, sessions and the dev loop work today. Islands are next.

cargo install --git https://github.com/konstantinos193/coldforge coldforge-cli
coldforge new my-app
cd my-app
coldforge dev

The repo is at github.com/konstantinos193/coldforge, and the test above is in tests/pipeline/tests/pipeline.rs. Feedback, issues and "this is wrong because" are all welcome.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.