我正在测试向外部系统发送一些简单的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,并将send转换为在线阅读后发送,这是通过tcp发送的正确方法。然而,这里似乎还有一些东西丢失了,因为它现在无法发送任何东西。
关于转换第一个代码块以允许它发送TCP而不是UDP的最佳方法的任何帮助