Repository: MystenLabs/sui
File: crates/sui-indexer-alt-graphql/src/extensions/timeout.rs (Rust)

Code before:
        let delay = Duration::from_millis(200);
        let response = Schema::build(Root(zero), Root(delay / 2), EmptySubscription)
            .extension(Timeout::new(TimeoutConfig {
                query: zero,
                mutation: delay,
            }))
            .finish()
            .execute("mutation { op }")
            .await;

        assert!(response.is_ok());
    }

    /// The request takes longer than the timeout to handle, so it should fail.
    #[tokio::test]
    async fn test_query_timeout_fail() {
        let timeout = Duration::from_millis(200);
        let event = test_timeout_fail(
            timeout,
            Duration::ZERO,
            timeout * 2,
            "query { op }",
            "Query",
        )
        .await;
        assert_snapshot!(event);
    }

    /// Like [test_query_timeout_fail], but for a mutation.
    #[tokio::test]
    async fn test_mutation_timeout_fail() {
        let timeout = Duration::from_millis(200);
        let event = test_timeout_fail(
            Duration::ZERO,
            timeout,
            timeout * 2,
            "mutation { op }",
            "Mutation",
        )
        .await;
        assert_snapshot!(event);
    }

    /// Mutations are resolved sequentially, and the timeout should apply to the total time spent
    /// on the request.
    #[tokio::test]
    async fn test_mutation_additive_timeout() {
        let timeout = Duration::from_millis(200);
        let event = test_timeout_fail(
            Duration::ZERO,
            timeout,

Review comment (suggestion):
I was thinking that you would use an inline snapshot here (add `@""` to the macro invocation and insta will populate the snapshot inline when you re-run the tests -- don't forget to delete the snapshots folder that was added before if you do this, otherwise it will stick around forever), because it's a bit easier to correlate the expected output with the operation that caused it.


```suggestion
        assert_snapshot!(event, @"");
```

Code after:
        let delay = Duration::from_millis(200);
        let response = Schema::build(Root(zero), Root(delay / 2), EmptySubscription)
            .extension(Timeout::new(TimeoutConfig {
                query: zero,
                mutation: delay,
            }))
            .finish()
            .execute("mutation { op }")
            .await;

        assert!(response.is_ok());
    }

    /// The request takes longer than the timeout to handle, so it should fail.
    #[tokio::test]
    async fn test_query_timeout_fail() {
        let timeout = Duration::from_millis(200);
        let event = test_timeout_fail(
            timeout,
            Duration::ZERO,
            timeout * 2,
            "query { op }",
            "Query",
        )
        .await;
        assert_snapshot!(event, @r"
        [WARN] Request timed out: timeout elapsed at:
        trace 0:
           0: async_graphql::graphql::field
                   with path: op, parent_type: Root, return_type: Boolean!
           1: async_graphql::graphql::execute
           2: async_graphql::graphql::request
         request_id=ffffffff-ffff-ffff-ffff-ffffffffffff kind=Query
        ")
    }

    /// Like [test_query_timeout_fail], but for a mutation.
    #[tokio::test]
    async fn test_mutation_timeout_fail() {
        let timeout = Duration::from_millis(200);
        let event = test_timeout_fail(
            Duration::ZERO,
            timeout,
            timeout * 2,
            "mutation { op }",
            "Mutation",
        )
        .await;
        assert_snapshot!(event, @r"
        [WARN] Request timed out: timeout elapsed at:
        trace 0: