我有一些客户端代码使用我想要测试的流与服务器进行通信。
我想测试客户端是否向服务器发送了正确的命令。
为此,我创建了一个 Connect 方法,该方法采用两个流:一个用于客户端读取,一个用于客户端写入。然后,我可以向两者提供 MemoryStream,并读取从客户端写入的 MemoryStream 发送的命令。
我的问题是我需要编写一个方法,要求这两个流相同。我还注意到,我唯一需要使用两个不同流的时候是当我想测试客户端时。因此,我想重构 Connect 方法以仅采用一个流,该流将被写入和读取。
我无法将 MemoryStream 传递给此方法,因为 write 方法写入某个内部字节数组,而 read 方法也从中读取。如果我将其提供给客户端,它将能够读取它刚刚发送的命令!
因此我想问什么流设置可以做到这一点?我只是想要一个从一个源读取但写入另一个源的流。我需要访问这两个源,以便我的测试“服务器”可以发送对命令的正确响应,并读取客户端发送的命令。
本质上我需要两个流:S1 和 S2。一个流提供给客户端,另一个流提供给服务器。
S1的读方法与S2的写方法“连接”=> S1 读取 S2 写入的内容。
S1的写方法与S2的读方法“连接”=> S2 读取 S1 写入的内容。
这正是客户端与服务器通信时的正常情况。这里我只是想在记忆中做一下。我该如何编码?
I have some client code that communicates with a server using streams that I want to test.
I want to test that the client sends the correct commands to the server.
To do this I have created a Connect method that takes two streams: one the client reads from and one the client writes to. I can then supply a MemoryStream to both, and read the commands sent off the MemoryStream that the client writes to.
My problem is that I need to code a method that requires those two streams to be the same. I have also noticed, that the only time I need to use two different streams, is when I want to test the client. Therefore I want to refactor the Connect method to take only one stream, which will both be written to and read from.
I cant pass a MemoryStream to this method, since the write method writes to some internal byte array, from which the read method also reads from. If I give this to a client, it will be able to read the commands it just sent!
Therefore I would ask what stream setup can do this? I simply want a stream that reads from one source, but writes to another. I would need access to both sources, so that the "server", which is my test, can send the correct responses to commands, and read the commands the client sent.
In essence I need two streams: S1 and S2. One stream is given to the client, the other to the server.
S1's read method is "connected" to S2's write method => S1 reads what S2 writes.
S1's write method is "connected" to S2's read method => S2 reads what S1 writes.
This is exactly the normal case when a client communicates with a server. Here I just want to do it in memory. How can I code this?
发布评论
评论(3)
为什么不从命令的传输中抽象出命令的发送呢?
我的意思是,您有一个类决定需要发送哪些命令,另一个类决定发送命令的方式。这样,您可以通过模拟传输类来测试命令类是否正在生成正确的命令(以正确的顺序),而无需担心流的实现细节。
然后,传输类的实现将只获取提供给它的数据并将其直接写入通常的任何流,而无需考虑使其与命令类测试一起工作。该类将更难测试,但到那时您已经确定正在生成正确的命令,因此需要进行集成测试以确保它们被成功编写。
Why not abstract the sending of the commands from the transport of the commands?
By that I mean you have one class that decides what commands need to be sent and another that dictates the means by which they're sent. That way you can test that the command class is generating the correct commands (in the correct order) by mocking away the transport class without ever having to worry about the implementation detail of streams.
The implementation of transport class would then just take the data given to it and write it directly to whatever stream it would normally without you having to think about making it work with the command class tests. That class would be harder to test, but by that point you've established the correct commands are being generated so it's integration testing to ensure they're being written successfully.
您可以创建 Stream 的子类,该子类在构造函数中接受另外两个流,然后重写
Read
和Write
方法以在正确的流中读取和写入。请务必阅读 MSDN 页面上的实施者注意事项部分,以获取
Stream
类。You can create a subclass of Stream that takes two other streams in the constructor and then override the
Read
andWrite
methods to read and write in the correct streams.Be sure to read the Notes to Implementers section on the MSDN page for the
Stream
class.您可以创建封装两个内存流的双向流。您从一个读取并写入另一个。
You can create a bidirectional stream which encapsulates two memory streams. You read from one and write to another.