1
[csql.git] / test / dbapi / Connection / isotest29.c
blobc2b7a7c8fdf8c822604dc1b55289389d33a05925
1 #include "common.h"
2 //READ_REPEATABLE isolation testing
3 // T1 T2
4 // -------------------------
5 // Read
6 // Delete
7 // Read
8 // T1 second read should fail saying "tuple not found"
9 int selectDone=0, deleteDone=0;
10 void* runTest1(void *p);
11 void* runTest2(void *p);
12 int *p1RetVal = NULL;
13 int *p2RetVal = NULL;
14 int main()
16 Connection conn;
17 DbRetVal rv = conn.open("root", "manager");
18 if (rv != OK) { printf("Error during connection %d\n", rv); return 1; }
19 DatabaseManager *dbMgr = conn.getDatabaseManager();
20 int ret = createTable(dbMgr);
21 if (ret != 0) { return 1; }
23 rv = conn.startTransaction();
24 if (rv != OK) {printf ("Unable to start trans\n"); return 1; }
25 rv = insert(dbMgr, 100, false);
26 if (rv != OK) { printf("Unable to insert\n"); return 2; }
27 conn.commit();
28 printf("Tuple inserted\n");
30 pthread_t thr[2];
31 int *status1, *status2;
32 pthread_create (&thr[0], NULL, &runTest1, NULL);
33 pthread_create (&thr[1], NULL, &runTest2, NULL);
34 printf("All threads started\n");
36 pthread_join(thr[0], (void**)&status1);
37 pthread_join(thr[1], (void**)&status2);
38 ret = 0;
39 if (*status1 != 0 || *status2 != 0) ret = 1;
40 dbMgr->dropTable("t1");
41 conn.close();
42 if (p1RetVal) { delete p1RetVal; p1RetVal = NULL; }
43 if (p2RetVal) { delete p2RetVal; p2RetVal = NULL; }
44 return ret;
46 void* runTest1(void *message)
48 Connection conn;
49 DbRetVal rv = conn.open("root", "manager");
50 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
51 DatabaseManager *dbMgr = conn.getDatabaseManager();
52 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
53 rv = conn.startTransaction(READ_REPEATABLE);
54 if (rv != OK) return NULL;
55 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
56 p1RetVal = new int();
57 *p1RetVal = 0;
58 rv = select(dbMgr, 100, true);
59 if (rv != OK) { printf("Test Failed:first thread failed to select\n"); *p1RetVal = 1; }
60 selectDone = 1;
61 while (deleteDone !=1) ::sleep(1);
62 rv = select(dbMgr, 100, true);
63 if (rv != OK) { printf("Test Failed:first thread read failed \n"); *p1RetVal = 1; }
64 conn.commit();
65 rv = conn.close();
66 pthread_exit(p1RetVal);
68 void* runTest2(void *message)
70 Connection conn;
71 DbRetVal rv = conn.open("root", "manager");
72 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
73 DatabaseManager *dbMgr = conn.getDatabaseManager();
74 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
75 rv = conn.startTransaction(READ_REPEATABLE);
76 if (rv != OK) return NULL;
77 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
78 while (selectDone !=1) ::sleep(1);
79 p2RetVal = new int();
80 *p2RetVal = 0;
81 rv = remove(dbMgr, 100, true);
82 if (rv != OK) { printf("Test Passed: second thread did not delete\n"); *p2RetVal = 0; }
83 if (rv == OK) { printf("Test Failed:second thread deleted\n"); *p2RetVal = 1; }
84 conn.commit();
85 deleteDone =1;
86 conn.close();
87 pthread_exit(p2RetVal);