且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在没有网络的情况下创建嵌入进程内/内存中的Hazelcast实例?

更新时间:2021-11-27 23:13:24

回答我自己的问题:

看来这是不可能的,但是我在下面提到的单元测试目的有一个很好的解决方案。看看Hazelcast的源代码,网络代码似乎是在Node构建时自动执行的,并且没有任何Config操作对我有用。如果可能的话,我很乐意以其他方式展示。

It appears that this isn't possible, but there is a good solution for unit testing purposes which I mention below. Looking at Hazelcast's source code, it appears that networking code is automatically executed upon Node construction, and no amount of Config manipulation worked for me. I'd love to be shown otherwise if possible.

无论如何,我能够完成单元测试所需的工作:

In any event, I was able to accomplish what I needed for unit testing:

作为EasyMock的长期用户,我不知道如何干净地测试调用 Hazelcast.newHazelcastInstance(config); 的代码,因为它是一个静态方法调用。这实际上是促使我提出这个问题的原因 - 我只想要一个仅在内存中的Hazelcast实例进行测试。我不希望在我们的受限构建机器上尝试网络操作 - 我不知道机器是否足够受限,以至于Hazelcast的发现逻辑可能会使构建失败。

As a long time user of EasyMock, I wasn't aware how to cleanly test the code that invoked Hazelcast.newHazelcastInstance(config); since it was a static method call. This is in fact what prompted me to ask this question - I just wanted an in-memory-only Hazelcast instance for testing. I did not want network operations to be attempted on our restricted build machine - I wasn't aware if the machine was restricted enough such that Hazelcast's discovery logic might fail the build.

然后我找到了 PowerMock 对EasyMock的扩展,允许我模拟静态方法调用。

I then found PowerMock's extension to EasyMock, allowing me to mock static method calls.

使用EasyMock和PowerMock,我能够在项目中完全对所有与Hazelcast相关的代码进行单元测试,而无需实际启动Hazelcast环境。

With EasyMock and PowerMock, I was able to fully unit test all Hazelcast-related code in our project without actually starting a Hazelcast environment.