adding test scripts
[csql.git] / test / sqlapi / Csql / Isolation / isotest9.c
blob1e38b97f1f76ddd0acbfbb79dec1f3578aa49fff
1 #include "common.h"
2 //READ_COMMITTED isolation testing
3 // T1 T2
4 // -------------------------
5 // Read
6 // Delete
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, deleteDone =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 # ifdef RDUNCOM
61 rv = conn->beginTrans(READ_UNCOMMITTED);
62 # elif defined RDRPT
63 rv = conn->beginTrans(READ_REPEATABLE);
64 # else
65 rv = conn->beginTrans(READ_COMMITTED);
66 #endif
67 if (rv != OK) return NULL;
68 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
69 p1RetVal = new int();
70 *p1RetVal = 0;
72 rv = select(stmt, 100, true);
73 if (rv != OK) { printf("Test Failed:first thread failed to select\n"); *p1RetVal = 1; }
74 selectDone = 1;
75 while (deleteDone != 1) {::sleep(1); }
76 printf("P2 deleted says P1 and P1 is selecting now\n");
77 rv = select(stmt, 100, false);
78 if (rv == OK) { printf("Test Failed:first thread read succeeded \n"); *p1RetVal = 1; }
79 printf ("P1 second select returned with rv = %d\n", rv);
80 conn->commit();
81 rv = conn->disconnect();
82 stmt->free(); delete stmt; delete conn;
83 printf("conn closed %d for Thread and pid is %d %lu\n", rv, os::getpid(), os::getthrid());
84 pthread_exit(p1RetVal);
87 void* runTest2(void *message)
89 AbsSqlConnection *conn = createConnection();
90 DbRetVal rv = conn->connect("root", "manager");
91 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
92 AbsSqlStatement *stmt = createStatement();
93 stmt->setConnection(conn);
94 # ifdef RDUNCOM
95 rv = conn->beginTrans(READ_UNCOMMITTED);
96 # elif defined RDRPT
97 rv = conn->beginTrans(READ_REPEATABLE);
98 # else
99 rv = conn->beginTrans(READ_COMMITTED);
100 #endif
101 if (rv != OK) return NULL;
102 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
103 while (selectDone != 1) {::sleep(1); }
104 p2RetVal = new int();
105 *p2RetVal = 0;
106 rv = remove(stmt, 100, true);
107 # ifdef RDUNCOM
108 if (rv != OK) {
109 printf("Test Failed:second thread failed to remove.\n");
110 # elif defined RDRPT
111 if (rv == OK) {
112 printf("Test Failed:second thread selects.\n");
113 # else
114 if (rv != OK) {
115 printf("Test Failed:second thread failed to remove\n");
116 #endif
117 *p2RetVal = 1;
119 printf("P2 deleted\n");
120 deleteDone =1;
121 sleep(5);
122 rv = conn->commit();
123 printf("P2 committed\n");
124 rv = conn->disconnect();
125 stmt->free(); delete stmt; delete conn;
126 printf("conn closed %d for Thread and pid is %d %lu\n", rv, os::getpid(), os::getthrid());
127 pthread_exit(p2RetVal);