cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ran Tavory (JIRA)" <j...@apache.org>
Subject [jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests
Date Tue, 02 Feb 2010 04:30:21 GMT

    [ 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.


Mime
View raw message