You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by "Ran Tavory (JIRA)" <ji...@apache.org> on 2010/01/26 08:04:34 UTC

[jira] Created: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Create InProcessCassandraServer for unit tests
----------------------------------------------

                 Key: CASSANDRA-740
                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
             Project: Cassandra
          Issue Type: New Feature
          Components: Contrib
            Reporter: Ran Tavory
             Fix For: 0.6


I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)

/**
 * An in-memory cassandra storage service that listens to the thrift interface.
 * Useful for unit testing,
 *
 * @author Ran Tavory (rantav@gmail.com)
 *
 */
public class InProcessCassandraServer implements Runnable {

  private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);

  CassandraDaemon cassandraDaemon;

  public void init() {
    try {
      prepare();
    } catch (IOException e) {
      log.error("Cannot prepare cassandra.", e);
    }
    try {
      cassandraDaemon = new CassandraDaemon();
      cassandraDaemon.init(null);
    } catch (TTransportException e) {
      log.error("TTransportException", e);
    } catch (IOException e) {
      log.error("IOException", e);
    }
  }

  @Override
  public void run() {
    cassandraDaemon.start();
  }

  public void stop() {
    cassandraDaemon.stop();
    rmdir("tmp");
  }


  /**
   * Creates all files and directories needed
   * @throws IOException
   */
  private void prepare() throws IOException {
    // delete tmp dir first
    rmdir("tmp");
    // make a tmp dir and copy storag-conf.xml and log4j.properties to it
    copy("/cassandra/storage-conf.xml", "tmp");
    copy("/cassandra/log4j.properties", "tmp");
    System.setProperty("storage-config", "tmp");

    // make cassandra directories.
    for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
      mkdir(s);
    }
    mkdir(DatabaseDescriptor.getBootstrapFileLocation());
    mkdir(DatabaseDescriptor.getLogFileLocation());
  }

  /**
   * Copies a resource from within the jar to a directory.
   *
   * @param resourceName
   * @param directory
   * @throws IOException
   */
  private void copy(String resource, String directory) throws IOException {
    mkdir(directory);
    InputStream is = getClass().getResourceAsStream(resource);
    String fileName = resource.substring(resource.lastIndexOf("/") + 1);
    File file = new File(directory + System.getProperty("file.separator") + fileName);
    OutputStream out = new FileOutputStream(file);
    byte buf[] = new byte[1024];
    int len;
    while ((len = is.read(buf)) > 0) {
      out.write(buf, 0, len);
    }
    out.close();
    is.close();
  }

  /**
   * Creates a directory
   * @param dir
   * @throws IOException
   */
  private void mkdir(String dir) throws IOException {
    FileUtils.createDirectory(dir);
  }

  /**
   * Removes a directory from file system
   * @param dir
   */
  private void rmdir(String dir) {
    FileUtils.deleteDir(new File(dir));
  }
}


And test code using this class looks like this:

public class XxxTest {

  private static InProcessCassandraServer cassandra;

  @BeforeClass
  public static void setup() throws TTransportException, IOException, InterruptedException {
    cassandra = new InProcessCassandraServer();
    cassandra.init();
    Thread t = new Thread(cassandra);
    t.setDaemon(true);
    t.start();
  }

  @AfterClass
  public static void shutdown() {
    cassandra.stop();
  }

  public void testX() {
    // connect to cassandra at localhost:9160
  }
}


note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12806375#action_12806375 ] 

Ran Tavory commented on CASSANDRA-740:
--------------------------------------

I've created a patch that implements this improvement, hope you like it ;)
Actually, I wanted to assign the issue to myself but couldn't figure out how (perhaps it's a permissions thing)

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>         Attachments: CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Ran Tavory updated CASSANDRA-740:
---------------------------------

    Attachment: CASSANDRA-740.patch

In this path there's a small improvement from Gary's comment to the previous patch.
Use the configuration file at $trunk/test/conf.

Basically I want the test to be simple, coherent and self contained so I set the storage-config property programmatically and I expect other developers that use the embedded server for testing to do so as well - it's just the easiest way to kick off a server and I think is appropriate for testing.

