【发布时间】:2018-07-23 19:55:32
【问题描述】:
我正在测试向外部系统发送一些简单的 HTTP 请求。虽然我已经成功地证明了在功能上,当我发送超过 20 条消息时,被测系统正在处理一条消息,但我遇到的故障似乎是由于未收到消息而不是系统本身的问题。
目前我正在通过 UDP 发送我的消息,但想看看这是否会导致丢失,因此想转换为通过 TCP 发送
通过 UDP 发送的代码
@when('{number_of_requests} HTTP requests are sent to it')
def step_impl(context, number_of_requests):
# Waiting for the service to be ready after restart
time.sleep(1)
# Get the Endpoint address and port from Config.py
UDP_IP = config.endpoint['host']
UDP_PORT = int(config.endpoint['port'])
context.messages_sent = []
# Setting up the socket to send the UDP datagrams
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(True)
# Getting the configured sub id header name
sub_id_header_name = get_subid_header_name()
context.subscribers = generate_subscribers(number_of_requests)
sock.sendto(bytes("xxx", "utf-8"), (UDP_IP, UDP_PORT))
for i in range(int(number_of_requests)):
# Generating a SPID
spid = encodespid("ab")
# Generating a SUB_ID
sub_id = context.subscribers[i]
# Setting up the whole message to be sent
message = small_message.format(spid, sub_id_header_name, sub_id)
# Sending the message
sock.sendto(bytes(message, "utf-8"), (UDP_IP, UDP_PORT))
context.messages_sent.append(spid)
# Closing the socket
sock.close()
通过 TCP 发送的代码(到目前为止)
@when('{number_of_requests} HTTP requests are sent to it')
def step_impl(context, number_of_requests):
# Waiting for the service to be ready after restart
time.sleep(1)
# Get the Endpoint address and port from Config.py
TCP_IP = config.endpoint['host']
TCP_PORT = int(config.endpoint['port'])
context.messages_sent = []
# Setting up the socket to send the UDP datagrams
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(True)
# Getting the configured sub id header name
sub_id_header_name = get_subid_header_name()
context.subscribers = generate_subscribers(number_of_requests)
sock.send(bytes("xxx", "utf-8"), (TCP_IP, TCP_PORT))
for i in range(int(number_of_requests)):
# Generating a SPID
spid = encodespid("ab")
# Generating a SUB_ID
sub_id = context.subscribers[i]
# Setting up the whole message to be sent
message = small_message.format(spid, sub_id_header_name, sub_id)
# Sending the message
sock.send(bytes(message, "utf-8"), (TCP_IP, TCP_PORT))
context.messages_sent.append(spid)
# Closing the socket
sock.close()
正如您在上面看到的,我已将所有对 UDP 的引用更改为 TCP,并在在线阅读后将 Sendto 转换为 Send,这是通过 TCP 发送的正确方法。但是,这里似乎还缺少其他东西,因为它现在无法发送任何东西。
关于转换第一个代码块以允许它发送 TCP 而不是 UDP 的最佳方法的任何帮助
【问题讨论】: