简体   繁体   English

如何测试您的客户端服务器程序

[英]How to test your client server program

我正在制作一个多人游戏,并且经常想测试它是否可以在全球网络上正常运行,因为有时它只能在本地运行,所以我怎么能做到这一点而无需将客户发送给朋友进行测试。

If you want to test it for the "global network" - you have to test it that way. 如果要针对“全球网络”进行测试-您必须采用这种方式进行测试。 There are multiple things that can go wrong which are not an issue on a local network. 有很多可能出错的地方,这在本地网络上不是问题。 Just off the top of my head 就在我的头顶上

  1. latency (important for a game) 延迟(对游戏很重要)
  2. NAT (common cause of problems depending on your game architecture - more so if P2P) NAT(问题的常见原因取决于您的游戏体系结构-如果是P2P,则更是如此)
  3. security 安全
  4. connection errors (Wifi/3G intermittent loss) 连接错误(Wifi / 3G间歇性丢失)

There are many aspects of networking that are often subtly different when you're talking over the internet rather than running on either localhost or a local network. 当您通过Internet而不是在localhost或本地网络上运行时,网络的许多方面通常会有所不同。

  • All manner of delays can occur, and this can throw out some poor assumptions in your code. 各种形式的延迟都可能发生,这可能会在您的代码中抛出一些错误的假设。
  • The TCP flow can be affected by TCP's flow control (when the TCP Window fills up the sender will stop sending and report the fact to you (or maybe not report it, if you're using async APIs)). TCP的流控制可能会影响TCP流(当TCP窗口填满时,发送方将停止发送并向您报告该事实(如果您使用的是异步API,则可能不报告该事实))。
  • TCP reads that return 'complete messages' on localhost and your own network may start to return pieces of a message. TCP读取将在本地主机上返回“完整的消息”,并且您自己的网络可能开始返回一条消息。
  • UDP datagrams may go missing and never arrive or may arrive multiple times or in any sequence. UDP数据报可能会丢失并且永远不会到达,或者可能会多次或以任何顺序到达。

So you're right to think that you need to test your code for these edge cases which rarely show up on your own network. 因此,您以为您需要针对很少出现在您自己的网络中的这些极端情况测试代码。 You're also right to think that simply sending a client to a friend, or running it on a remote machine, is not enough. 您还以为仅将客户端发送给朋友或在远程计算机上运行客户端是不够的。

One approach is to build a dedicated test client which sends known game play and checks that it gets expected responses (how hard this is depends on your protocol and your game). 一种方法是构建一个专用的测试客户端,该客户端发送已知的游戏并检查其是否获得了预期的响应(这有多难取决于您的协议和游戏)。 Once you have that working you then have the test client deliberately send data in such a way that the items above are tested. 完成上述工作后,测试客户端会故意以测试上述各项的方式发送数据。 So, if you're using UDP, you might put some code in your test client so that it sometimes doesn't bother to send a UDP datagram at all. 因此,如果您使用的是UDP,则可以在测试客户端中放入一些代码,以便有时根本不用去发送UDP数据报。 The client should think it sent it. 客户应该认为它已发送。 The networking layer simply ditches it. 网络层只是干掉它。 This tests your UDP protocol for missing datagrams. 这将测试您的UDP协议中是否缺少数据报。 Then send some datagrams multiple times, then send some out of sequence, etc. For TCP add delays, break logical "messages" into separate network sends with large delays between them; 然后多次发送一些数据报,然后按顺序发送一些数据报,依此类推。对于TCP添加延迟,将逻辑“消息”分解为单独的网络发送,它们之间存在较大的延迟。 ideally send each distinct message type as a sequence of single bytes to check that the server's 'message accumulation code' works correctly. 理想情况下,将每种不同的消息类型作为单字节序列发送,以检查服务器的“消息累积代码”是否正常工作。

Once you have done this you need to do the same for your client code, perhaps by adding a "fuzzing" option to your server's network code to do the same kind of thing... 完成此操作后,您需要对客户端代码执行相同的操作,也许是通过在服务器的网络代码中添加“模糊处理”选项来执行相同的操作...

Personally I tend to try and take a step back and do as much of this as possible in dedicated 'unit tests' (I know that some people will say that these aren't unit tests, call them what you like, just write them!). 我个人倾向于尝试退后一步,并在专用的“单元测试”中尽可能多地这样做(我知道有些人会说这些不是单元测试,称呼自己喜欢的东西,只写它们! )。 These tests exercise your networking layer using real networking (talking to a dummy server/client that the test creates) and validate the horrible edge cases. 这些测试使用真实的网络(与测试创建的虚拟服务器/客户端进行通信)来练习网络层,并验证可怕的极端情况。

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

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