我遇到此错误:
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 2062): Could not open database
#################################################################
Error Code : 2062 (SQLITE_CANTOPEN_EMFILE)
Caused By : Application has opened too many files. Maximum of available file descriptors in one process is 1024 in default.
(unknown error (code 2062): Could not open database)
#################################################################
我认为发生这种情况是因为我忘记了在方法中关闭游标。为节省起见,我想在单元测试中重现此问题。但是我不知道如何检查现有的连接/光标数量以及如何断言该异常
public int getServicesAmount() {
int servicesAmount;
AppDbHelper dbHelper = new AppDbHelper(context);
SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] columns = {/* some columns */};
Cursor cursor = db.query(
AppEntryContract.ServiceEntry.TABLE,
columns,
null,
null,
null,
null,
null
);
servicesAmount = cursor.getCount();
// here I should close the cursor
return servicesAmount;
}
这是到目前为止的单元测试
@Test
public void connectionNotClosed() throws Exception {
try
{
Storage storage = new Storage(RuntimeEnvironment.application);
for(int i = 0; i <= 1025; i++) {
storage.getServicesAmount();
}
Assert.fail();
}
catch(ArithmeticException e)
{
System.out.println("---------------------------------------------");
System.out.println("----------------- SUCCESS -------------------");
System.out.println("---------------------------------------------");
assertThat("",is(""));
}
}