模拟网络断开连接以在本地测试分布式应用程序分区
我有几个在本地主机上运行的分布式应用程序实例;每个实例都通过某些端口与其他实例通信,所有实例一起构成一个整体。 (我实际上是在谈论 ZooKeeper,在 Linux 上运行)
现在我想编写单元测试来模拟整体划分。
例如,我有 5 个实例,我想将它们分成两组,每组 3 个和 2 个,以便一组中的实例无法与另一组中的实例进行通信。它将模拟现实世界的情况,即一个数据中心有 3 台机器,另一个数据中心有 2 台机器,并且数据中心被分区。
问题本质上是让套接字有选择地工作:与一个套接字通信,但不与另一个套接字通信。我想到的一个解决方案是抽象通信层并向其中注入测试规则(形式为“如果我是一个组中的实例,则不允许与另一组中的实例进行对话 - 关闭套接字”)或忽略数据或其他什么”)。
但也许有一些工具可以实现这一点,也许有一些测试框架? 一般来说,您如何在分布式应用程序中测试此类情况?
PS 虽然问题被标记为“java”(因为 ZooKeeper 是用 Java 编写的),但很高兴听到任何其他语言或独立于语言的解决方案——也许是一些 Linux 大师的技巧。
I have several instances of a distributed application running on the localhost; every instance communicate with others through certain ports, all instances together make an ensemble.
(I'm actually talking about ZooKeeper, running on Linux)
Now I want to write unit tests to emulate ensemble partitioning.
E.g. I have 5 instances, and I want to split them into two groups of 3 and 2 so that an instance from one group couldn't communicate to an instance from another group. It would emulate real-world situation when 3 machines are in one datacenter, 2 machines are in another one, and datacenters become partitioned.
The problem is essentially to make a socket work selectively: speak to one socket, but don't speak to another. One solution that comes to mind is to abstract communication layer and inject testing rules into it (in the form of "if I'm an instance from one group I'm not not allowed to speak to an instance from another group -- close socket or ignore data or whatever").
But maybe there exist some tools for this, maybe some testing framework?
In general, how do you test such cases in your distributed apps?
P.S. Though question is tagged with "java" (since ZooKeeper is written in Java), it'd be great to hear solutions for any other language, or language-independent ones -- maybe some linux guru-tricks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许这个答案会为某人节省几个小时的谷歌搜索时间。
Linux 有一个非常好的防火墙实用程序,
iptables
。它允许您阻止进程之间的通信,如下所示:虽然从任何角度来看都不是一个成熟的单元测试框架,但这在我的情况下对手动测试有所帮助。
Maybe this answer will save a few hours of googling for someone.
Linux has a very good firewall utility,
iptables
. It allows you to block communication between processes, like this:While not a full-blown unit testing framework by any measure, this helps a bit with manual testing in my case.
我很想说这是集成测试而不是单元测试(或者很难对此类事情进行适当的单元测试)。
有几次我需要测试此类东西,我使用虚拟化(例如VMWare)来测试系统。您可以从运行多个映像的一台物理机器上测试所有节点的重新启动、关闭等。
I would be tempted to say that this is integration testing rather than unit testing (or that it will be really hard to do proper unit testing of such thing).
The few times I needed to test such things, I've used virtualisation (e.g. VMWare) to test the system. You can test reboot, shutdown, etc. of nodes all from one physical machine running several images.
过去我曾断开过网络电缆。您始终可以通过操作系统禁用网络接口。
In the past I have disconnected network cables. You can always disable a network interface via the OS.
尝试封锁。 https://github.com/dcm-oss/blockade
这是一个基于 docker 的工具,用于模拟你想要的行为。仅在 Linux 上运行,但如果您想从其他操作系统运行它,可以使用 Vagrant 文件进行安装。
try blockade. https://github.com/dcm-oss/blockade
It's a docker based tool to emulate the behaviour you want. Runs on linux only, but there is a Vagrant file for setup if you want to run it from another OS.