changes for libcsqlstorage and inclusion of libcsqlbase
[csql.git] / test / sqlapi / Csql / Isolation / isotest10.c
blob31caf770193e0012f396445b703c4196d9b35a5c
1 #include "common.h"
2 //READ_COMMITTED isolation testing
3 // T1 T2
4 // -------------------------
5 // Read
6 // Update
7 // Read
8 // T1 second read should fail saying "tuple not found
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;
15 int main()
18 AbsSqlConnection *conn = createConnection();
19 DbRetVal rv = conn->connect("root", "manager");
20 if (rv != OK) { printf("Error during connection %d\n", rv); return 1; }
21 AbsSqlStatement *stmt = createStatement();
22 stmt->setConnection(conn);
24 char statement[1024]="create table t1 (f1 int, f2 char(196), primary key(f1));";
25 int ret = executeDdlQuery(conn, stmt, statement);
26 if (ret != 0) { return 1; }
27 rv = conn->beginTrans(READ_COMMITTED);
28 if (rv != OK) return 2;
29 rv = insert(stmt, 100, false);
30 if (rv != OK) { printf("Error in inserting the record\n"); return 3; }
31 conn->commit();
32 printf("Tuple inserted\n");
34 pthread_t thr[2];
35 int *status1, *status2;
36 pthread_create (&thr[0], NULL, &runTest1, NULL);
37 pthread_create (&thr[1], NULL, &runTest2, NULL);
38 printf("All threads started\n");
40 pthread_join(thr[0], (void**)&status1);
41 pthread_join(thr[1], (void**)&status2);
42 strcpy(statement, "drop table t1;");
43 ret = executeDdlQuery(conn, stmt, statement);
44 if (ret != 0) { return 3; }
45 conn->disconnect();
46 stmt->free(); delete stmt; delete conn;
47 if (*status1 != 0 || *status2 != 0) return 4;
48 if (p1RetVal) { delete p1RetVal; p1RetVal = NULL; }
49 if (p2RetVal) { delete p2RetVal; p2RetVal = NULL; }
50 return 0;
53 void* runTest1(void *message)
55 AbsSqlConnection *conn = createConnection();
56 DbRetVal rv = conn->connect("root", "manager");
57 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
58 AbsSqlStatement *stmt = createStatement();
59 stmt->setConnection(conn);
60 rv = conn->beginTrans(READ_COMMITTED);
61 if (rv != OK) return NULL;
62 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
63 p1RetVal = new int();
64 *p1RetVal = 0;
66 rv = select(stmt, 100, true);
67 if (rv != OK) { printf("Test Failed:first thread failed to select\n"); *p1RetVal = 1; }
68 selectDone = 1;
69 while (updateDone != 1) {::sleep(1); }
70 rv = select(stmt, 100, true, true);
71 if (rv == OK) { printf("Test Failed:first thread read succeeded \n"); *p1RetVal = 1; }
72 allDone = 1;
73 conn->commit();
74 rv = conn->disconnect();
75 stmt->free(); delete stmt; delete conn;
76 printf("conn closed %d for Thread and pid is %d %lu\n", rv, os::getpid(), os::getthrid());
77 pthread_exit(p1RetVal);
80 void* runTest2(void *message)
82 AbsSqlConnection *conn = createConnection();
83 DbRetVal rv = conn->connect("root", "manager");
84 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
85 AbsSqlStatement *stmt = createStatement();
86 stmt->setConnection(conn);
87 rv = conn->beginTrans(READ_COMMITTED);
88 if (rv != OK) return NULL;
89 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
90 while (selectDone != 1) {::sleep(1); }
91 p2RetVal = new int();
92 *p2RetVal = 0;
93 rv = update(stmt, 100, true);
94 if (rv != OK) {
95 printf("Test Failed:second thread failed to update\n");
96 *p2RetVal = 1;
98 updateDone = 1;
99 while (allDone !=1) ::sleep(1);
100 rv = conn->commit();
101 rv = conn->disconnect();
102 stmt->free(); delete stmt; delete conn;
103 printf("conn closed %d for Thread and pid is %d %lu\n", rv, os::getpid(), os::getthrid());
104 pthread_exit(p2RetVal);