#include "lsmtest.h" typedef struct OomTest OomTest; struct OomTest { lsm_env *pEnv; int iNext; /* Next value to pass to testMallocOom() */ int nFail; /* Number of OOM events injected */ int bEnable; int rc; /* Test case error code */ }; static void testOomStart(OomTest *p){ memset(p, 0, sizeof(OomTest)); p->iNext = 1; p->bEnable = 1; p->nFail = 1; p->pEnv = tdb_lsm_env(); } static void xOomHook(OomTest *p){ p->nFail++; } static int testOomContinue(OomTest *p){ if( p->rc!=0 || (p->iNext>1 && p->nFail==0) ){ return 0; } p->nFail = 0; testMallocOom(p->pEnv, p->iNext, 0, (void (*)(void*))xOomHook, (void *)p); return 1; } static void testOomEnable(OomTest *p, int bEnable){ p->bEnable = bEnable; testMallocOomEnable(p->pEnv, bEnable); } static void testOomNext(OomTest *p){ p->iNext++; } static int testOomHit(OomTest *p){ return (p->nFail>0); } static int testOomFinish(OomTest *p){ return p->rc; } static void testOomAssert(OomTest *p, int bVal){ if( bVal==0 ){ test_failed(); p->rc = 1; } } /* ** Test that the error code matches the state of the OomTest object passed ** as the first argument. Specifically, check that rc is LSM_NOMEM if an ** OOM error has already been injected, or LSM_OK if not. */ static void testOomAssertRc(OomTest *p, int rc){ testOomAssert(p, rc==LSM_OK || rc==LSM_NOMEM); testOomAssert(p, testOomHit(p)==(rc==LSM_NOMEM) || p->bEnable==0 ); } static void testOomOpen( OomTest *pOom, const char *zName, lsm_db **ppDb, int *pRc ){ if( *pRc==LSM_OK ){ int rc; rc = lsm_new(tdb_lsm_env(), ppDb); if( rc==LSM_OK ) rc = lsm_open(*ppDb, zName); testOomAssertRc(pOom, rc); *pRc = rc; } } static void testOomFetch( OomTest *pOom, lsm_db *pDb, void *pKey, int nKey, void *pVal, int nVal, int *pRc ){ testOomAssertRc(pOom, *pRc); if( *pRc==LSM_OK ){ lsm_cursor *pCsr; int rc; rc = lsm_csr_open(pDb, &pCsr); if( rc==LSM_OK ) rc = lsm_csr_seek(pCsr, pKey, nKey, 0); testOomAssertRc(pOom, rc); if( rc==LSM_OK ){ const void *p; int n; testOomAssert(pOom, lsm_csr_valid(pCsr)); rc = lsm_csr_key(pCsr, &p, &n); testOomAssertRc(pOom, rc); testOomAssert(pOom, rc!=LSM_OK || (n==nKey && memcmp(pKey, p, nKey)==0) ); } if( rc==LSM_OK ){ const void *p; int n; testOomAssert(pOom, lsm_csr_valid(pCsr)); rc = lsm_csr_value(pCsr, &p, &n); testOomAssertRc(pOom, rc); testOomAssert(pOom, rc!=LSM_OK || (n==nVal && memcmp(pVal, p, nVal)==0) ); } lsm_csr_close(pCsr); *pRc = rc; } } static void testOomWrite( OomTest *pOom, lsm_db *pDb, void *pKey, int nKey, void *pVal, int nVal, int *pRc ){ testOomAssertRc(pOom, *pRc); if( *pRc==LSM_OK ){ int rc; rc = lsm_insert(pDb, pKey, nKey, pVal, nVal); testOomAssertRc(pOom, rc); *pRc = rc; } } static void testOomFetchStr( OomTest *pOom, lsm_db *pDb, const char *zKey, const char *zVal, int *pRc ){ int nKey = strlen(zKey); int nVal = strlen(zVal); testOomFetch(pOom, pDb, (void *)zKey, nKey, (void *)zVal, nVal, pRc); } static void testOomFetchData( OomTest *pOom, lsm_db *pDb, Datasource *pData, int iKey, int *pRc ){ void *pKey; int nKey; void *pVal; int nVal; testDatasourceEntry(pData, iKey, &pKey, &nKey, &pVal, &nVal); testOomFetch(pOom, pDb, pKey, nKey, pVal, nVal, pRc); } static void testOomWriteStr( OomTest *pOom, lsm_db *pDb, const char *zKey, const char *zVal, int *pRc ){ int nKey = strlen(zKey); int nVal = strlen(zVal); testOomWrite(pOom, pDb, (void *)zKey, nKey, (void *)zVal, nVal, pRc); } static void testOomWriteData( OomTest *pOom, lsm_db *pDb, Datasource *pData, int iKey, int *pRc ){ void *pKey; int nKey; void *pVal; int nVal; testDatasourceEntry(pData, iKey, &pKey, &nKey, &pVal, &nVal); testOomWrite(pOom, pDb, pKey, nKey, pVal, nVal, pRc); } static void testOomScan( OomTest *pOom, lsm_db *pDb, int bReverse, const void *pKey, int nKey, int nScan, int *pRc ){ if( *pRc==0 ){ int rc; int iScan = 0; lsm_cursor *pCsr; int (*xAdvance)(lsm_cursor *) = 0; rc = lsm_csr_open(pDb, &pCsr); testOomAssertRc(pOom, rc); if( rc==LSM_OK ){ if( bReverse ){ rc = lsm_csr_seek(pCsr, pKey, nKey, LSM_SEEK_LE); xAdvance = lsm_csr_prev; }else{ rc = lsm_csr_seek(pCsr, pKey, nKey, LSM_SEEK_GE); xAdvance = lsm_csr_next; } } testOomAssertRc(pOom, rc); while( rc==LSM_OK && lsm_csr_valid(pCsr) && iScan "one" ** "two" -> "four" ** "three" -> "nine" ** "four" -> "sixteen" ** "five" -> "twentyfive" ** "six" -> "thirtysix" ** "seven" -> "fourtynine" ** "eight" -> "sixtyfour" */ static void setup_populate_db(void){ const char *azStr[] = { "one", "one", "two", "four", "three", "nine", "four", "sixteen", "five", "twentyfive", "six", "thirtysix", "seven", "fourtynine", "eight", "sixtyfour", }; int rc; int ii; lsm_db *pDb; testDeleteLsmdb(LSMTEST6_TESTDB); rc = lsm_new(tdb_lsm_env(), &pDb); if( rc==LSM_OK ) rc = lsm_open(pDb, LSMTEST6_TESTDB); for(ii=0; rc==LSM_OK && ii