Updated vsys conc test
[tests.git] / qaapi / qa / tests / node / support / vsys_conctest.c
1 #include <stdio.h>
2 #include <sys/select.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <errno.h>
10
11 #define IMAPIG 1024
12
13 int main(int argc,char *argv[])
14 {
15   FILE *fp = NULL, *fp_in = NULL;
16   FILE *out_fp = NULL, *diff_fp = NULL;
17
18   if (argc<2) {
19           printf("usage: vsys_conctest <slicename>\n");
20           exit(-1);
21   }
22   const char topcmd[IMAPIG];
23   const char top_in_file[IMAPIG];
24   char buf[IMAPIG*8];
25   int fd_in = -1, fd_out;
26   int res;
27   int flag,flag2;
28   int count = 1;
29   struct timeval tv={.tv_sec=5,.tv_usec=0};
30
31   sprintf("%s/vsys/test.out",argv[1]);
32   sprintf("%s/vsys/test.in",argv[1]);
33   while (count < 200000) {
34     fd_set readSet;
35     int res;
36     int nlines=0;
37
38     
39     printf("(%d)", count);fflush(stdout);
40     if ((fd_out = open(topcmd, O_RDONLY | O_NONBLOCK)) < 0) {
41       fprintf(stderr, "error executing top\n");
42       exit(-1);
43     }
44
45     
46     while ((fd_in = open(top_in_file, O_WRONLY| O_NONBLOCK)) < 0) {
47       fprintf(stderr, "Waiting for %s (%s)\n", top_in_file,strerror(errno));
48         usleep (50); 
49     }
50
51     
52     if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
53       printf("fcntl get failed\n");
54       exit(-1);
55     }
56     
57
58     
59         if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) {
60       printf("fcntl get failed\n");
61       exit(-1);
62     }
63     
64
65
66     while (1) {
67             FD_ZERO(&readSet);
68             FD_SET(fd_out, &readSet);
69
70     
71             res = select(fd_out + 1, &readSet, NULL, NULL, NULL);
72     
73             if (res < 0) {
74                     if (errno == EINTR || errno == EAGAIN) {
75                             printf(".");
76                             continue;
77                     }
78                     fprintf(stderr,"select failed errno=%d errstr=%s\n", errno, strerror(errno));
79                     exit(-1);
80             }
81             break; /* we're done */
82     }
83
84     
85     if (fcntl(fd_out, F_SETFL, flag & ~O_NONBLOCK) == -1) {
86       printf("fcntl set failed\n");
87       exit(-1);
88     }
89     
90
91     
92     if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
93       printf("fcntl get failed\n");
94       exit(-1);
95     }
96     
97
98
99     
100     if (fcntl(fd_in, F_SETFL, flag2 & ~O_NONBLOCK) == -1) {
101       printf("fcntl set failed\n");
102       exit(-1);
103     }
104
105     
106     if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) {
107       printf("fcntl get failed\n");
108       exit(-1);
109     }
110     
111
112     if (flag & O_NONBLOCK == 0) {
113       printf("fd_out still nonblocking\n");
114       exit(-1);
115     }
116
117     if (flag & O_NONBLOCK == 0) {
118       printf("fd_in still nonblocking\n");
119       exit(-1);
120     }
121     if ((fp = fdopen(fd_out, "r")) == NULL) {
122       printf("fdopen failed\n");
123       exit(-1);
124     }
125
126     while (fgets(buf, sizeof(buf), fp) != NULL) {
127             nlines++;
128     }
129
130     if (nlines<5) {
131             printf("Test returned different results - run again to verify\n");
132             exit(-1);
133     }
134
135     fclose(fp);
136     close(fd_in);
137     close(fd_out);
138     count++;
139   }
140   printf("test successful.\n");
141   exit(0);
142
143 }