jsevellec / cassandra-unit

Utility tool to load Data into Cassandra to help you writing good isolated JUnit Test into your application
GNU Lesser General Public License v3.0
425 stars 0 forks source link

netty not included in cassandra-unit-shaded 4.3.1.0 #307

Open TvdW opened 4 years ago

TvdW commented 4 years ago

The pom.xml of cassandra-unit-shaded specifies that it should shade io.netty, however for some reason it doesn't end up doing so, as of the 4.3.1.0 release. This renders the shaded package useless, as it cannot start Cassandra.

Compare:

tom:~/.m2/repository/org/cassandraunit/cassandra-unit-shaded$ less 3.7.1.0/cassandra-unit-shaded-3.7.1.0.jar | grep io/netty | wc -l
1075
tom:~/.m2/repository/org/cassandraunit/cassandra-unit-shaded$ less 4.3.1.0/cassandra-unit-shaded-4.3.1.0.jar | grep io/netty | wc -l
0

I suspect this is somehow related to changes in the dependency tree from the com.datastax.oss modules, but it's not clear to me what the fix should be.

TvdW commented 4 years ago

... this would be even better if I mentioned what actually goes wrong.

java.lang.NoClassDefFoundError: org/cassandraunit/shaded/io/netty/util/concurrent/FastThreadLocal

    at org.apache.cassandra.config.DatabaseDescriptor.applySimpleConfig(DatabaseDescriptor.java:614)
    at org.apache.cassandra.config.DatabaseDescriptor.applyAll(DatabaseDescriptor.java:324)
    at org.apache.cassandra.config.DatabaseDescriptor.daemonInitialization(DatabaseDescriptor.java:153)
    at org.apache.cassandra.config.DatabaseDescriptor.daemonInitialization(DatabaseDescriptor.java:137)
    at org.cassandraunit.utils.EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.java:145)
    at org.cassandraunit.utils.EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.java:108)
    at org.cassandraunit.utils.EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.java:88)
    at my.package.CassandraTestUtils.setUp(CassandraTestUtils.java:42)
    at my.package.CassandraTestCase.setUp(CassandraTestCase.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.ClassNotFoundException: org.cassandraunit.shaded.io.netty.util.concurrent.FastThreadLocal
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 44 more
bapostol91 commented 4 years ago

same issue happens to me...

PeterFras commented 4 years ago

We're in the same boat. Latest version of the library suffers from ClassNotFound exceptions.

Crovitz commented 4 years ago

I also had the above problem, I understood that there is currently no library that would allow creating embedded Cassandra for testing with Spring 2.3.x and Java 11.

bapostol91 commented 4 years ago

hi, for the moment i used test containers for testing cassandra in java 11 and worked like a charm.

All you need to do is:

GenericContainer<?> cassandra =
                    new GenericContainer<>("cassandra:3").withExposedPorts(9042);

            cassandra.start();
holub commented 4 years ago

I also had the above problem, I understood that there is currently no library that would allow creating embedded Cassandra for testing with Spring 2.3.x and Java 11.

I've managed non shaded version work with SpringBoot 2.3.1

  <dependencies>
    <dependency>
      <groupId>org.cassandraunit</groupId>
      <artifactId>cassandra-unit-spring</artifactId>
      <scope>test</scope>
      <version>4.3.1.0</version>
      <exclusions>
        <exclusion>
          <artifactId>junit</artifactId>
          <groupId>junit</groupId>
        </exclusion>
        <exclusion>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-validator</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

You must also make sure you use old version of Google Guava in the project 25.1-jre

Chr3is commented 4 years ago

I switched to testcontainers as well:

@EnableAutoConfiguration
@ContextConfiguration(initializers = MyRepositoryTest.Initializer)
@EnableCassandraRepositories(basePackages = "my.package.repository")
class MyRepositoryTest extends Specification {

    static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

        @Override
        void initialize(ConfigurableApplicationContext applicationContext) {

            CassandraContainer cassandra = new CassandraContainer()
            cassandra.withInitScript(new ClassPathResource('setup.cql').path)
            cassandra.start()

            TestPropertyValues.of(
                    "spring.data.cassandra.contact-points=127.0.0.1",
                    "spring.data.cassandra.port=${cassandra.getMappedPort(9042)}",
                    'spring.data.cassandra.local-datacenter=datacenter1',
                    'spring.data.cassandra.keyspace-name=my_key_space'
            ).applyTo(applicationContext);
        }
    }
...
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>testcontainers</artifactId>
            <version>1.14.3</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>cassandra</artifactId>
            <version>1.14.3</version>
            <scope>test</scope>
        </dependency>

Inspirered from https://stackoverflow.com/questions/57529031/how-to-use-testcontainers-cassandra-with-spring-data-for-apache-cassandra

Crovitz commented 4 years ago

Test containers are the great way for local junit tests, but unfortunately for eg jenkins when we don't want to use docker is useless. I'm still thinking how to test cassandra without docker :/

Chr3is commented 4 years ago

@Crovitz Why do you don't want to use docker on your jenkins? We're using these testcontainers without any issues on our jenkins nodes eg. for frontendtests with different browsers and now for cassandra.

Crovitz commented 4 years ago

@Chr3is okey perhaps i should change my deployment way, but does it really affect the length of deploy, because it must run docker and container with exposed port?

Chr3is commented 4 years ago

@Crovitz in our case we have nodes which run directly on hardware and nodes which are run on vms. But docker is always directly installed on the host so builds running with jenkins can access the docker deamon. This means builds won't start a docker deamon - the deamon is running the whole time. The most expensive part is probably downloading the image but this should be cached on the host machine after the first download/build. In terms of performance you should be fine. And a big advantage is that you do not depend on an os. So your tests can be run without any problems on every os eg. windows, mac, linux they just need to have docker installed (which is/should probably always the case nowadays). You should give it a try and make your own expirience :).

Crovitz commented 4 years ago

@Chr3is thank you for reply, sound like a great solution. I will try to do it like you ;)

stonedu1011 commented 4 years ago

I'm having the same issue. The cause of it is that io.netty is transparent dependency of Cassandra's java-driver-core, which is marked "optional" in cassandra-unit. Therefore, cassandra-shaded doesn't have dependency on io.netty at all.

I solved this issue by having my own cassandra-unit-repack project that tweak the dependency and shading configurations. It works fine with spring-boot 2.3.1 and Java 11.

Hope this helps

jsevellec commented 4 years ago

Hi @stonedu1011 , Do you mind to provide a PR about that?

GDownes commented 2 years ago

Is there any workaround for this issue, enabling the use of the shaded jar? Any plan to merge the Pull Request?