简体   繁体   English

如何使用自定义协议测试驱动网络应用程序?

[英]How to test drive a networking application with custom protocol?

I'm currently developing two Java networking applications for school projects. 我目前正在为学校项目开发两个Java网络应用程序。 One over TCP and the other one over UDP. 一个通过TCP,另一个通过UDP。 In both I have to implement simple custom protocol. 在这两种方法中,我都必须实现简单的自定义协议。

Even though I'm trying pretty hard, I can't find a way how to correctly test this kind of apps, or better develop with test first development . 即使我在努力工作, 也无法找到一种方法来正确测试此类应用程序,或者无法通过测试优先开发更好地进行开发

If I have a client and I want real test without stubbing everything out, I have to implement server with simulated behaviour, which in case of simple apps like these is almost the whole project . 如果我有一个客户并且想要进行真正的测试而又不花钱,那么我必须以模拟的行为来实现服务器,在像这样的简单应用程序的情况下,这几乎就是整个项目 I understand, that when something big, than writing few lines of Perl script to test it could really help. 我了解,当事情很大时,比编写几行Perl脚本进行测试可能确实有帮助。

Right now I'm developing server and client simultaneously, so that I can at least test by hand, but this doesn't seem like a clean way to develop. 现在,我正在同时开发服务器和客户端,以便至少可以手动进行测试,但这似乎并不是一种干净的开发方式。 The only thing that is helping is tunneling the connection through logger, so that I can see all the data that goes through (using TunneliJ plugin for IDEA ). 唯一有帮助的是通过记录器建立连接隧道,以便我可以查看通过的所有数据(使用IDEA的TunneliJ插件 )。

What is the best way to TDD a networking application with custom protocol? 用自定义协议TDD网络应用程序的最佳方法是什么? Should I just stub everything and be fine with it? 我是否应该对所有内容都进行存根处理并妥善处理?

Separate the protocol from the network layer. 将协议与网络层分开。 Testing the logic of the protocol will become easier once you can feed it your own data, without the need to go through the network stack. 一旦您可以自己输入数据,而无需通过网络堆栈,测试协议的逻辑将变得更加容易。 Even though you are not using Python, I'd suggest to look at the structure of the Twisted framework. 即使您没有使用Python,我还是建议您看一下Twisted框架的结构。 It's a nice example of how to unit-test networking applications. 这是如何对网络应用程序进行单元测试的一个很好的示例。

We wound up with the same problem a while ago. 我们不久前遇到了同样的问题。 We decided it was simpler to put two developers on the task: one to write the server and one to write the client. 我们认为,让两名开发人员来完成任务更简单:一个负责编写服务器,另一个负责编写客户端。 We started working in the same office so that we could code, test, modify, repeat a little bit more easily. 我们开始在同一办公室工作,以便我们可以更轻松地进行编码,测试,修改和重复。

All in all, I think it was the best solution for us. 总而言之,我认为这是对我们最好的解决方案。 It gave us the ability to actually test the program in conditions there were not ideal. 它使我们能够在不理想的条件下实际测试程序。 For instance, our Internet went out a couple of times and our program crashed, so we fixed it. 例如,我们的互联网出现了几次故障,程序崩溃了,因此我们进行了修复。 It worked rather well for us, but if you are a sole developer, it may not be the solution for you. 它对我们来说效果很好,但是如果您是唯一的开发人员,那么它可能不是您的解决方案。

Whatever you do, when writing a custom protocol, I would check out Wireshark for monitoring your network traffic to make sure all of the packets are correct. 无论您做什么,在编写自定义协议时,我都会签出Wireshark来监视您的网络流量,以确保所有数据包都是正确的。

In my app I have code such as this 在我的应用中,我有这样的代码

    m_socket.receive(packet);

    doSomething(packet);

I mock up the receive and hence can exercise everything that doSomething() needs to do. 我模拟了接收操作,因此可以执行doSomething()需要做的所有事情。

Where does this break down for you? 这在哪里分解为您? Here you are truly unit testing that your code behaves correctly, you can also mock the socket send, and se expectations for what you think should be sent according to your protocol. 在这里,您可以进行真正的单元测试,以确保代码行为正确,还可以模拟套接字发送,并根据协议期望发送期望的内容。

We are of course not actually testing that the other end of the protocol is happy. 当然,我们实际上并没有测试协议另一端是否满意。 That's integration testing. 那就是集成测试。 I always hanker after getting to IT as soon as possible. 我总是渴望尽快接触IT。 It's when you interact with the "other end" that you find the interesting stuff. 当您与“另一端”互动时,您会发现有趣的东西。

You are in the luck position of being in control of both ends, in that position I would probably spend some time instrument to create suitable, controllable test harnesses. 您处于控制两端的好运位置,在那个位置,我可能会花一些时间来创建合适的,可控制的测试工具。

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

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