Skip to content

Commit 00c1103

Browse files
committed
Update ecpg thread testing program to be more automated.
1 parent 9c6d549 commit 00c1103

File tree

1 file changed

+79
-53
lines changed

1 file changed

+79
-53
lines changed
Lines changed: 79 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,96 @@
1-
/* ---
1+
/*
22
* Thread test program
33
* by Philip Yarra
4-
*
5-
* To run, create this table in the 'test' database:
6-
*
7-
* CREATE TABLE foo (
8-
* message character(40)
9-
* );
10-
* ---
114
*/
125

136

147
#include <pthread.h>
158

16-
int main(void);
17-
void ins1(void);
18-
void ins2(void);
9+
void ins1(void);
10+
void ins2(void);
1911

20-
int main(void)
12+
EXEC SQL BEGIN DECLARE SECTION;
13+
char *dbname;
14+
EXEC SQL END DECLARE SECTION;
15+
16+
int
17+
main(int argc, char **argv)
2118
{
22-
pthread_t thread1, thread2;
23-
pthread_create(&thread1, NULL, (void *) ins1, NULL);
24-
pthread_create(&thread2, NULL, (void *) ins2, NULL);
25-
pthread_join(thread1, NULL);
26-
pthread_join(thread2, NULL);
27-
printf("Program done!\n");
28-
return 0;
19+
pthread_t thread1,
20+
thread2;
21+
EXEC SQL BEGIN DECLARE SECTION;
22+
int rows;
23+
EXEC SQL END DECLARE SECTION;
24+
25+
if (argc != 2)
26+
{
27+
fprintf(stderr, "Usage: %s dbname\n", argv[0]);
28+
return 1;
29+
}
30+
dbname = argv[1];
31+
32+
EXEC SQL CONNECT TO:dbname AS test0;
33+
34+
/* DROP might fail */
35+
EXEC SQL AT test0 DROP TABLE test_thread;
36+
EXEC SQL AT test0 COMMIT WORK;
37+
EXEC SQL AT test0 CREATE TABLE test_thread(message character(40));
38+
EXEC SQL AT test0 COMMIT WORK;
39+
EXEC SQL DISCONNECT test0;
40+
41+
pthread_create(&thread1, NULL, (void *) ins1, NULL);
42+
pthread_create(&thread2, NULL, (void *) ins2, NULL);
43+
pthread_join(thread1, NULL);
44+
pthread_join(thread2, NULL);
45+
EXEC SQL CONNECT TO:dbname AS test3;
46+
EXEC SQL AT test3 SELECT COUNT(*) INTO :rows FROM test_thread;
47+
EXEC SQL AT test3 COMMIT WORK;
48+
EXEC SQL DISCONNECT test3;
49+
50+
if (rows == 10)
51+
printf("Success.\n");
52+
else
53+
printf("Failure.\n");
54+
return 0;
2955
}
3056

31-
void ins1(void)
57+
void
58+
ins1(void)
3259
{
33-
int i;
34-
EXEC SQL BEGIN DECLARE SECTION;
35-
char* cs = "test";
36-
char* bar = "one!";
37-
EXEC SQL END DECLARE SECTION;
38-
EXEC SQL WHENEVER sqlerror sqlprint;
39-
EXEC SQL CONNECT TO :cs AS test1;
40-
for (i = 0; i < 5; i++)
41-
{
42-
printf("thread 1 : inserting\n");
43-
EXEC SQL AT test1 INSERT INTO foo VALUES(:bar);
44-
printf("thread 1 : insert done\n");
45-
}
46-
EXEC SQL AT test1 COMMIT WORK;
47-
EXEC SQL DISCONNECT test1;
48-
printf("thread 1 : done!\n");
60+
int i;
61+
EXEC SQL WHENEVER sqlerror sqlprint;
62+
EXEC SQL CONNECT TO:dbname AS test1;
63+
64+
for (i = 0; i < 5; i++)
65+
{
66+
printf("thread 1 : inserting\n");
67+
EXEC SQL AT test1 INSERT INTO test_thread VALUES('thread1');
68+
69+
printf("thread 1 : insert done\n");
70+
}
71+
EXEC SQL AT test1 COMMIT WORK;
72+
EXEC SQL DISCONNECT test1;
73+
74+
printf("thread 1 : done!\n");
4975
}
5076

5177

52-
void ins2(void)
78+
void
79+
ins2(void)
5380
{
54-
int i;
55-
EXEC SQL BEGIN DECLARE SECTION;
56-
char* cs = "test";
57-
char* bar = "two!";
58-
EXEC SQL END DECLARE SECTION;
59-
EXEC SQL WHENEVER sqlerror sqlprint;
60-
EXEC SQL CONNECT TO :cs AS test2;
61-
for (i = 0; i < 5; i++)
62-
{
63-
printf("thread 2: inserting\n");
64-
EXEC SQL AT test2 INSERT INTO foo VALUES(:bar);
65-
printf("thread 2: insert done\n");
66-
}
67-
EXEC SQL AT test2 COMMIT WORK;
68-
EXEC SQL DISCONNECT test2;
69-
printf("thread 2: done!\n");
81+
int i;
82+
EXEC SQL WHENEVER sqlerror sqlprint;
83+
EXEC SQL CONNECT TO:dbname AS test2;
84+
85+
for (i = 0; i < 5; i++)
86+
{
87+
printf("thread 2: inserting\n");
88+
EXEC SQL AT test2 INSERT INTO test_thread VALUES('thread2');
89+
90+
printf("thread 2: insert done\n");
91+
}
92+
EXEC SQL AT test2 COMMIT WORK;
93+
EXEC SQL DISCONNECT test2;
94+
95+
printf("thread 2: done!\n");
7096
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy