You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Testcontainers.CosmosDb调用CreateContainerIfNotExistsAsync时如何调整请求超时时间?

Testcontainers.CosmosDb调用CreateContainerIfNotExistsAsync时如何调整请求超时时间?

我来帮你搞定这个问题!在使用Testcontainers.CosmosDb做集成测试时,确实经常会遇到创建容器超时的情况——毕竟测试环境里的Cosmos Emulator启动速度、资源情况都不稳定。下面给你两种实用的调整方式,按需选就行:

方法一:全局设置CosmosClient的请求超时

默认情况下,Testcontainers生成的CosmosClient用的是系统默认的超时(一般是10秒),我们可以自己创建CosmosClient并自定义超时配置,覆盖默认值:

using NUnit.Framework;
using Microsoft.Azure.Cosmos;
using Testcontainers.CosmosDb;
using System;
using System.Threading.Tasks;

public class CosmosIntegrationTests
{
    private CosmosDbContainer _cosmosDbContainer;
    private CosmosClient _cosmosClient;
    private Database _cosmosDatabase;
    private Container _cosmosContainer1;

    [SetUp]
    public async Task Setup()
    {
        // 构建并启动CosmosDb测试容器
        _cosmosDbContainer = new CosmosDbBuilder()
            .WithImage("mcr.microsoft.com/azure-cosmos-db-emulator:latest")
            .Build();
        await _cosmosDbContainer.StartAsync();

        // 自定义CosmosClient配置,重点设置请求超时
        var clientOptions = new CosmosClientOptions
        {
            RequestTimeout = TimeSpan.FromSeconds(30), // 把超时改成30秒,你可以按需调整
            ConnectionMode = ConnectionMode.Gateway // 可选,根据测试环境调整连接模式
        };

        // 用容器的连接字符串+自定义配置创建CosmosClient
        _cosmosClient = new CosmosClient(_cosmosDbContainer.GetConnectionString(), clientOptions);

        // 创建数据库和容器,此时会使用我们设置的超时时间
        _cosmosDatabase = await _cosmosClient.CreateDatabaseIfNotExistsAsync("TestDb");
        _cosmosContainer1 = await _cosmosDatabase.CreateContainerIfNotExistsAsync(
            id: "TestContainer",
            partitionKeyPath: "/partitionKey",
            throughput: 400);
    }

    [TearDown]
    public async Task Teardown()
    {
        await _cosmosDbContainer.StopAsync();
        await _cosmosDbContainer.DisposeAsync();
    }

    [Test]
    public async Task VerifyContainerExists()
    {
        var containerResponse = await _cosmosContainer1.ReadContainerAsync();
        Assert.That(containerResponse.Resource.Id, Is.EqualTo("TestContainer"));
    }
}

这种方式会让这个CosmosClient发起的所有请求都使用设置的超时时间,适合整个测试用例都需要更长超时的场景。

方法二:仅针对创建容器的请求单独设置超时

如果你只是创建容器这一步容易超时,其他请求没问题,可以给CreateContainerIfNotExistsAsync单独传超时配置:

// 在创建容器时,传入RequestOptions设置超时
var createContainerOptions = new RequestOptions
{
    RequestTimeout = TimeSpan.FromSeconds(30)
};

_cosmosContainer1 = await _cosmosDatabase.CreateContainerIfNotExistsAsync(
    id: "TestContainer",
    partitionKeyPath: "/partitionKey",
    throughput: 400,
    requestOptions: createContainerOptions);

这种方式更灵活,只会影响当前创建容器的请求,不会改变其他操作的超时设置。

额外小Tips

如果还是偶尔超时,建议在容器启动后加一点等待时间,确保Cosmos Emulator完全就绪:

await _cosmosDbContainer.StartAsync();
await Task.Delay(5000); // 等待5秒,给Emulator足够的启动时间

备注:内容来源于stack exchange,提问作者SiBrit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 16:18:14