fixing timing issue in test script
[csql.git] / test / dbapi / Connection / isotest10.c
blob3d5296a4cdd9a30489921640708e8a9f5e7cb2b4
1 #include "common.h"
2 //READ_COMMITTED isolation testing
3 // T1 T2
4 // -------------------------
5 // Read
6 // Update
7 // Read
8 // T1 should read updated tuple by T2
9 void* runTest1(void *p);
10 void* runTest2(void *p);
11 int *p1RetVal = NULL;
12 int *p2RetVal = NULL;
13 int selectDone =0, updateDone=0, allDone=0;
14 int main()
17 Connection conn;
18 DbRetVal rv = conn.open("root", "manager");
19 if (rv != OK) { printf("Error during connection %d\n", rv); return 1; }
20 DatabaseManager *dbMgr = conn.getDatabaseManager();
21 int ret = createTable(dbMgr);
22 if (ret != 0) { return 1; }
24 rv = conn.startTransaction();
25 if (rv != OK) {printf ("Unable to start trans\n"); return 1; }
26 rv = insert(dbMgr, 100, false);
27 if (rv != OK) { printf("Unable to insert\n"); return 2; }
28 conn.commit();
29 printf("Tuple inserted\n");
31 pthread_t thr[2];
32 int *status1, *status2;
33 pthread_create (&thr[0], NULL, &runTest1, NULL);
34 pthread_create (&thr[1], NULL, &runTest2, NULL);
35 printf("All threads started\n");
37 pthread_join(thr[0], (void**)&status1);
38 pthread_join(thr[1], (void**)&status2);
39 ret = 0;
40 if (*status1 != 0 || *status2 != 0) ret = 1;
41 dbMgr->dropTable("t1");
42 conn.close();
43 if (p1RetVal) { delete p1RetVal; p1RetVal = NULL; }
44 if (p2RetVal) { delete p2RetVal; p2RetVal = NULL; }
45 return ret;
47 void* runTest1(void *message)
49 Connection conn;
50 DbRetVal rv = conn.open("root", "manager");
51 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
52 DatabaseManager *dbMgr = conn.getDatabaseManager();
53 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
54 #ifndef DEFAULT
55 rv = conn.startTransaction(READ_COMMITTED);
56 #else
57 rv = conn.startTransaction();
58 #endif
60 if (rv != OK) return NULL;
61 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
62 p1RetVal = new int();
63 *p1RetVal = 0;
64 rv = select(dbMgr, 100, true);
65 if (rv != OK) { printf("Test Failed:first thread read failed \n"); *p1RetVal = 1; }
66 selectDone = 1;
67 while (updateDone != 1) {::sleep(1); }
68 rv = select(dbMgr, 100, true, true);
69 if (rv == OK) { printf("Test Failed:first thread read succeeded \n"); *p1RetVal = 1; }
70 allDone = 1;
71 conn.commit();
72 rv = conn.close();
73 pthread_exit(p1RetVal);
75 void* runTest2(void *message)
77 os::sleep(1);
78 Connection conn;
79 DbRetVal rv = conn.open("root", "manager");
80 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
81 DatabaseManager *dbMgr = conn.getDatabaseManager();
82 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
83 #ifndef DEFAULT
84 rv = conn.startTransaction(READ_COMMITTED);
85 #else
86 rv = conn.startTransaction();
87 #endif
89 if (rv != OK) return NULL;
90 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
91 while (selectDone != 1) {::sleep(1); }
93 p2RetVal = new int();
94 *p2RetVal = 0;
95 rv = update(dbMgr, 100, true);
96 if (rv != OK) { printf("Test Failed:second thread failed to update\n"); *p2RetVal = 1; }
97 updateDone = 1;
98 while (allDone !=1) ::sleep(1);
99 conn.commit();
100 conn.close();
101 pthread_exit(p2RetVal);