0

How can I achieve unit tests for caching using jCache and HazelcastProvider?

Currently I'm getting this error:

Caused by: java.lang.IllegalStateException: Unable to connect to any address in the config! The following addresses were tried: [localhost/127.0.0.1:5701, localhost/127.0.0.1:5702, localhost/127.0.0.1:5703]

Simplified code below:

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource("classpath:provider.properties")
@ContextConfiguration(classes = {
    CustomCacheConfiguration.class
})

I have cache declared in hazelcast.xml file along with multicast, so as I understand it correctly, I should have an embedded instance.

//EDIT

here is the hazelcast.xml file:

<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.hazelcast.com/schema/config
                           http://www.hazelcast.com/schema/config/hazelcast-config-3.8.xsd"
       xmlns="http://www.hazelcast.com/schema/config">
    <group>
        <name>test</name>
        <password>test</password>
    </group>
    <network>
        <join>
            <multicast enabled="true"/>
        </join>
     </network>

    <!-- cache definitions -->
</hazelcast>

And here are the logs:

Jun 28, 2017 10:55:45 AM com.hazelcast.client.config.XmlClientConfigLocator
INFO: Loading 'hazelcast-client-default.xml' from classpath.
Jun 28, 2017 10:55:45 AM com.hazelcast.client.config.XmlClientConfigLocator
INFO: Loading 'hazelcast-client-default.xml' from classpath.
Jun 28, 2017 10:55:45 AM com.hazelcast.core.LifecycleService
INFO: hz.client_0 [dev] [3.8.2] HazelcastClient 3.8.2 (20170518 - a60f944) is STARTING
Jun 28, 2017 10:55:46 AM com.hazelcast.core.LifecycleService
INFO: hz.client_0 [dev] [3.8.2] HazelcastClient 3.8.2 (20170518 - a60f944) is STARTED
Jun 28, 2017 10:55:46 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
INFO: hz.client_0 [dev] [3.8.2] Trying to connect to [127.0.0.1]:5701 as owner member
Jun 28, 2017 10:55:46 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
INFO: hz.client_0 [dev] [3.8.2] Trying to connect to [127.0.0.1]:5702 as owner member
Jun 28, 2017 10:55:46 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
INFO: hz.client_0 [dev] [3.8.2] Trying to connect to [127.0.0.1]:5703 as owner member
Jun 28, 2017 10:55:46 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
WARNING: hz.client_0 [dev] [3.8.2] Unable to get alive cluster connection, try in 2940 ms later, attempt 1 of 2.
Jun 28, 2017 10:55:49 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
INFO: hz.client_0 [dev] [3.8.2] Trying to connect to [127.0.0.1]:5703 as owner member
Jun 28, 2017 10:55:49 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
INFO: hz.client_0 [dev] [3.8.2] Trying to connect to [127.0.0.1]:5702 as owner member
Jun 28, 2017 10:55:49 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
INFO: hz.client_0 [dev] [3.8.2] Trying to connect to [127.0.0.1]:5701 as owner member
Jun 28, 2017 10:55:49 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
WARNING: hz.client_0 [dev] [3.8.2] Unable to get alive cluster connection, try in 2996 ms later, attempt 2 of 2.
Jun 28, 2017 10:55:52 AM com.hazelcast.core.LifecycleService
INFO: hz.client_0 [dev] [3.8.2] HazelcastClient 3.8.2 (20170518 - a60f944) is SHUTTING_DOWN
Jun 28, 2017 10:55:52 AM com.hazelcast.core.LifecycleService
INFO: hz.client_0 [dev] [3.8.2] HazelcastClient 3.8.2 (20170518 - a60f944) is SHUTDOWN
Forin
  • 1,549
  • 2
  • 20
  • 44
  • Can you post the logs ? Might be Hazelcast isn't starting. Strictly this wouldn't be a unit test, and you might want to use mock objects. Alternatively, have a look at https://github.com/hazelcast/hazelcast/tree/master/hazelcast/src/test/java/com/hazelcast/cache to see how tests are run there. – Neil Stevenson Jun 28 '17 at 08:52
  • 1
    You should check the classpath and other beans. The `hazelcast.xml` file would be used by a Hazelcast server, and either you aren't starting one or it's not getting started soon enough. The error message you have is from a `HazelcastClient`. A client is starting and can't find a server to connect to -- so either you're starting a client instead of a server, or the client before the server. – Neil Stevenson Jun 28 '17 at 19:11
  • Problem is that I am not starting a client anywhere, it seems, that during testing hazelcast cannot load my xml file, so loads default one for client. – Forin Jun 29 '17 at 08:22
  • `com.hazelcast:hazelcast-client` depends on `com.hazelcast:hazelcast` - the client depends on the server and not the reverse. So the server code can *never* start the client. But clearly something in your set-up is. Can you post a full example to Github and link to it here ? Alternatively, ensure you remove `com.hazelcast:hazelcast-client` from the classpath and see what happens. Worth also checking how you have "hazelcast.jcache.provider.type" set. – Neil Stevenson Jun 29 '17 at 10:35
  • I think I've found a solution, I posted it as an answer. Do you have any suggestions, concerns? – Forin Jun 29 '17 at 14:14

1 Answers1

0

I think I've found a solution.

I created new hazelcast-test.xml file with following XML:

<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.hazelcast.com/schema/config
                           http://www.hazelcast.com/schema/config/hazelcast-config-3.9.xsd"
       xmlns="http://www.hazelcast.com/schema/config">

    <properties>
        <property name="hazelcast.shutdownhook.enabled">false</property>
    </properties>

    <network>
        <join>
            <multicast enabled="false"/>
            <tcp-ip enabled="false"/>
        </join>
    </network>

    <import resource="classpath:hazelcast-cache-declarations.xml"/>
</hazelcast>

And then in tests I added this:

@BeforeClass
public void init() {
    Config config = new ClasspathXmlConfig("hazelcast-test.xml");
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
}

@AfterClass
public void clean() {
    Hazelcast.shutdownAll();
}
Forin
  • 1,549
  • 2
  • 20
  • 44
  • You create the Hazelcast instance in `@Before` not `@BeforeClass`, so you will have more than one if you have multiple `@Test` methods. That could explain the error, but really it needs more code uploaded before any cause would be obvious. – Neil Stevenson Jun 29 '17 at 16:45
  • I've fixed it, thanks for pointing `@BeforeClass` annotation. I've changed the answer, now all tests work. – Forin Jul 04 '17 at 09:33