简体   繁体   English

对使用UDP套接字的代码进行单元测试的推荐方法是什么?

[英]What is the recommended way to unit test code that uses UDP sockets?

After a bit of searching, I have not come across a well-defined solution to unit (Junit) testing Java(or any!) code that includes a UDP socket. 经过一番搜索,我还没有找到一种定义明确的解决方案来对包含UDP套接字的Java(或任何!)代码进行单元(Junit)测试。 I have a piece of Java code that opens up a UDP socket and sends some encoded data in. The test I am writing will make sure that the data going in is the "same" data coming out. 我有一段Java代码可以打开UDP套接字并向其中发送一些编码数据。我正在编写的测试将确保输入的数据与输出的“相同”数据相同。 Therefore, I will need to send one one side, and receive on the other. 因此,我将需要一侧发送,而另一侧接收。 Due to the nature of UDP sockets, one can not open up multiple sockets listening in on the same port number. 由于UDP套接字的性质,不能打开侦听同一端口号的多个套接字。 If someone tries to do so, they will hit this exception: 如果有人尝试这样做,他们将遇到以下异常:

java.net.BindException: Address already in use: Cannot bind

As far as I understand, sending and receiving over the same port via UDP requires the same socket to be used for both function. 据我了解,通过UDP通过同一端口发送和接收都需要将相同的套接字用于这两种功能。 Please correct me if I am wrong. 如果我错了,请纠正我。

I have two ideas at the moment: 我目前有两个想法:

  1. Use reflection to expose the private member datagramSocketOut, and then set the reuse flag to true. 使用反射公开私有成员datagramSocketOut,然后将重用标志设置为true。 At which point, I would copy(& reuse) this socket to a datagramSocketIn and receive from this new end. 在这一点上,我会将这个套接字复制(和重用)到datagramSocketIn并从这个新端接收。 In my eyes, this seems rather janky and just plain smells bad. 在我眼中,这看起来有点过时,而且闻起来很普通。

  2. Refactor my code to no longer create a new socket internaly, but instead be passed one in externally. 重构我的代码以不再在内部创建新的套接字,而是在外部传递一个。 This willremove the responsibility of creating the socket from the code I am testing, and allow the same socket to be reused for testing. 这将消除我正在测试的代码中创建套接字的责任,并允许将相同的套接字重用于测试。

Is one of these two the reccomended method to unit testing that involves UDP sockets? 这是涉及UDP套接字的单元测试的推荐方法之一吗? Or is there another approach I am missing? 还是我错过了另一种方法?

Edit: For the sake of anyone with the same misinterpretation as I had, they should know that a UDP socket can be null constructed DatagramSocket ds = new DatagramSocket(); 编辑:为了与我有相同的误解,他们应该知道UDP套接字可以为null构造DatagramSocket ds = new DatagramSocket(); and therefore not binded to a port on instantiation. 因此在实例化时未绑定到端口。 This avoids having two UDP sockets listening on the same port. 这样可以避免让两个UDP套接字监听同一端口。 For the sake of brevity, here is a code snippet that avoids the problem I described. 为了简洁起见,下面是一个代码片段,它避免了我描述的问题。

socketOut = new DatagramSocket(PORT, addressOut);   
socketIn = new DatagramSocket(PORT+1); 
socketOut.send(new DatagramPacket(Payload, Payload.length, addressOut, PORT+1 )); 
socketIn.receive(new DatagramPacket(bufferIn, bufferIn.length));

The code you're testing should have two parameters for port, one for a local port to bind to and one for a remote port to send to. 您要测试的代码应具有两个用于端口的参数,一个用于绑定到本地端口的参数,一个用于发送到远程端口的参数。 That way, both endpoints can live on the same machine. 这样,两个端点都可以位于同一台计算机上。 Then in your unit test code, you bind to the "remote" port and send to the "local" port. 然后,在单元测试代码中,绑定到“远程”端口并发送到“本地”端口。

For example, your class is set to bind on port 7000 and send on port 7001. So outgoing packets will have a source port of 7000 and a destination port of 7001. Your unit test code would then bind on port 7001 and send on port 7000. 例如,您的类设置为在端口7000上绑定并在端口7001上发送。因此,传出数据包将具有源端口7000和目标端口7001。然后,您的单元测试代码将在端口7001上绑定并在端口7000上发送。

暂无
暂无

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

相关问题 如何对使用 Java UUID 的代码进行单元测试? - How do I unit test code which uses Java UUID? 无法对 Java 代码使用 Mockito 或 PowerMockito 的反射进行单元测试 - Cannot unit test Java code uses reflection with Mockito nor PowerMockito 单元测试-为什么我的单元测试在使用finish(),处理程序和线程的代码上失败? - unit test - why does my unit test fail on this code that uses finish(), handler, and thread? 在Java中的源代码和测试之间共享资源的推荐方法是什么? - What is the recommended way to share resources between source code and tests in java? 针对不同的依赖项集构建相同代码的推荐方法是什么? - What is the recommended way to build the same code against different sets of dependencies? 测试使用Java中的另一个比较器的比较器的最佳方法是什么? - What's the best way to test a comparator that uses another comparator in Java? 我应该如何对使用 google guava 库的代码进行单元测试,尤其是 io package 中的代码? - How should I unit test code that uses the google guava libraries, especially stuff in the io package? 单元测试REST端点(Jersey)的最佳方法是什么 - What is the best way to unit test REST Endpoints (Jersey) 将工作委托给其他人的单元测试类的最佳方法是什么? - What's the best way to unit test classes that delegate the work to others? 使用 Junit 在 Spring 引导中编写单元测试的更好方法是什么? - What is better way to write Unit test in Spring Boot using Junit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM