Return-Path: Delivered-To: apmail-incubator-cassandra-commits-archive@minotaur.apache.org Received: (qmail 31812 invoked from network); 3 Feb 2010 13:59:49 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 3 Feb 2010 13:59:49 -0000 Received: (qmail 7423 invoked by uid 500); 3 Feb 2010 13:59:49 -0000 Delivered-To: apmail-incubator-cassandra-commits-archive@incubator.apache.org Received: (qmail 7397 invoked by uid 500); 3 Feb 2010 13:59:49 -0000 Mailing-List: contact cassandra-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: cassandra-dev@incubator.apache.org Delivered-To: mailing list cassandra-commits@incubator.apache.org Received: (qmail 7387 invoked by uid 99); 3 Feb 2010 13:59:49 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Feb 2010 13:59:49 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Feb 2010 13:59:48 +0000 Received: from brutus.apache.org (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id 00A53234C4A8 for ; Wed, 3 Feb 2010 05:59:28 -0800 (PST) Message-ID: <426519218.2731265205568001.JavaMail.jira@brutus.apache.org> Date: Wed, 3 Feb 2010 13:59:28 +0000 (UTC) From: "Jonathan Ellis (JIRA)" To: cassandra-commits@incubator.apache.org Subject: [jira] Commented: (CASSANDRA-740) Create InProcessCassandraServer for unit tests In-Reply-To: <954290021.32051264489474856.JavaMail.jira@brutus.apache.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ 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.