adding test scripts
[csql.git] / test / sqlapi / Csql / Isolation / isotest8.c
blob22bc0a86475d778e939c7fa568d6ad1427fa0c81
1 #include "common.h"
2 //READ_COMMITTED isolation testing
3 //T1 doing update and T2 doing delete for same tuple
5 void* runTest1(void *p);
6 void* runTest2(void *p);
7 int *p1RetVal = NULL;
8 int *p2RetVal = NULL;
10 int updateDone1 = 0, updateDone2=0;
11 int main()
14 AbsSqlConnection *conn = createConnection();
15 DbRetVal rv = conn->connect("root", "manager");
16 if (rv != OK) { printf("Error during connection %d\n", rv); return 1; }
17 AbsSqlStatement *stmt = createStatement();
18 stmt->setConnection(conn);
20 char statement[1024]="create table t1 (f1 int, f2 char(196), primary key(f1));";
21 int ret = executeDdlQuery(conn, stmt, statement);
22 if (ret != 0) { return 1; }
23 rv = conn->beginTrans(READ_COMMITTED);
24 if (rv != OK) return 2;
25 rv = insert(stmt, 100, false);
26 if (rv != OK) { printf("Error in inserting the record\n"); return 3; }
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 strcpy(statement, "drop table t1;");
39 ret = executeDdlQuery(conn, stmt, statement);
40 if (ret != 0) { return 3; }
41 conn->disconnect();
42 stmt->free(); delete stmt; delete conn;
43 if (*status1 != 0 || *status2 != 0) return 4;
44 if (p1RetVal) { delete p1RetVal; p1RetVal = NULL; }
45 if (p2RetVal) { delete p2RetVal; p2RetVal = NULL; }
46 return 0;
49 void* runTest1(void *message)
51 AbsSqlConnection *conn = createConnection();
52 DbRetVal rv = conn->connect("root", "manager");
53 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
54 AbsSqlStatement *stmt = createStatement();
55 stmt->setConnection(conn);
56 # ifdef RDUNCOM
57 rv = conn->beginTrans(READ_UNCOMMITTED);
58 # elif defined RDRPT
59 rv = conn->beginTrans(READ_REPEATABLE);
60 # else
61 rv = conn->beginTrans(READ_COMMITTED);
62 #endif
63 if (rv != OK) return NULL;
64 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
65 p1RetVal = new int();
66 *p1RetVal = 0;
68 rv = update(stmt, 100, true);
69 if (rv != OK) { printf("Test Failed:first thread failed to update\n"); *p1RetVal = 1; }
70 updateDone1 = 1;
71 while (updateDone2 != 1) sleep(1);
72 rv = select(stmt, 100, true, true);
73 if (rv != OK) { printf("Test Failed:first thread read wrong value.\n"); *p1RetVal = 1; }
74 conn->commit();
75 conn->disconnect();
76 stmt->free(); delete stmt; delete conn;
77 printf("conn closed %d for Thread and pid is %d %lu\n", rv, os::getpid(), os::getthrid());
78 pthread_exit(p1RetVal);
81 void* runTest2(void *message)
83 AbsSqlConnection *conn = createConnection();
84 DbRetVal rv = conn->connect("root", "manager");
85 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
86 AbsSqlStatement *stmt = createStatement();
87 stmt->setConnection(conn);
88 # ifdef RDUNCOM
89 rv = conn->beginTrans(READ_UNCOMMITTED);
90 # elif defined RDRPT
91 rv = conn->beginTrans(READ_REPEATABLE);
92 # else
93 rv = conn->beginTrans(READ_COMMITTED);
94 #endif
95 if (rv != OK) return NULL;
96 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
97 p2RetVal = new int();
98 *p2RetVal = 0;
99 rv = update(stmt, 100, true, "RITHISH");
100 if (rv == OK) {
101 printf("Test Failed:second thread updates.\n");
102 *p2RetVal = 1;
104 updateDone2 = 1;
105 rv = conn->commit();
106 rv = conn->disconnect();
107 stmt->free(); delete stmt; delete conn;
108 printf("conn closed %d for Thread and pid is %d %lu\n", rv, os::getpid(), os::getthrid());
109 pthread_exit(p2RetVal);