Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to steer the router state machine during integration tests. #3099

Merged
merged 6 commits into from
May 23, 2023

Conversation

o0Ignition0o
Copy link
Contributor

@o0Ignition0o o0Ignition0o commented May 16, 2023

This changeset provides a new TestRouterHttpServer with a couple of interesting functions for us to write "semi" integrated tests.

Why?

This will allow us to make sure the router updates as expected if configuration, schema and entitlements change.

Ok this sounds great but how do I...

Create a TestRouterHttpServer?

Add your test to apollo-router/src/router.rs:

    #[tokio::test(flavor = "multi_thread")]
    async fn basic_event_stream_test() {
        let mut router_handle = TestRouterHttpServer::new();
   }

Get the router to start?

It will need a valid configuration, a valid schema, and valid entitlements:

      let configuration =
            Configuration::from_str(include_str!("testdata/supergraph_config.router.yaml"))
                .unwrap();
        let schema = include_str!("testdata/supergraph.graphql");

        router_handle
            .send_event(UpdateConfiguration(configuration))
            .await
            .unwrap();
        router_handle
            .send_event(UpdateSchema(schema.to_string()))
            .await
            .unwrap();
        router_handle
            .send_event(UpdateEntitlement(EntitlementState::Unentitled))
            .await
            .unwrap();

Send requests to the router?

        let request = Request::builder().query(r#"{ me { username } }"#).build();

        let response = router_handle.request(request).await.unwrap();
        assert_eq!(
            "@ada",
            response
                .data
                .unwrap()
                .get("me")
                .unwrap()
                .get("username")
                .unwrap()
        );

Get the router to shut down once my tests have passed ?

        router_handle.shutdown().await.unwrap();

You can now have fine grained control over the sequence of events that are processed by the router, and write the relevant regression tests.

That's it! No more excuses!

@github-actions

This comment has been minimized.

@o0Ignition0o o0Ignition0o marked this pull request as ready for review May 16, 2023 15:51
@o0Ignition0o o0Ignition0o requested review from BrynCooke, Geal, abernix and bnjjj and removed request for BrynCooke May 16, 2023 15:54
@o0Ignition0o o0Ignition0o changed the title Improve the way we can test the state_machine in integration Provide a way to steer the router state machine during integration tests. May 16, 2023
apollo-router/src/router.rs Outdated Show resolved Hide resolved
apollo-router/src/router.rs Show resolved Hide resolved
@o0Ignition0o o0Ignition0o merged commit 07c9bfc into dev May 23, 2023
@o0Ignition0o o0Ignition0o deleted the igni/state_machine_tests branch May 23, 2023 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants