PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/com/shorrockin/cascal/testing/CassandraTestPool.scala

http://github.com/shorrockin/cascal
Scala | 110 lines | 74 code | 20 blank | 16 comment | 2 complexity | 4cfd4a148dc0b2f2a10e4ec8d127fcfe MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.shorrockin.cascal.testing
  2. import org.apache.cassandra.thrift.CassandraDaemon
  3. import org.apache.cassandra.config.DatabaseDescriptor
  4. import java.io.File
  5. import java.net.ConnectException
  6. import org.apache.thrift.transport.{TTransportException, TSocket}
  7. import com.shorrockin.cascal.session._
  8. import com.shorrockin.cascal.utils.{Utils, Logging}
  9. /**
  10. * trait which mixes in the functionality necessary to embed
  11. * cassandra into a unit test
  12. */
  13. trait CassandraTestPool extends Logging {
  14. def borrow(f:(Session) => Unit) = {
  15. EmbeddedTestCassandra.init
  16. EmbeddedTestCassandra.pool.borrow(f)
  17. }
  18. }
  19. /**
  20. * maintains the single instance of the cassandra server
  21. */
  22. object EmbeddedTestCassandra extends Logging {
  23. import Utils._
  24. var initialized = false
  25. val hosts = Host("localhost", 9160, 250) :: Nil
  26. val params = new PoolParams(10, ExhaustionPolicy.Fail, 500L, 6, 2)
  27. lazy val pool = new SessionPool(hosts, params, Consistency.One)
  28. def init = synchronized {
  29. if (!initialized) {
  30. val homeDirectory = new File("target/cassandra.home.unit-tests")
  31. delete(homeDirectory)
  32. homeDirectory.mkdirs
  33. log.debug("creating cassandra instance at: " + homeDirectory.getCanonicalPath)
  34. log.debug("copying cassandra configuration files to root directory")
  35. val fileSep = System.getProperty("file.separator")
  36. val storageFile = new File(homeDirectory, "storage-conf.xml")
  37. val logFile = new File(homeDirectory, "log4j.properties")
  38. replace(copy(resource("/storage-conf.xml"), storageFile), ("%temp-dir%" -> (homeDirectory.getCanonicalPath + fileSep)))
  39. copy(resource("/log4j.properties"), logFile)
  40. System.setProperty("storage-config", homeDirectory.getCanonicalPath)
  41. log.debug("creating data file and log location directories")
  42. DatabaseDescriptor.getAllDataFileLocations.foreach { (file) => new File(file).mkdirs }
  43. new File(DatabaseDescriptor.getLogFileLocation).mkdirs
  44. val daemon = new CassandraDaemonThread
  45. daemon.start
  46. // try to make sockets until the server opens up - there has to be a better
  47. // way - just not sure what it is.
  48. val socket = new TSocket("localhost", 9160);
  49. var opened = false
  50. while (!opened) {
  51. try {
  52. socket.open()
  53. opened = true
  54. socket.close()
  55. } catch {
  56. case e:TTransportException => /* ignore */
  57. case e:ConnectException => /* ignore */
  58. }
  59. }
  60. initialized = true
  61. }
  62. }
  63. private def resource(str:String) = classOf[CassandraTestPool].getResourceAsStream(str)
  64. }
  65. /**
  66. * daemon thread used to start and stop cassandra
  67. */
  68. class CassandraDaemonThread extends Thread("CassandraDaemonThread") with Logging {
  69. private val daemon = new CassandraDaemon
  70. setDaemon(true)
  71. /**
  72. * starts the server and blocks until it has
  73. * completed booting up.
  74. */
  75. def startServer = {
  76. }
  77. override def run:Unit = {
  78. log.debug("initializing cassandra daemon")
  79. daemon.init(new Array[String](0))
  80. log.debug("starting cassandra daemon")
  81. daemon.start
  82. }
  83. def close():Unit = {
  84. log.debug("instructing cassandra deamon to shut down")
  85. daemon.stop
  86. log.debug("blocking on cassandra shutdown")
  87. this.join
  88. log.debug("cassandra shut down")
  89. }
  90. }