MojErr MojDbQuotaTest::testErrors() { #ifdef MOJ_USE_BDB MojRefCountedPtr<MojDbStorageEngine> engine(new MojDbBerkeleyEngine()); #elif MOJ_USE_LDB MojRefCountedPtr<MojDbStorageEngine> engine(new MojDbLevelEngine()); #else MojRefCountedPtr<MojDbStorageEngine> engine; #endif MojAllocCheck(engine.get()); MojRefCountedPtr<MojDbTestStorageEngine> testEngine(new MojDbTestStorageEngine(engine.get())); MojAllocCheck(testEngine.get()); MojErr err = testEngine->open(MojDbTestDir); MojTestErrCheck(err); MojDb db; err = db.open(MojDbTestDir, testEngine.get()); MojTestErrCheck(err); // test that failed put does not affect quota MojInt64 quotaUsage1 = 0; err = getQuotaUsage(db, _T("com.foo.*"), quotaUsage1); MojTestErrCheck(err); err = testEngine->setNextError(_T("txn.commit"), MojErrDbDeadlock); MojTestErrCheck(err); err = put(db, MojTestKind3Objects[1]); MojTestErrExpected(err, MojErrDbDeadlock); MojInt64 quotaUsage2 = 0; err = getQuotaUsage(db, _T("com.foo.*"), quotaUsage2); MojTestErrCheck(err); MojTestAssert(quotaUsage2 == quotaUsage1); // test that failed putQuota has no effect err = testEngine->setNextError(_T("txn.commit"), MojErrDbDeadlock); MojTestErrCheck(err); MojObject obj; err = obj.fromJson(_T("{\"owner\":\"com.foo.boo\",\"size\":1000}")); MojErrCheck(err); err = db.putQuotas(&obj, &obj + 1); MojTestErrExpected(err, MojErrDbDeadlock); MojInt64 quotaUsage3 = 0; err = getQuotaUsage(db, _T("com.foo.*"), quotaUsage3); MojTestErrCheck(err); MojTestAssert(quotaUsage3 == quotaUsage1); // test that failed putKind has no effect err = testEngine->setNextError(_T("txn.commit"), MojErrDbDeadlock); MojTestErrCheck(err); err = obj.fromJson(MojTestKind3Str2); MojTestErrCheck(err); err = db.putKind(obj); MojTestErrExpected(err, MojErrDbDeadlock); MojInt64 quotaUsage4 = 0; err = getQuotaUsage(db, _T("com.foo.*"), quotaUsage4); MojTestErrCheck(err); MojTestAssert(quotaUsage4 == quotaUsage1); err = db.close(); MojTestErrCheck(err); return MojErrNone; }
MojErr MojDbQuotaTest::testEnforce(MojDb& db) { // set quota size to current usage MojInt64 quotaUsage1 = 0; MojErr err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage1); MojTestErrCheck(err); MojObject obj; err = obj.putString(_T("owner"), _T("com.foo.bar")); MojErrCheck(err); err = obj.putInt(_T("size"), quotaUsage1); MojErrCheck(err); err = db.putQuotas(&obj, &obj + 1); MojErrCheck(err); err = put(db, MojTestKind1Objects[3]); MojTestErrExpected(err, MojErrDbQuotaExceeded); // Try to delete the kind MojString kindStr; err = kindStr.assign(_T("Test:1")); MojTestErrCheck(err); bool found = false; err = db.delKind(kindStr, found); //The delete should be failure, because it contain sub kind "Test2:1" MojTestErrExpected(err,MojErrDbKindHasSubKinds); MojTestAssert(!found); return MojErrNone; }
MojErr MojDbQuotaTest::testEnforce(MojDb& db) { // set quota size to current usage MojInt64 quotaUsage1 = 0; MojErr err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage1); MojTestErrCheck(err); MojObject obj; err = obj.putString(_T("owner"), _T("com.foo.bar")); MojErrCheck(err); err = obj.putInt(_T("size"), quotaUsage1); MojErrCheck(err); err = db.putQuotas(&obj, &obj + 1); MojErrCheck(err); err = put(db, MojTestKind1Objects[3]); MojTestErrExpected(err, MojErrDbQuotaExceeded); // make sure we can delete the kind MojString kindStr; err = kindStr.assign(_T("Test:1")); MojTestErrCheck(err); bool found = false; err = db.delKind(kindStr, found); MojTestErrCheck(err); MojTestAssert(found); return MojErrNone; }
MojErr MojDbQuotaTest::testMultipleQuotas(MojDb& db) { // quota for com.foo.baz MojObject obj; MojErr err = obj.fromJson(_T("{\"owner\":\"com.foo.baz\",\"size\":1000}")); MojErrCheck(err); err = db.putQuotas(&obj, &obj + 1); MojErrCheck(err); // register kinds err = obj.fromJson(MojTestKind1Str1); MojTestErrCheck(err); err = db.putKind(obj); MojTestErrCheck(err); err = obj.fromJson(MojTestKind2Str1); MojTestErrCheck(err); err = db.putKind(obj); MojTestErrCheck(err); // put object of kind1 and kind2 err = put(db, MojTestKind1Objects[0]); MojTestErrCheck(err); err = put(db, MojTestKind2Objects[0]); MojTestErrCheck(err); MojInt64 quotaUsage1 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage1); MojTestErrCheck(err); MojInt64 quotaUsage2 = 0; err = getQuotaUsage(db, _T("com.foo.baz"), quotaUsage2); MojTestErrCheck(err); MojTestAssert(quotaUsage1 > 0); MojTestAssert(quotaUsage2 == 0); // change owner of kind2 to com.foo.baz err = obj.fromJson(MojTestKind2Str2); MojTestErrCheck(err); err = db.putKind(obj); MojTestErrCheck(err); MojInt64 quotaUsage3 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage3); MojTestErrCheck(err); MojInt64 quotaUsage4 = 0; err = getQuotaUsage(db, _T("com.foo.baz"), quotaUsage4); MojTestErrCheck(err); MojTestAssert(quotaUsage3 > 0); MojTestAssert(quotaUsage3 == quotaUsage4); // make kind2 inherit from kind1 err = obj.fromJson(MojTestKind2Str3); MojTestErrCheck(err); err = db.putKind(obj); MojTestErrCheck(err); MojInt64 quotaUsage5 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage5); MojTestErrCheck(err); MojInt64 quotaUsage6 = 0; err = getQuotaUsage(db, _T("com.foo.baz"), quotaUsage6); MojTestErrCheck(err); MojTestAssert(quotaUsage5 > quotaUsage1); MojTestAssert(quotaUsage6 == 0); // kind3 and object err = obj.fromJson(MojTestKind3Str1); MojTestErrCheck(err); err = db.putKind(obj); MojTestErrCheck(err); err = put(db, MojTestKind3Objects[0]); MojTestErrCheck(err); MojInt64 quotaUsage7 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage7); MojTestErrCheck(err); MojTestAssert(quotaUsage7 == quotaUsage5); // wildcard err = obj.fromJson(_T("{\"owner\":\"com.foo.*\",\"size\":1000}")); MojErrCheck(err); err = db.putQuotas(&obj, &obj + 1); MojErrCheck(err); MojInt64 quotaUsage8 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage8); MojTestErrCheck(err); MojInt64 quotaUsage9 = 0; err = getQuotaUsage(db, _T("com.foo.*"), quotaUsage9); MojTestErrCheck(err); MojTestAssert(quotaUsage8 == quotaUsage5); MojTestAssert(quotaUsage9 > 0); return MojErrNone; }
MojErr MojDbQuotaTest::testUsage(MojDb& db) { // put quota MojObject obj; MojErr err = obj.fromJson(_T("{\"owner\":\"com.foo.bar\",\"size\":1000}")); MojErrCheck(err); err = db.putQuotas(&obj, &obj + 1); MojErrCheck(err); // empty MojInt64 kindUsage = 0; err = getKindUsage(db, _T("Test:1"), kindUsage); MojTestErrCheck(err); MojTestAssert(kindUsage == 0); MojInt64 quotaUsage = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage); MojTestErrCheck(err); MojTestAssert(quotaUsage == 0); // new obj err = put(db, MojTestKind1Objects[0]); MojTestErrCheck(err); MojInt64 kindUsage1 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage1); MojTestErrCheck(err); MojTestAssert(kindUsage1 > 0); MojInt64 quotaUsage1 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage1); MojTestErrCheck(err); MojTestAssert(quotaUsage1 > 0); // add prop to existing obj err = obj.fromJson(MojTestKind1Objects[0]); MojTestErrCheck(err); err = obj.put(_T("bar"), 2); MojTestErrCheck(err); err = db.put(obj, MojDb::FlagForce); MojTestErrCheck(err); MojInt64 kindUsage2 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage2); MojTestErrCheck(err); MojTestAssert(kindUsage2 > kindUsage1); MojInt64 quotaUsage2 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage2); MojTestErrCheck(err); MojTestAssert(quotaUsage2 > quotaUsage1); // add 2nd obj err = put(db, MojTestKind1Objects[1]); MojTestErrCheck(err); MojInt64 kindUsage3 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage3); MojTestErrCheck(err); MojTestAssert(kindUsage3 > kindUsage2); MojInt64 quotaUsage3 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage3); MojTestErrCheck(err); MojTestAssert(quotaUsage3 > quotaUsage2); // del first obj bool found = false; err = db.del(1, found, MojDb::FlagPurge); MojTestErrCheck(err); MojTestAssert(found); MojInt64 kindUsage4 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage4); MojTestErrCheck(err); MojTestAssert(kindUsage4 == kindUsage3 - kindUsage2); MojInt64 quotaUsage4 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage4); MojTestErrCheck(err); MojTestAssert(quotaUsage4 == quotaUsage3 - quotaUsage2); // add index err = obj.fromJson(MojTestKind1Str2); MojTestErrCheck(err); err = db.putKind(obj); MojTestErrCheck(err); MojInt64 kindUsage5 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage5); MojTestErrCheck(err); MojTestAssert(kindUsage5 > kindUsage4); MojInt64 quotaUsage5 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage5); MojTestErrCheck(err); MojTestAssert(quotaUsage5 > quotaUsage4); // update locale err = db.updateLocale(_T("FR_fr")); MojTestErrCheck(err); err = db.updateLocale(_T("EN_us")); MojTestErrCheck(err); MojInt64 kindUsage6 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage6); MojTestErrCheck(err); MojTestAssert(kindUsage6 == kindUsage5); MojInt64 quotaUsage6 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage6); MojTestErrCheck(err); MojTestAssert(quotaUsage6 == quotaUsage5); // drop index err = obj.fromJson(MojTestKind1Str1); MojTestErrCheck(err); err = db.putKind(obj); MojTestErrCheck(err); MojInt64 kindUsage7 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage7); MojTestErrCheck(err); MojTestAssert(kindUsage7 == kindUsage4); MojInt64 quotaUsage7 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage7); MojTestErrCheck(err); MojTestAssert(quotaUsage7 == quotaUsage4); // drop kind MojString kindStr; err = kindStr.assign(_T("Test:1")); MojTestErrCheck(err); err = db.delKind(kindStr, found); MojTestErrCheck(err); MojTestAssert(found); MojInt64 kindUsage8 = 0; err = getKindUsage(db, _T("Test:1"), kindUsage8); MojTestErrCheck(err); MojTestAssert(kindUsage8 == 0); MojInt64 quotaUsage8 = 0; err = getQuotaUsage(db, _T("com.foo.bar"), quotaUsage8); MojTestErrCheck(err); MojTestAssert(quotaUsage8 == 0); err = db.quotaStats(obj); MojErrCheck(err); return MojErrNone; }