简体   繁体   English

你如何对tcp连接进行单元测试?

[英]How do you unit test for tcp connection?

I've recently been given a chance to write a simple tcp-client for a project of mine, and due to my huge ignorance with tcp/ip, I have a hard time getting it to work properly. 我最近有机会为我的项目编写一个简单的tcp-client,由于我对tcp / ip的巨大无知,我很难让它正常工作。

I find that sometimes I can get weird connection issue when the connection refused as the tcp server is down or sometimes I might get an exception when calling receive. 我发现有时候当tcp服务器关闭时连接被拒绝或者有时在调用receive时我可能会遇到异常时,我会遇到奇怪的连接问题。

As the tcp-server is a black box and we have no access to it, I'm wondering in this situation, what's the best way to write unit tests for this? 由于tcp-server是一个黑盒子,我们无法访问它,我想知道在这种情况下,为此编写单元测试的最佳方法是什么?

I'm thinking either I should write a tcp-server and make it return certain inputs as what I would expect from the real server or just mock the functions related to return ideal situation data. 我想要么我应该写一个tcp-server并让它返回某些输入,就像我期望从真实服务器那样,或者只是模拟与返回理想情况数据相关的函数。

I just don't like the fact that sometimes I might get weird connection/receive issue and I would like to know how to properly write unit test which I can reuse/extend in the future to ensure that everything that work before should still be working in case my code or their code change. 我只是不喜欢这样的事实:有时我可能会遇到奇怪的连接/接收问题而且我想知道如何正确编写单元测试,我可以在将来重复使用/扩展以确保之前工作的所有内容仍然有效以防我的代码或代码发生变化。

thanks 谢谢

You should have two kinds of tests: 你应该有两种测试:

  1. Integration tests - with real bare-bone TCP server that can send and receive connections. 集成测试 - 使用可以发送和接收连接的真正的裸骨TCP服务器。 Create this server that has the bare minimum functionality and use it to test how your client behaves. 创建具有最低功能的此服务器,并使用它来测试客户端的行为方式。 Using a simple TCP server you can test how your client send and receive messages and how it connects and disconnect from the server. 使用简单的TCP服务器,您可以测试客户端如何发送和接收消息以及如何连接和断开与服务器的连接。 Another useful test is how several clients connect and send messages to the server. 另一个有用的测试是多个客户端如何连接并向服务器发送消息。
  2. Unit tests - using Mocking you can test more complex scenarios. 单元测试 - 使用Mocking可以测试更复杂的场景。 You won't be able to send or receive messages but you can test the client's internal logic - how it behaves if two messages with arrive, re-sending in case of errors etc. 您将无法发送或接收消息,但您可以测试客户端的内部逻辑 - 如果两个消息到达,如果出现错误则重新发送,它的行为方式等等。

Using both kinds of tests you should be able to cover most of your client's functioanlity 使用这两种测试,您应该能够涵盖大部分客户的功能

For unit testing, I'd create a simple socket server (kicked off when the UT starts) just for the purpose of testing the client. 对于单元测试,我创建一个简单的套接字服务器(在UT启动时启动)只是为了测试客户端。 If you make it simple and standalone you'll reduce hassles running the test. 如果你简单而独立,你将减少运行测试的麻烦。 You may also be able to use tools like ncat to facilitate this. 您也可以使用像ncat这样的工具来实现这一点。

However having said that there may be problems that might be harder for the UT to pick up. 然而,尽管如此,可能存在可能难以接受的问题。 Keepalive issues, maybe external problems such as routing. Keepalive问题,可能是路由等外部问题。 But if you use a real listening socket (as opposed to mocking a connection) it's real TCP. 但是如果你使用真正的侦听套接字(而不是模拟连接),它就是真正的TCP。

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

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