test_weakxaction.c 2.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* Find out about weak transactions.
 *  User A does a transaction.
 *  User B does somethign without a transaction, and it conflicts.
 */

#include <assert.h>
#include <db.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
11
#include "test.h"
12

Vincenzo Liberatore's avatar
Vincenzo Liberatore committed
13
void test_autotxn(u_int32_t env_flags, u_int32_t db_flags) {
14 15 16 17 18
    DB_ENV *env;
    DB *db;
    int r;
    system("rm -rf " DIR);
    mkdir(DIR, 0777);
Vincenzo Liberatore's avatar
Vincenzo Liberatore committed
19 20 21 22 23
    r = db_env_create (&env, 0);           CKERR(r);
    r = env->set_flags(env, env_flags, 1); CKERR(r);
    r = env->open(env, DIR, 
                  DB_CREATE | DB_PRIVATE | DB_INIT_MPOOL | 
                  DB_INIT_LOG | DB_INIT_TXN | DB_INIT_LOCK, 0777); CKERR(r);
24
    r = db_create(&db, env, 0);
25
    CKERR(r);
26 27
    db->set_errfile(db, stderr);
    {
Vincenzo Liberatore's avatar
Vincenzo Liberatore committed
28 29 30 31 32 33 34 35 36
	DB_TXN *x = NULL;
        #ifdef USE_BDB
	    r = env->txn_begin(env, 0, &x, 0); assert(r==0);
        #endif
	r = db->open(db, x, "numbers.db", 0, DB_BTREE, DB_CREATE | db_flags, 0);
	CKERR(r);
        #ifdef USE_BDB
	    r = x->commit(x, 0); assert(r==0);
        #endif
37
    }
38

Vincenzo Liberatore's avatar
Vincenzo Liberatore committed
39 40 41 42 43
    DB_TXN *x1, *x2 = NULL;
    r = env->txn_begin(env, 0, &x1, DB_TXN_NOWAIT); CKERR(r);
    #ifdef USE_BDB
        r = env->txn_begin(env, 0, &x2, DB_TXN_NOWAIT); CKERR(r);
    #endif
44 45 46 47 48 49 50 51 52
    DBT k1,k2,v1,v2;
    memset(&k1, 0, sizeof(DBT));
    memset(&k2, 0, sizeof(DBT));
    memset(&v1, 0, sizeof(DBT));
    memset(&v2, 0, sizeof(DBT));
    k2.data = k1.data = "hello";
    k2.size = k1.size = 6;
    v1.data = "there";
    v1.size = 6;
53
    r = db->put(db, x1, &k1, &v1, 0); CKERR(r);
54
    r = db->get(db, x2, &k2, &v2, 0); assert(r==DB_LOCK_DEADLOCK || r==DB_LOCK_NOTGRANTED);
Vincenzo Liberatore's avatar
Vincenzo Liberatore committed
55 56 57 58 59
    r = x1->commit(x1, 0);         CKERR(r);
    #ifdef USE_BDB
        r = x2->commit(x2, 0);     assert(r==0);
    #endif
    r = db->close(db, 0);          CKERR(r);
60
    r = env->close(env, 0);        assert(r==0);
Vincenzo Liberatore's avatar
Vincenzo Liberatore committed
61 62 63 64 65 66 67
}

int main (int argc, char *argv[])  {
    test_autotxn(DB_AUTO_COMMIT, DB_AUTO_COMMIT); 
    test_autotxn(0,              DB_AUTO_COMMIT); 
    test_autotxn(DB_AUTO_COMMIT, 0); 
    test_autotxn(0,              0); 
68 69
    return 0;
}