模拟网络断开连接以在本地测试分布式应用程序分区

发布于 2024-09-06 18:42:25 字数 594 浏览 8 评论 0原文

我有几个在本地主机上运行的分布式应用程序实例;每个实例都通过某些端口与其他实例通信,所有实例一起构成一个整体。 (我实际上是在谈论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北陌 2024-09-13 18:42:25

也许这个答案会为某人节省几个小时的谷歌搜索时间。

Linux 有一个非常好的防火墙实用程序,iptables。它允许您阻止进程之间的通信,如下所示:

iptables -A INPUT -p tcp --sport <source port> --dport <dest port> -j DROP

虽然从任何角度来看都不是一个成熟的单元测试框架,但这在我的情况下对手动测试有所帮助。

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:

iptables -A INPUT -p tcp --sport <source port> --dport <dest port> -j DROP

While not a full-blown unit testing framework by any measure, this helps a bit with manual testing in my case.

゛清羽墨安 2024-09-13 18:42:25

我很想说这是集成测试而不是单元测试(或者很难对此类事情进行适当的单元测试)。

有几次我需要测试此类东西,我使用虚拟化(例如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.

潦草背影 2024-09-13 18:42:25

过去我曾断开过网络电缆。您始终可以通过操作系统禁用网络接口。

In the past I have disconnected network cables. You can always disable a network interface via the OS.

夏日落 2024-09-13 18:42:25

尝试封锁。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文