fixing timing issue in test script
[csql.git] / test / dbapi / Connection / isotest11.c
blob0da010b9e88755a4cad34f799ab7b5a8a0a8cd9c
1 #include "common.h"
2 //READ_UNCOMMITTED isolation testing
3 //T1 and T2 both inserting, T2 will fail saying unique key constraint
5 void* runTest1(void *p);
6 void* runTest2(void *p);
7 int *p1RetVal = NULL;
8 int *p2RetVal = NULL;
9 int main()
12 Connection conn;
13 DbRetVal rv = conn.open("root", "manager");
14 if (rv != OK) { printf("Error during connection %d\n", rv); return 1; }
15 DatabaseManager *dbMgr = conn.getDatabaseManager();
16 int ret = createTable(dbMgr);
17 if (ret != 0) { return 1; }
19 pthread_t thr[2];
20 int *status1, *status2;
21 pthread_create (&thr[0], NULL, &runTest1, NULL);
22 pthread_create (&thr[1], NULL, &runTest2, NULL);
23 printf("All threads started\n");
25 pthread_join(thr[0], (void**)&status1);
26 pthread_join(thr[1], (void**)&status2);
27 ret = 0;
28 if (*status1 != 0 || *status2 != 0) ret = 1;
29 if (p1RetVal) { delete p1RetVal; p1RetVal = NULL; }
30 if (p2RetVal) { delete p2RetVal; p2RetVal = NULL; }
31 dbMgr->dropTable("t1");
32 conn.close();
33 return ret;
35 void* runTest1(void *message)
37 Connection conn;
38 DbRetVal rv = conn.open("root", "manager");
39 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
40 DatabaseManager *dbMgr = conn.getDatabaseManager();
41 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
42 rv = conn.startTransaction(READ_UNCOMMITTED);
43 if (rv != OK) return NULL;
44 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
45 p1RetVal = new int();
46 *p1RetVal = 0;
48 rv = insert(dbMgr, 100, true);
49 if (rv != OK) { printf("Test Failed:first thread failed to insert\n"); *p1RetVal = 1; }
51 conn.commit();
52 rv = conn.close();
53 printf("conn closed %d for Thread and pid is %d %lu\n", rv, os::getpid(), os::getthrid());
54 pthread_exit(p1RetVal);
56 void* runTest2(void *message)
58 os::sleep(1);
59 Connection conn;
60 DbRetVal rv = conn.open("root", "manager");
61 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
62 DatabaseManager *dbMgr = conn.getDatabaseManager();
63 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
64 rv = conn.startTransaction(READ_UNCOMMITTED);
65 if (rv != OK) return NULL;
66 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
68 p2RetVal = new int();
69 *p2RetVal = 0;
71 rv = insert(dbMgr, 100, false);
72 if (rv == OK) { printf("Test Failed:second thread inserted\n"); *p2RetVal = 1; }
74 conn.commit();
75 rv = conn.close();
76 printf("conn closed %d for Thread and pid is %d %lu\n", rv, os::getpid(), os::getthrid());
77 pthread_exit(p2RetVal);