【问题标题】:How do we test a rest api end point that uses azure durable functions?我们如何测试使用 azure 持久函数的 rest api 端点?
【发布时间】:2020-08-25 10:12:54
【问题描述】:

我们有一个使用 azure 函数来获取结果的端点。有时我们几乎立即得到结果,有时我们在 10 秒或 20 秒后得到结果。

我想知道是否有更好的方法来使用邮递员测试这些端点,或者您是否使用不同的工具来测试依赖于持久功能的端点。这个解决方案工作正常,但并不总是可靠。

statusCode = responseCode.code;
var result = JSON.parse(responseBody);

var maxNumberOfTries = 24;
var sleepBetweenTries = 5000;

if (!pm.environment.get("collection_tries")) {
    pm.environment.set("collection_tries", 1);
}

console.log(result);
tests["Status code is 200"] = statusCode == environment["status_OK"];

if (result.abc.length == 0) {
    if (pm.environment.get("collection_tries") <= maxNumberOfTries && statusCode == 200) {
        var tries = parseInt(pm.environment.get("collection_tries"), 10);
        pm.environment.set("collection_tries", tries + 1);
        setTimeout(function () { }, sleepBetweenTries);
        postman.setNextRequest(pm.info.requestId);
    }
    else {
        pm.environment.unset("collection_tries");
        console.log("Exceeded all attempts to check ABC");
        tests["ABC exist"] = false;
    }
} else {
    pm.environment.unset("collection_tries");
    tests["ABC exist"] = true;
    
    var abcid = result._id;

【问题讨论】:

    标签: api postman postman-collection-runner azure-durable-functions


    【解决方案1】:

    当您在 http 触发函数中启动编排时,内置持久函数 http 响应具有以下输出:

    HTTP/1.1 202 Accepted
    Connection: close
    Date: Wed, 26 Aug 2020 07:43:55 GMT
    Content-Type: application/json; charset=utf-8
    Server: Kestrel
    Content-Length: 958
    Location: http://localhost:7071/runtime/webhooks/durabletask/instances/a30d3b1b203e4135b302204b99c989b3?taskHub=DurableDemo&connection=Storage&code=4X/ekkGia/Ng07GHcwLz9CKLClggXjdKKZQ0hQC6pifxteBWvA19jg==
    Retry-After: 10
    
    {
      "id": "a30d3b1b203e4135b302204b99c989b3",
      "statusQueryGetUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/a30d3b1b203e4135b302204b99c989b3?taskHub=DurableDemo&connection=Storage&code=4X/ekkGia/Ng07GHcwLz9CKLClggXjdKKZQ0hQC6pifxteBWvA19jg==",
      "sendEventPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/a30d3b1b203e4135b302204b99c989b3/raiseEvent/{eventName}?taskHub=DurableDemo&connection=Storage&code=4X/ekkGia/Ng07GHcwLz9CKLClggXjdKKZQ0hQC6pifxteBWvA19jg==",
      "terminatePostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/a30d3b1b203e4135b302204b99c989b3/terminate?reason={text}&taskHub=DurableDemo&connection=Storage&code=4X/ekkGia/Ng07GHcwLz9CKLClggXjdKKZQ0hQC6pifxteBWvA19jg==",
      "purgeHistoryDeleteUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/a30d3b1b203e4135b302204b99c989b3?taskHub=DurableDemo&connection=Storage&code=4X/ekkGia/Ng07GHcwLz9CKLClggXjdKKZQ0hQC6pifxteBWvA19jg=="
    }
    
    

    然后您轮询 Location 标头中提到的端点(或 statusQueryGetUri 属性的值)并应用 Retry 中提到的等待时间-在 标头之后。一旦您收到带有实际结果的 200 响应,您检查 runtimeStatus 属性是否标记为 Completed(或 Failed 等)。

    此解决方案与您已有的解决方案没有太大区别。但是您使用的是来自 DF 的更多内置功能。

    有关 DF HTTP API 的更多信息:https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-http-api

    【讨论】:

    • 谢谢你,Marc,我会试试这个
    猜你喜欢
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2013-08-28
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多