如何测试复杂的异步网络代码并遵循 tdd

how to test complex asynchronous networked code and follow tdd

单元测试网络异步代码的最佳做法是什么?我正在尝试 do/learn tdd

我目前正在规划图书馆的这一部分,但原则上: 这是一个 ssh 客户端库。我希望它是异步的。实际上,ssh 连接过程非常复杂。我的连接方法将以原子方式设置一些连接状态变量,然后使用某种任务执行器来安排连接任务。连接需要连接到服务器,通过发送和接收ssh协议版本之类的介绍,然后完成一个密钥交换过程,这本身分为几种情况,因为密钥交换算法很少,每个算法都需要交换不同的数据包。

虽然我听说我应该测试public api,并通过测试使用它的public方法来测试私有方法,但在这种情况下似乎很难,因为任务非常复杂,只伪造协商的一部分可能比整个 connection/negotiation 更容易,只是为了检查连接方法的每个可能结果,包括每个密钥交换算法的结果。

将较大的连接任务拆分成较小的任务是否是一个很好的理由,即使它们并非 public 仅对用户可用,并且测试每个单独的连接阶段而不是仅测试整个连接方法立刻?它是否以某种方式打破了最佳实践,或者如何以不同的方式做到这一点?例如,它是否在测试实施细节?

What would be the best practices for unit testing networked asynchronous code? I am trying to do/learn tdd

您需要阅读的参考文献是 Growing Object Oriented Software,作者是 Freeman 和 Price。该文本详细介绍了如何使用测试来开发异步网络拍卖客户端。

正如作者所描述的那样,该过程预先加载了大量工作以进行初始端到端测试,并且 运行 首先,在开始填写其他详细信息之前。

当然,这不是唯一的方法。

Although I heard that I should test public api, and test private methods by testing public methods that use it

是的,而且...

in this case it seems difficult, as the task is quite complex and it is probably easier to fake only parts of a negotiation versus the whole connection/negotiation, just to check each possible result of a connect method including results of every key exchange algorithm.

经常发生的情况是,一个复杂的解决方案可以分解成多个模块,每个模块都包含自己的 "public API" -- 请参阅 On the Criteria to be Used in Decomposing Systems into Modules,作者:Parnas。然后您可以单独测试模块。

例如,你的代码经常会被组织成两堆;内部功能核心,然后是与 boundary of your system.

交互的命令式 shell

通常来说,功能核心比命令式shell更容易测试,所以争取shell即"so simple that there are obviously no deficiencies."

so what is the definition of public api?

粗略地说:可在实施范围之外访问的可供性。

换句话说,它们是模块的一部分,如果不重写调用模块的代码就无法更改。

in this case I would probably split connection process into subtasks, like connection, ssh introduction and key exchange. And test individual subtasks in isolation. Also I would test key exchange support in isolation from specific key exchange algorithm implementations. Requirements for testing each of those parts are different, and only the first requires mocking a socket.

您可能还想看看 Cory Benfield 的演讲 Building Protocol Libraries the Right Way

Not sure if that is okay or not.

如果你不这样做,TDD 警察就不会来敲你的门 "right"。在最坏的情况下,他们会给你写 a nasty note.