Reputation: 477
Following guide here I try to build a test suite for Cassandra with embedded storage. However when I run sbt test
I get the following error:
Error connecting to localhost/127.0.0.1:9142 (Connection refused: localhost/127.0.0.1:9142)
.
So here's my code (I put all the pieces together):
case class User(
id: String,
items: Map[String, Int]
)
class CassandraStorage(val keyspace: KeySpaceDef) extends Database(keyspace) {
object users extends Users with keyspace.Connector
}
object TestConnector {
val connector = ContactPoint.embedded.keySpace("test")
}
object TestDb extends CassandraStorage(TestConnector.connector)
trait TestDatabaseProvider extends DatabaseProvider[TestDb] {
override def database = TestDb
}
trait DatabaseTest extends Suite
with BeforeAndAfterAll
with ScalaFutures
with Matchers
with OptionValues
with TestDatabaseProvider
with TestConnector.connector.Connector {
override def beforeAll(): Unit = {
super.beforeAll()
// Automatically create every single table in Cassandra.
database.create()
}
}
class UserDatabaseTests extends FlatSpec with DatabaseTest {
it should "store a user in the database and retrieve it" in {
val user = User("test_user", Map("a" -> 1))
val chain = for {
store <- database.users.store(user)
get <- database.users.getById(user.id)
} yield get
whenReady(chain) {
res =>
res shouldBe defined
res.value shouldEqual user
}
}
}
Looks like the embedded storage hasn't started. Do I have to start it somehow manually? Or is it supposed that I need local running instance of Cassandra? I use build.sbt
.
For reference here's the complete log:
13:30:10.173 [pool-52-thread-4] DEBUG com.datastax.driver.core.SystemProperties - com.datastax.driver.NEW_NODE_DELAY_SECONDS is undefined, using default value 1
13:30:10.178 [pool-52-thread-4] DEBUG com.datastax.driver.core.SystemProperties - com.datastax.driver.NON_BLOCKING_EXECUTOR_SIZE is undefined, using default value 4
13:30:10.180 [pool-52-thread-4] DEBUG com.datastax.driver.core.SystemProperties - com.datastax.driver.NOTIF_LOCK_TIMEOUT_SECONDS is undefined, using default value 60
13:30:10.303 [pool-52-thread-4] DEBUG com.datastax.driver.core.Cluster - Starting new cluster with contact points [localhost/127.0.0.1:9142]
13:30:10.342 [pool-52-thread-4] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
13:30:10.353 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
13:30:10.353 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
13:30:10.353 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
13:30:10.353 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: true
13:30:10.358 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent - Java version: 8
13:30:10.358 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noUnsafe: false
13:30:10.358 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available
13:30:10.358 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noJavassist: false
13:30:10.360 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent - Javassist: unavailable
13:30:10.360 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent - You don't have Javassist in your class path or you don't have enough permission to load dynamically generated classes. Please check the configuration for better performance.
13:30:10.361 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: /tmp (java.io.tmpdir)
13:30:10.361 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
13:30:10.361 [pool-52-thread-4] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
13:30:10.386 [pool-52-thread-4] DEBUG com.datastax.driver.core.SystemProperties - com.datastax.driver.FORCE_NIO is undefined, using default value false
13:30:10.388 [pool-52-thread-4] INFO com.datastax.driver.core.NettyUtil - Did not find Netty's native epoll transport in the classpath, defaulting to NIO.
13:30:10.406 [pool-52-thread-4] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 8
13:30:10.417 [pool-52-thread-4] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
13:30:10.417 [pool-52-thread-4] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
13:30:10.444 [pool-52-thread-4] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
13:30:10.444 [pool-52-thread-4] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.maxRecords: 4
13:30:10.452 [pool-52-thread-4] DEBUG com.datastax.driver.core.SystemProperties - com.datastax.driver.EXTENDED_PEER_CHECK is undefined, using default value true
13:30:10.534 [pool-52-thread-4] DEBUG com.datastax.driver.core.Host.STATES - [localhost/127.0.0.1:9142] preparing to open 1 new connections, total = 1
13:30:10.665 [pool-52-thread-4] DEBUG com.datastax.driver.core.SystemProperties - com.datastax.driver.DISABLE_COALESCING is undefined, using default value false
13:30:10.696 [pool-52-thread-4] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 8
13:30:10.698 [pool-52-thread-4] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 8
13:30:10.698 [pool-52-thread-4] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192
13:30:10.699 [pool-52-thread-4] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 11
13:30:10.699 [pool-52-thread-4] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 16777216
13:30:10.699 [pool-52-thread-4] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.tinyCacheSize: 512
13:30:10.699 [pool-52-thread-4] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256
13:30:10.699 [pool-52-thread-4] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64
13:30:10.699 [pool-52-thread-4] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
13:30:10.699 [pool-52-thread-4] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192
13:30:10.732 [pool-52-thread-4] DEBUG io.netty.util.internal.ThreadLocalRandom - -Dio.netty.initialSeedUniquifier: 0xbeb54a85e70f88b0 (took 1 ms)
13:30:10.779 [pool-52-thread-4] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: unpooled
13:30:10.779 [pool-52-thread-4] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 65536
13:30:10.779 [pool-52-thread-4] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
13:30:10.841 [cluster1-nio-worker-0] DEBUG com.datastax.driver.core.Connection - Connection[localhost/127.0.0.1:9142-1, inFlight=0, closed=false] Error connecting to localhost/127.0.0.1:9142 (Connection refused: localhost/127.0.0.1:9142)
13:30:10.846 [cluster1-nio-worker-0] DEBUG com.datastax.driver.core.Host.STATES - Defuncting Connection[localhost/127.0.0.1:9142-1, inFlight=0, closed=false] because: [localhost/127.0.0.1] Cannot connect
13:30:10.847 [cluster1-nio-worker-0] DEBUG com.datastax.driver.core.Host.STATES - [localhost/127.0.0.1:9142] preventing new connections for the next 1000 ms
13:30:10.847 [cluster1-nio-worker-0] DEBUG com.datastax.driver.core.Host.STATES - [localhost/127.0.0.1:9142] Connection[localhost/127.0.0.1:9142-1, inFlight=0, closed=false] failed, remaining = 0
13:30:10.848 [cluster1-nio-worker-0] DEBUG com.datastax.driver.core.Connection - Connection[localhost/127.0.0.1:9142-1, inFlight=0, closed=true] closing connection
13:30:10.861 [pool-52-thread-4] DEBUG com.datastax.driver.core.ControlConnection - [Control connection] error on localhost/127.0.0.1:9142 connection, no more host to try
com.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1] Cannot connect
at com.datastax.driver.core.Connection$1.operationComplete(Connection.java:158)
at com.datastax.driver.core.Connection$1.operationComplete(Connection.java:141)
at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:680)
at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:603)
at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:563)
at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:424)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:276)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:292)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:528)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection refused: localhost/127.0.0.1:9142
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:224)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:289)
... 6 common frames omitted
13:30:10.862 [pool-52-thread-4] DEBUG com.datastax.driver.core.Cluster - Shutting down
[info] DeferredAbortedSuite:
[info] Exception encountered when attempting to run a suite with class name: org.scalatest.DeferredAbortedSuite *** ABORTED ***
[info] java.lang.ExceptionInInitializerError:
[info] at TestDb$.<init>(MatrixActorSystemSpec.scala:15)
[info] at TestDb$.<clinit>(MatrixActorSystemSpec.scala)
[info] at TestDatabaseProvider$class.$init$(MatrixActorSystemSpec.scala:18)
[info] at UserDatabaseTests.<init>(MatrixActorSystemSpec.scala:35)
[info] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[info] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[info] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[info] at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
[info] at java.lang.Class.newInstance(Class.java:442)
[info] at org.scalatest.tools.Framework$ScalaTestTask.execute(Framework.scala:468)
[info] ...
[info] Cause: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1:9142 (com.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1] Cannot connect))
[info] at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:233)
[info] at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:79)
[info] at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1424)
[info] at com.datastax.driver.core.Cluster.init(Cluster.java:163)
[info] at com.datastax.driver.core.Cluster.connectAsync(Cluster.java:334)
[info] at com.datastax.driver.core.Cluster.connectAsync(Cluster.java:309)
[info] at com.datastax.driver.core.Cluster.connect(Cluster.java:251)
[info] at com.websudos.phantom.connectors.DefaultSessionProvider$$anonfun$3$$anonfun$4.apply(DefaultSessionProvider.scala:66)
[info] at com.websudos.phantom.connectors.DefaultSessionProvider$$anonfun$3$$anonfun$4.apply(DefaultSessionProvider.scala:66)
[info] at scala.concurrent.BlockContext$DefaultBlockContext$.blockOn(BlockContext.scala:53)
[info] ...
[trace] Stack trace suppressed: run last test:test for the full output.
[error] Could not run test UserDatabaseTests: java.lang.ExceptionInInitializerError
[info] Run completed in 1 second, 244 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 0, aborted 1
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
[info] *** 1 SUITE ABORTED ***
[error] Error during tests:
[error] UserDatabaseTests
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 4 s, completed Nov 22, 2016 1:30:11 PM
Upvotes: 1
Views: 1833
Reputation: 291
Your cassandra is running on localhost:9042
but your embedded cassandra is connecting to localhost:9142
.
For testing purpose, you can use EmbeddedCassandraServerHelper after adding the dependency for org.cassandraunit :-
override def beforeAll(): Unit = {
EmbeddedCassandraServerHelper.startEmbeddedCassandra("test-cassandra.yaml", 1000000L)
database.create()
}
override def afterAll(): Unit = {
database.truncate()
}
Make sure you have added test-cassandra.yaml file. You don't need to create TestConnector for testing. Hope this helps!!
Upvotes: 1
Reputation: 477
So I ended up with rolling back to version 1.22.0
following this example. The only difference was to add PhantomSbtPlugin.projectSettings
to build.sbt
. There's probably something missed in tutorial, something that should be added to build.sbt
to activate the plugin because as far as I get my problem was that plugin was not active hence embedded instance was not started.
Please feel free to argue or edit my answer if you have any idea of how can I fix it to use the latest version of plugin.
Upvotes: 0