fixing timing issue in test script
[csql.git] / test / dbapi / Connection / isotest14.c
bloba2e06740035b28c8d95c3ee592767567e53faa5d
1 #include "common.h"
2 //READ_UNCOMMITTED isolation testing
3 //T1 doing select and T2 doing update for same tuple
4 //Both should succeed
6 void* runTest1(void *p);
7 void* runTest2(void *p);
8 int *p1RetVal = NULL;
9 int *p2RetVal = NULL;
11 int main()
14 Connection conn;
15 DbRetVal rv = conn.open("root", "manager");
16 if (rv != OK) { printf("Error during connection %d\n", rv); return 1; }
17 DatabaseManager *dbMgr = conn.getDatabaseManager();
18 int ret = createTable(dbMgr);
19 if (ret != 0) { return 1; }
21 rv = conn.startTransaction();
22 if (rv != OK) {printf ("Unable to start trans\n"); return 1; }
23 rv = insert(dbMgr, 100, false);
24 if (rv != OK) { printf("Unable to insert\n"); return 2; }
25 conn.commit();
26 printf("Tuple inserted\n");
28 pthread_t thr[2];
29 int *status1, *status2;
30 pthread_create (&thr[0], NULL, &runTest1, NULL);
31 pthread_create (&thr[1], NULL, &runTest2, NULL);
32 printf("All threads started\n");
34 pthread_join(thr[0], (void**)&status1);
35 pthread_join(thr[1], (void**)&status2);
36 ret = 0;
37 if (*status1 != 0 || *status2 != 0) ret = 1;
38 dbMgr->dropTable("t1");
39 conn.close();
40 if (p1RetVal) { delete p1RetVal; p1RetVal = NULL; }
41 if (p2RetVal) { delete p2RetVal; p2RetVal = NULL; }
43 return ret;
45 void* runTest1(void *message)
47 Connection conn;
48 DbRetVal rv = conn.open("root", "manager");
49 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
50 DatabaseManager *dbMgr = conn.getDatabaseManager();
51 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
52 rv = conn.startTransaction(READ_UNCOMMITTED);
53 if (rv != OK) return NULL;
54 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
55 p1RetVal = new int();
56 *p1RetVal = 0;
57 rv = select(dbMgr, 100, true);
58 if (rv != OK) { printf("Test Failed:first thread failed to select\n"); *p1RetVal = 1; }
60 conn.commit();
61 rv = conn.close();
62 pthread_exit(p1RetVal);
64 void* runTest2(void *message)
66 os::sleep(1);
67 Connection conn;
68 DbRetVal rv = conn.open("root", "manager");
69 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
70 DatabaseManager *dbMgr = conn.getDatabaseManager();
71 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
72 rv = conn.startTransaction(READ_UNCOMMITTED);
73 if (rv != OK) return NULL;
74 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
76 p2RetVal = new int();
77 *p2RetVal = 0;
78 rv = update(dbMgr, 100, false);
79 if (rv != OK) { printf("Test Failed:second thread failed to update\n"); *p2RetVal = 1; }
80 conn.commit();
81 conn.close();
82 pthread_exit(p2RetVal);