Statistics
| Branch: | Revision:

peerstreamer-src / Test / task_manager_test.c @ 9eb656e7

History | View | Annotate | Download (3.96 KB)

1 9eb656e7 Luca Baldesi
#include<assert.h>
2
#include<stdio.h>
3
#include<task_manager.h>
4
#include<stdint.h>
5
#include<unistd.h>
6
#include<stdlib.h>
7
#include<string.h>
8
#include<sys/socket.h>
9
#include<sys/time.h>
10
#include <arpa/inet.h>
11
#include <netinet/in.h>
12
#include <sys/types.h>
13
14
int counter = 0;
15
int reset = 0;
16
struct sockaddr_in consumer_addr, producer_addr;
17
int consumer_sock, producer_sock;
18
19
int UDP_socket(struct sockaddr_in * addr, int port)
20
{
21
        int s;
22
        s=socket(AF_INET, SOCK_DGRAM, 0);
23
        memset((char *) addr, 0, sizeof(struct sockaddr_in));
24
        addr->sin_family = AF_INET;
25
        addr->sin_port = htons(port);
26
        inet_aton("127.0.0.1", &(addr->sin_addr));
27
        bind(s, (struct sockaddr *) addr, sizeof(*addr));
28
        return s;
29
}
30
31
uint8_t producer_callback(int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
32
{
33
        char msg[] = "ciao";
34
        sendto(producer_sock, msg, strlen(msg), 0, (struct sockaddr *) &consumer_addr, sizeof(consumer_addr));
35
        return 0;
36
}
37
38
uint8_t dummy_callback(int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
39
{
40
        counter++;
41
        return 0;
42
}
43
44
uint8_t dummy_callback2(int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
45
{
46
        counter--;
47
        return 0;
48
}
49
50
uint8_t dummy_reinit(struct periodic_task * pt)
51
{
52
        reset++;
53
        return 0;
54
}
55
56
uint8_t consumer_reinit(struct periodic_task * pt)
57
{
58
        periodic_task_readfd_add(pt, consumer_sock);
59
        reset++;
60
        return 0;
61
}
62
63
int stopwatch_task_manager_poll(struct task_manager * tm, timeout to, timeout * elapsed)
64
{
65
        int res;
66
        struct timeval t1, t2, diff;
67
68
        gettimeofday(&t1, NULL);
69
        res = task_manager_poll(tm, to);
70
        gettimeofday(&t2, NULL);
71
        timersub(&t2, &t1, &diff);
72
        *elapsed = ((diff.tv_sec) * 1000 + diff.tv_usec/1000.0) + 0.5;
73
        return res;
74
}
75
76
void task_manager_new_test()
77
{
78
        struct task_manager * tm;
79
80
        tm = task_manager_new();
81
        task_manager_destroy(&tm);
82
        assert(tm == NULL);
83
84
        fprintf(stderr,"%s successfully passed!\n",__func__);
85
}
86
87
void task_manager_new_task_test()
88
{
89
        struct periodic_task * res;
90
        struct task_manager * tm;
91
92
        tm = task_manager_new();
93
94
        res = task_manager_new_task(NULL, NULL, NULL, 0);
95
        assert(res == NULL);
96
97
        res = task_manager_new_task(tm, NULL, NULL, 0);
98
        assert(res != NULL);
99
100
        res = task_manager_new_task(tm, dummy_reinit, dummy_callback, 10);
101
        assert(res != NULL);
102
103
        task_manager_destroy(&tm);
104
        reset = 0;
105
106
        fprintf(stderr,"%s successfully passed!\n",__func__);
107
}
108
109
void task_manager_poll_test()
110
{
111
        struct task_manager * tm;
112
        int res;
113
        timeout elapsed;
114
115
        /****** timeout part ***********/
116
117
        tm = task_manager_new();
118
119
        task_manager_new_task(tm, dummy_reinit, dummy_callback, 10);
120
        assert(reset == 1);
121
122
        res = task_manager_poll(NULL, 0);
123
        assert(res < 0);
124
125
        res = stopwatch_task_manager_poll(tm, 0, &elapsed);
126
127
        assert(res == 0);
128
        assert((elapsed) < 5);
129
130
        res = stopwatch_task_manager_poll(tm, 1000, &elapsed);
131
132
        assert(res == 0);
133
        assert((elapsed) < 15);
134
        assert(counter == 1);
135
        assert(reset == 2);
136
137
        task_manager_new_task(tm, dummy_reinit, dummy_callback2, 5);
138
        assert(reset == 3);
139
140
        res = stopwatch_task_manager_poll(tm, 1000, &elapsed);
141
142
        assert(res == 0);
143
        assert((elapsed) < 10);
144
        assert(counter == 0);
145
        assert(reset == 4);
146
147
        task_manager_destroy(&tm);
148
149
        /****** File descriptor part ***********/
150
151
        char tmpfile[] = "/tmp/hello-XXXXXX";
152
        counter = 0;
153
        reset = 0;
154
155
        tm = task_manager_new();
156
        consumer_sock = UDP_socket(&consumer_addr, 6000);
157
        producer_sock = UDP_socket(&producer_addr, 7000);
158
159
        task_manager_new_task(tm, dummy_reinit, producer_callback, 5);
160
        task_manager_new_task(tm, consumer_reinit, dummy_callback, 100);
161
        assert(reset == 2);
162
163
        res = stopwatch_task_manager_poll(tm, 1000, &elapsed);
164
165
        assert((elapsed) < 10);
166
        assert(reset == 3); // producer executed
167
168
        res = stopwatch_task_manager_poll(tm, 1000, &elapsed);
169
170
        assert((elapsed) < 5); // the consumer has been waken up by the producer
171
        assert(reset == 4); 
172
        assert(counter == 1); 
173
174
        task_manager_destroy(&tm);
175
176
        counter = 0;
177
        reset = 0;
178
        fprintf(stderr,"%s successfully passed!\n",__func__);
179
}
180
181
int main(char ** argc,int argv)
182
{
183
        task_manager_new_test();
184
        task_manager_new_task_test();
185
        task_manager_poll_test();
186
        return 0;
187
}