There is no guarantee that the command will always fail with MaxTimeMSExpired because of a race in the AsyncRequestSender. When the opCtx is killed due to the maxTimeMS deadline, the ARS stores the status and marks any outstanding remote responses as failed with MaxTimeMSExpired. However, if the shard has already responded before that, the ARS will still return the response from shard. So in that case, the StaleConfig error from the shard will get handled in the mongos retry loop, causing the mongos to mark the shard as stale and send out setShardVersion to the shard which will then fail due to MaxTimeMSExpired. So the command will just keep failing in all next retries.
The test should instead use checkLog to check for the log lineĀ "Failed to refresh metadata for collection.*MaxTimeMSExpired" on the shard.