无法使用Phantom

时间:2016-11-22 06:49:03

标签: scala cassandra phantom-dsl

关注指南here我尝试为带有嵌入式存储的Cassandra构建测试套件。但是当我运行sbt test时,我收到以下错误: Error connecting to localhost/127.0.0.1:9142 (Connection refused: localhost/127.0.0.1:9142)

所以这里是我的代码(我将所有部分放在一起):

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
    }

  }
}

看起来嵌入式存储设备尚未启动。我必须以某种方式手动启动吗?或者我认为我需要本地运行的Cassandra实例?我使用build.sbt

此处参考完整的日志:

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

2 个答案:

答案 0 :(得分:1)

您的cassandra正在localhost:9042上运行,但您的嵌入式cassandra已连接到localhost:9142

出于测试目的,您可以在添加org.cassandraunit的依赖项后使用 EmbeddedCassandraServerHelper : -

  override def beforeAll(): Unit = {
    EmbeddedCassandraServerHelper.startEmbeddedCassandra("test-cassandra.yaml", 1000000L)
    database.create()
  }

  override def afterAll(): Unit = {
    database.truncate()
  }

确保添加了 test-cassandra.yaml 文件。您无需创建TestConnector进行测试。 希望这会有所帮助!!

答案 1 :(得分:0)

所以我最后在this示例后回滚到版本1.22.0。唯一的区别是将PhantomSbtPlugin.projectSettings添加到build.sbt。可能在教程中遗漏了一些内容,应该添加到build.sbt以激活插件,因为据我所知,插件未激活,因此嵌入式实例未启动。

如果您知道如何修复它以使用最新版本的插件,请随时争辩或编辑我的答案。