简体   繁体   English

发送2个http并发请求

[英]Send 2 http concurrent requests

I need to test web-api of one app.我需要测试一个应用程序的 web-api。 Test-case is if 2 simular requests came to the 2 nodes of web app at the same time, it returns erorr.测试用例是如果2个simular requests同时到达web app的2个节点,它会返回errr。 So i need to send 2 requests at one moment on a two separate nodes of web app.所以我需要在 web 应用程序的两个单独节点上同时发送 2 个请求。 I know how to send 1 request and how to send requests in rotation.我知道如何发送 1 个请求以及如何轮流发送请求。 Here is the example of request i made:这是我提出的请求示例:

 var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; var xhr = new XMLHttpRequest(); var url = "url"; xhr.open("POST", url, true); xhr.setRequestHeader("Content-Type", "application/json"); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { var json = JSON.parse(xhr.responseText); console.log(json.result); } }; var data = JSON.stringify(); xhr.send(data);

First, and it's mostly my opinion, I would use the fetch API to make the call to the resources you want to pull to your web-app.首先,这主要是我的意见,我会使用fetch API来调用你想要拉到你的网络应用程序的资源。

I think it needs less boilerplate than a XMLHttpRequest.我认为它需要的样板文件比 XMLHttpRequest 少。

On the other hand, it isn't clear to me if you want to check if your web-app is making two request to the same resource, or if want to know how to know and handle the case when your back-end get a cloned request.另一方面,我不清楚您是否想检查您的网络应用程序是否正在向同一资源发出两个请求,或者是否想知道如何知道和处理后端获得的情况克隆请求。

In the first case, you could use something like RxJs to avoid making a request until you have a response.在第一种情况下,您可以使用RxJs 之类的东西来避免在收到响应之前发出请求。 But maybe it is a really big dependency for your project.但也许它对您的项目来说是一个非常大的依赖项。

An other way could be disabling the user input (like the button that is making the request) until you have a response.另一种方法可能是禁用用户输入(例如发出请求的按钮),直到您得到响应。

But, if your question is about how to know in your backend app when you have a duplicated request from the same client, it would depend on your backend architecture.但是,如果您的问题是关于如何在后端应用程序中知道来自同一个客户端的重复请求,那将取决于您的后端架构。 Like, if you have one load balancer and many backend services, or if you have only one nodejs backend facing the public internet, etc.比如,如果你有一个负载均衡器和许多后端服务,或者如果你只有一个 nodejs 后端面向公共互联网,等等。

I would recommend you to somehow persist the client data some were (memory, a db, or something), like IP, or session-ID, or something useful to know who is making that API call;我建议您以某种方式保留一些客户端数据(内存、数据库或其他东西),例如 IP 或会话 ID,或者对知道谁在进行 API 调用有用的东西; and then check if there is already an ongoing request to the same resource so you can make a pertinent response.然后检查是否已经有对同一资源的持续请求,以便您做出相关响应。 Once the call is done, you can remove that from your persistence layer.调用完成后,您可以将其从持久层中删除。

Maybe you were expecting some code snippets, but I hope this can be useful to you.也许你期待一些代码片段,但我希望这对你有用。

Edit after your comments:评论后编辑:

I have host1.webapp/api/methodX and host2.webapp/api/methodX.我有 host1.webapp/api/methodX 和 host2.webapp/api/methodX。 Simply I have to send 2 requests ( 1 for host1 and 1 for host2) at same time.只需我必须同时发送 2 个请求(1 个用于 host1,1 个用于 host2)。 I don't need to wait a response from host1 and then send request to host2, i need to send them togheter just to the different hosts.我不需要等待 host1 的响应然后向 host2 发送请求,我需要将它们一起发送到不同的主机。 And i don't care about response time/ request processing time/etc.而且我不在乎响应时间/请求处理时间/等。

like if i'd used curl it'd looks like: curl request1 & request2就像我使用 curl 它看起来像: curl request1 & request2

In that case, it depends on what behavior you want to test.在这种情况下,这取决于您要测试的行为。 You cant make two AJAX calls using the same XMLHttpRequest object.您不能使用相同的 XMLHttpRequest object 进行两个 AJAX 调用。 If I were you I would make two request, maybe wrap them in a promise and then use the Promise.allSettled() method.如果我是你,我会提出两个请求,也许将它们包装在 promise 中,然后使用Promise.allSettled()方法。 Then you check what happened on the returned values.然后检查返回值上发生了什么。 Another (but more complex way) should be to have a service worker that intercepts all http traffic, and there you can see if the API calls are made as you wanted.另一种(但更复杂的方法)应该是有一个服务工作者来拦截所有 http 流量,在那里你可以看到 API 调用是否如你所愿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM