简体   繁体   English

如何测试程序无法连接到互联网时的行为?

[英]How to test how a program behaves when it can't connect to the internet?

I'm writing an RSS reader, and I want to gracefully handle situations where the internet connection is unavailable. 我正在写一个RSS阅读器,我想优雅地处理互联网连接不可用的情况。 What's a good way (on linux) to test program behavior in the absence of internet without pulling the cord and/or RF-killing everything? 在没有互联网的情况下测试程序行为而不拉动电源线和/或RF杀死所有东西的好方法(在Linux上)是什么?

As User left in a comment, you could test this by mocking your network access library. 当用户留下评论时,您可以通过模拟您的网络访问库来测试它。 At the point the library would normally access the network, you modify the behavior to grab a local file instead. 在库通常访问网络时,您可以修改行为以获取本地文件。 This post describes the technique more, and includes code samples for Python and urllib. 这篇文章更多地描述了该技术,并包含Python和urllib的代码示例。

From https://bbs.archlinux.org/viewtopic.php?id=83384 : 来自https://bbs.archlinux.org/viewtopic.php?id=83384

ifconfig eth0 down/up? ifconfig eth0 down / up? (or other interface instead of eth0) (或其他接口而不是eth0)

I can't test this, as I'm at work and need the Internet for stuff currently running, but hopefulyy this at least points you in the right direction. 我无法测试这个,因为我正在工作并且需要因特网上目前正在运行的东西,但是希望这至少能指向正确的方向。

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

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