我正在尝试为scala中的orientDB客户端编写一些单元测试实用程序。
以下内容旨在对DB进行操作,并且应该使用代码包装该函数,以便为单个单元测试创建和销毁数据库。
但是,没有看到关于如何清理内存数据库的很好的文档(并且查看许多开源项目,人们似乎只是泄漏数据库并在新端口上创建新数据库)。 / p>
只需调用db.close就会使数据库监听端口,后续测试失败。调用db.drop似乎可行,但前提是func成功将数据添加到数据库。
那么,finally子句中需要进行哪些清理?
@Test
def fTest2(): Unit = {
def withJSONDBLoan(func: ODatabaseDocumentTx => Unit) : Unit = {
val db: ODatabaseDocumentTx = new ODatabaseDocumentTx("memory:jsondb")
db.create()
try {
func(db)
} finally {
if (!db.isClosed){
db.close // Nope. DB is leaked.
}
// db.drop seems to close the DB but can't
// see when to safely call this.
}
}
val query1 = "insert into ouser set name='test',password='test', status='ACTIVE'"
withJSONDBLoan { db =>
db.command(new OCommandSQL(query1)).execute[ODocument]()
}
// Fails at create because DB already exists.
val query2 = "insert into ouser set name='test2',password='test2', status='ACTIVE'"
withJSONDBLoan { db =>
db.command(new OCommandSQL(query2)).execute[ODocument]()
}
}