Repository: tikv/tikv
File: src/coprocessor/endpoint.rs (Rust)

Code before:
        // Test case 1: Deadline already expired (zero remaining duration)
        {
            let mut inner = ReqContextInner::default_for_test();
            // Set deadline to a time in the past
            inner.deadline =
                Deadline::new(tikv_util::time::Instant::now_coarse() - Duration::from_millis(100));
            let req_ctx: ReqContext = inner.into();

            let result =
                block_on(unsafe { Endpoint::<RocksEngine>::get_snapshot_with_timeout(&req_ctx) });
            assert!(result.is_err());
            assert_matches!(result, Err(Error::DeadlineExceeded));
        }

        // Test case 2: Snapshot retrieval delayed by failpoint, causing timeout
        {
            #[cfg(feature = "failpoints")]
            {
                let failpoint_name = "coprocessor_async_in_memory_snapshot_timeout";
                // Enable failpoint to sleep for 5000ms, simulating slow snapshot retrieval
                // This is more realistic than pause, as it simulates actual delay
                fail::cfg(failpoint_name, "sleep(5000)").unwrap();

                let mut inner = ReqContextInner::default_for_test();
                // Set a short deadline (100ms) that will timeout before the sleep completes
                // Use a longer deadline to give GLOBAL_TIMER_HANDLE time to trigger
                inner.deadline = Deadline::from_now(Duration::from_millis(100));
                let req_ctx: ReqContext = inner.into();

                let start = std::time::Instant::now();
                let result = block_on(unsafe {
                    Endpoint::<RocksEngine>::get_snapshot_with_timeout(&req_ctx)
                });
                let elapsed = start.elapsed();

                // Clean up failpoint
                fail::remove(failpoint_name);

                // Verify that timeout occurred
                // Note: In test environment, GLOBAL_TIMER_HANDLE might not trigger
                // immediately, so we check if either timeout occurred or the result is an error
                if result.is_ok() {
                    // If timeout didn't trigger, it means GLOBAL_TIMER_HANDLE didn't work
                    // in test environment. This is a known limitation.
                    // We'll skip this assertion in test environment.
                    eprintln!(
                        "Warning: Timeout did not trigger in test environment. \
                         This may be due to GLOBAL_TIMER_HANDLE not working properly. \
                         Elapsed: {:?}, Result: {:?}",
                        elapsed, result
                    );

Review comment (bug):
The comment on line 2464 says "Use a longer deadline to give GLOBAL_TIMER_HANDLE time to trigger" but then sets a short deadline of 100ms. This comment is misleading and should be removed or clarified, as the intent is actually to set a short deadline to test timeout behavior, not a longer one.
```suggestion
                // Set a short deadline (100ms) so the request times out well before the 5000ms
                // failpoint sleep completes, exercising GLOBAL_TIMER_HANDLE timeout behavior.
```

Code after:
            let mut inner = ReqContextInner::default_for_test();
            // Set deadline to a time in the past
            inner.deadline =
                Deadline::new(tikv_util::time::Instant::now_coarse() - Duration::from_millis(100));
            let req_ctx: ReqContext = inner.into();

            let result =
                block_on(unsafe { Endpoint::<RocksEngine>::get_snapshot_with_timeout(&req_ctx) });
            assert!(result.is_err());
            assert_matches!(result, Err(Error::DeadlineExceeded));
        }

        // Test case 2: Snapshot retrieval delayed by failpoint, causing timeout
        {
            #[cfg(feature = "failpoints")]
            {
                let failpoint_name = "coprocessor_async_in_memory_snapshot_timeout";
                // Enable failpoint to sleep for 5000ms, simulating slow snapshot retrieval
                // This is more realistic than pause, as it simulates actual delay
                fail::cfg(failpoint_name, "sleep(5000)").unwrap();

                let mut inner = ReqContextInner::default_for_test();
                // Set a short deadline (200ms) so the request timeout before the sleep
                // completes
                inner.deadline = Deadline::from_now(Duration::from_millis(200));
                let req_ctx: ReqContext = inner.into();

                let start = std::time::Instant::now();
                let result = block_on(unsafe {
                    Endpoint::<RocksEngine>::get_snapshot_with_timeout(&req_ctx)
                });
                let elapsed = start.elapsed();

                // Clean up failpoint
                fail::remove(failpoint_name);

                // Verify that timeout occurred
                // Note: In test environment, GLOBAL_TIMER_HANDLE might not trigger
                // immediately, so we check if either timeout occurred or the result is an error
                if result.is_ok() {
                    // If timeout didn't trigger, it means GLOBAL_TIMER_HANDLE didn't work
                    // in test environment. This is a known limitation.
                    // We'll skip this assertion in test environment.
                    eprintln!(
                        "Warning: Timeout did not trigger in test environment. \
                         This may be due to GLOBAL_TIMER_HANDLE not working properly. \
                         Elapsed: {:?}, Result: {:?}",
                        elapsed, result
                    );
                    // In production yatp FuturePool environment, this would
                    // timeout correctly