Looking forward to the next review, thanks!

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Gary Dusbabek (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12829076#action_12829076 ] 

Gary Dusbabek commented on CASSANDRA-740:
-----------------------------------------

Splitting it like that is fine.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Ran Tavory updated CASSANDRA-740:
---------------------------------

    Component/s:     (was: Contrib)
                 Core

Moved from contrib -> core

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Core
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Jonathan Ellis (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Jonathan Ellis updated CASSANDRA-740:
-------------------------------------


(and our unit tests run with ant's spawn-new-jvm-per-class option on)

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>             Fix For: 0.6
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Gary Dusbabek (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12830962#action_12830962 ] 

Gary Dusbabek commented on CASSANDRA-740:
-----------------------------------------

When EmbeddedCassandraServiceTest runs, it sets the conf dir to be $trunk/conf, which contains a config file that points to /var/cassandra, which may not be set in a development environment.  It would be better to point it to $trunk/test/conf.  Or, if the reason you're specifying -Dstorage-config programatically is to verify that it can be done and is adhered to, then you should create an alternate conf dir in $trunk/test that can be specified in place of "conf".

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12805033#action_12805033 ] 

Ran Tavory commented on CASSANDRA-740:
--------------------------------------

Hi Jonathan, so is there a recommended way to shut down cassandra in the test scenario I'm describing?

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>             Fix For: 0.6
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Gary Dusbabek (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12806500#action_12806500 ] 

Gary Dusbabek commented on CASSANDRA-740:
-----------------------------------------

Ran, the test is erroring for me:

test:
   [delete] Deleting directory /Users/gary.dusbabek/codes/cassandra-git/contrib/embedded/reports
    [mkdir] Created dir: /Users/gary.dusbabek/codes/cassandra-git/contrib/embedded/reports
    [junit] Running org.apache.cassandra.contrib.embedded.service.ExampleTest.java.ExampleTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Test org.apache.cassandra.contrib.embedded.service.ExampleTest.java.ExampleTest FAILED

Can you rebase and submit a new patch?

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>         Attachments: CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Jonathan Ellis (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12829078#action_12829078 ] 

Jonathan Ellis commented on CASSANDRA-740:
------------------------------------------

to clarify, I think (1) would be best under the main source tree, and (2) in contrib, but if it makes your life eaiser to have them both in contrib that is fine too.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Issue Comment Edited: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Jonathan Ellis (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12805160#action_12805160 ] 

Jonathan Ellis edited comment on CASSANDRA-740 at 1/26/10 7:55 PM:
-------------------------------------------------------------------

(and our unit tests run with junit's spawn-new-jvm-per-class option on)

      was (Author: jbellis):
    (and our unit tests run with ant's spawn-new-jvm-per-class option on)
  
> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>             Fix For: 0.6
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Ran Tavory updated CASSANDRA-740:
---------------------------------

    Attachment: CASSANDRA-740.patch

Updated patch after fixing build errors due to the main cassandra project build changes.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Jeff Hodges (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12828209#action_12828209 ] 

Jeff Hodges commented on CASSANDRA-740:
---------------------------------------

This isn't for testing Cassandra but for testing apps that depend on Cassandra.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Jonathan Ellis (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12805042#action_12805042 ] 

Jonathan Ellis commented on CASSANDRA-740:
------------------------------------------

No.  Cassandra's system tests start separate JVMs for the server.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>             Fix For: 0.6
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12831296#action_12831296 ] 

Ran Tavory commented on CASSANDRA-740:
--------------------------------------

Thanks for the review, Guy. 
I'll create a separate issue for the contrib part. I'll call it a "cleanup utility" and I'll link it to this issue. 
Any suggestions for a good location or name for the project? (I was thinking contrib/testing/utilities or contrib/utilities which may be a set of all sorts of utilities that aren't part of core).

What's the next step getting the patch submitted, then?

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Ran Tavory updated CASSANDRA-740:
---------------------------------

    Attachment: CASSANDRA-740.patch

In this patch I've only included the EmbeddedCassandraService and its test and they're all part of core.
I've decided to split the solution to two - first commit the embedded server which doesn't have unit-tests code specifics and, when accepted, submit another patch that helps testers with data cleanup and other wiring into contrib.
Looking forward to your review, thanks.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Hudson (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12834880#action_12834880 ] 

Hudson commented on CASSANDRA-740:
----------------------------------

Integrated in Cassandra #357 (See [http://hudson.zones.apache.org/hudson/job/Cassandra/357/])
    

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Core
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>             Fix For: 0.6
>
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12830476#action_12830476 ] 

Ran Tavory commented on CASSANDRA-740:
--------------------------------------

I've attached another patch per your comments above, thank you

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12828439#action_12828439 ] 

Ran Tavory commented on CASSANDRA-740:
--------------------------------------

Would you guys be happy if I separate the app into two classes as Jonathan suggests and put it under contrib/embedded?
(1) an embedded Cassandra server that does not use CassandraDaemon (and does not advertise a stop() method, since that doesn't work anyway as described above) 
(2) a utility for cleaning out log + data directories for use in testing 

As for (2) I'm not sure - should that be under contrib/embedded as well? It would certainly make my build plumbing easier if it does and It sort of makes sense from my use case to keep them under the same project scope. Also, to clean up the data files I'll need to run in the same host, which is a typical use case for the embedded scenario.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Gary Dusbabek (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12828121#action_12828121 ] 

Gary Dusbabek commented on CASSANDRA-740:
-----------------------------------------

Thanks for reformatting.  Everything worked this time.

I'm not sure this utility is going to be that useful.  The nostests (see test/system/__init__.py and test/system/test_server.py) were created for exactly this kind of testing.  I don't see the point of recreating the functionality in java.

If you can make a compelling argument, I'm listening.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Gary Dusbabek (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12831139#action_12831139 ] 

Gary Dusbabek commented on CASSANDRA-740:
-----------------------------------------

Thanks Ran, this is good.  Would you like the contrib portion to be considered as part of this ticket, or separtely?

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Reopened: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Ran Tavory reopened CASSANDRA-740:
----------------------------------


I'm working on it

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12805030#action_12805030 ] 

Ran Tavory commented on CASSANDRA-740:
--------------------------------------

Hi Gary, no, I have not found that all threads are shut down (not that they don't..).
So far I haven't run into the issue you're describing, so it's definitely possible that I'll run into it in the future, so I'll look out.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>             Fix For: 0.6
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Resolved: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Jonathan Ellis (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Jonathan Ellis resolved CASSANDRA-740.
--------------------------------------

       Resolution: Won't Fix
    Fix Version/s:     (was: 0.6)

Closing as wontfix for now.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Jonathan Ellis (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12805029#action_12805029 ] 

Jonathan Ellis commented on CASSANDRA-740:
------------------------------------------

right.  it's not designed to do that and the complexity required to do so is not desirable.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>             Fix For: 0.6
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12831439#action_12831439 ] 

Ran Tavory commented on CASSANDRA-740:
--------------------------------------

Related work: https://issues.apache.org/jira/browse/CASSANDRA-782

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Core
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Gary Dusbabek (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12831430#action_12831430 ] 

Gary Dusbabek commented on CASSANDRA-740:
-----------------------------------------

I would have committed yesterday, but wanted to verify that the other piece would be included separately.  

contrib is very much disorganized atm, but either of the locations you suggest would be appropriate.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Jonathan Ellis (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12828253#action_12828253 ] 

Jonathan Ellis commented on CASSANDRA-740:
------------------------------------------

This feels like 2 separate things to me.

(1) an embedded Cassandra server that does not use CassandraDaemon (and does not advertise a stop() method, since that doesn't work anyway as described above)
(2) a utility for cleaning out log + data directories for use in testing


> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Ran Tavory updated CASSANDRA-740:
---------------------------------

    Attachment: CASSANDRA-740.patch

Attaching the proposed patch to implement this

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>         Attachments: CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Gary Dusbabek (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12805010#action_12805010 ] 

Gary Dusbabek commented on CASSANDRA-740:
-----------------------------------------

Have you found that all threads associated with spinning up cassandra are stopped when CassandraDaemon.stop() is called?  I was working on something a few months ago and discovered that there were still large parts of cassandra that were not reentrant, meaning that it was nearly impossible to start a cassanrdra process, shut it down, and then start it up again all within the same jvm instance.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>             Fix For: 0.6
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Assigned: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Jonathan Ellis (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Jonathan Ellis reassigned CASSANDRA-740:
----------------------------------------

    Assignee: Ran Tavory

You needed to be added as a Contributor, which I have done.

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>         Attachments: CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Gary Dusbabek (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Gary Dusbabek updated CASSANDRA-740:
------------------------------------

    Priority: Minor  (was: Major)

> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests

Posted by "Ran Tavory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CASSANDRA-740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12828248#action_12828248 ] 

Ran Tavory commented on CASSANDRA-740:
--------------------------------------

An embedded cassandra server is useful for testing apps that use cassandra, not cassandra itself.
I've actually developed this for my own use and then posted it on the mailing list so I was asked to make that available publicly. I think contrib is a suitable place for it (and I consulted Jonathan about this).
By having an embedded cassandra server in your unit tests it's very easy to start a clean cassandra instance, run your tests independently of other developers and clean up after they finish. I use it on my day to day job and find it very useful.


> Create InProcessCassandraServer for unit tests
> ----------------------------------------------
>
>                 Key: CASSANDRA-740
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-740
>             Project: Cassandra
>          Issue Type: New Feature
>          Components: Contrib
>            Reporter: Ran Tavory
>            Assignee: Ran Tavory
>            Priority: Minor
>         Attachments: CASSANDRA-740.patch, CASSANDRA-740.patch
>
>
> I've been personally using an in-process cassandra server and found it useful so I was ask to make it available publicly, here goes.
> When unit-testing with cassandra I create an in process cassandra instance. That's nice since it lets you isolate tests, and you don't have to worry about a server being available for your unit tests.
> The code goes more or less like this (I'll attach a patch when the work is done after cleanup etc)
> /**
>  * An in-memory cassandra storage service that listens to the thrift interface.
>  * Useful for unit testing,
>  *
>  * @author Ran Tavory (rantav@gmail.com)
>  *
>  */
> public class InProcessCassandraServer implements Runnable {
>   private static final Logger log = LoggerFactory.getLogger(InProcessCassandraServer.class);
>   CassandraDaemon cassandraDaemon;
>   public void init() {
>     try {
>       prepare();
>     } catch (IOException e) {
>       log.error("Cannot prepare cassandra.", e);
>     }
>     try {
>       cassandraDaemon = new CassandraDaemon();
>       cassandraDaemon.init(null);
>     } catch (TTransportException e) {
>       log.error("TTransportException", e);
>     } catch (IOException e) {
>       log.error("IOException", e);
>     }
>   }
>   @Override
>   public void run() {
>     cassandraDaemon.start();
>   }
>   public void stop() {
>     cassandraDaemon.stop();
>     rmdir("tmp");
>   }
>   /**
>    * Creates all files and directories needed
>    * @throws IOException
>    */
>   private void prepare() throws IOException {
>     // delete tmp dir first
>     rmdir("tmp");
>     // make a tmp dir and copy storag-conf.xml and log4j.properties to it
>     copy("/cassandra/storage-conf.xml", "tmp");
>     copy("/cassandra/log4j.properties", "tmp");
>     System.setProperty("storage-config", "tmp");
>     // make cassandra directories.
>     for (String s: DatabaseDescriptor.getAllDataFileLocations()) {
>       mkdir(s);
>     }
>     mkdir(DatabaseDescriptor.getBootstrapFileLocation());
>     mkdir(DatabaseDescriptor.getLogFileLocation());
>   }
>   /**
>    * Copies a resource from within the jar to a directory.
>    *
>    * @param resourceName
>    * @param directory
>    * @throws IOException
>    */
>   private void copy(String resource, String directory) throws IOException {
>     mkdir(directory);
>     InputStream is = getClass().getResourceAsStream(resource);
>     String fileName = resource.substring(resource.lastIndexOf("/") + 1);
>     File file = new File(directory + System.getProperty("file.separator") + fileName);
>     OutputStream out = new FileOutputStream(file);
>     byte buf[] = new byte[1024];
>     int len;
>     while ((len = is.read(buf)) > 0) {
>       out.write(buf, 0, len);
>     }
>     out.close();
>     is.close();
>   }
>   /**
>    * Creates a directory
>    * @param dir
>    * @throws IOException
>    */
>   private void mkdir(String dir) throws IOException {
>     FileUtils.createDirectory(dir);
>   }
>   /**
>    * Removes a directory from file system
>    * @param dir
>    */
>   private void rmdir(String dir) {
>     FileUtils.deleteDir(new File(dir));
>   }
> }
> And test code using this class looks like this:
> public class XxxTest {
>   private static InProcessCassandraServer cassandra;
>   @BeforeClass
>   public static void setup() throws TTransportException, IOException, InterruptedException {
>     cassandra = new InProcessCassandraServer();
>     cassandra.init();
>     Thread t = new Thread(cassandra);
>     t.setDaemon(true);
>     t.start();
>   }
>   @AfterClass
>   public static void shutdown() {
>     cassandra.stop();
>   }
>   public void testX() {
>     // connect to cassandra at localhost:9160
>   }
> }
> note: I've set Fix Version to 6.0, hope it's correct

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.