-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathio_test_full.c
146 lines (115 loc) · 3.53 KB
/
io_test_full.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <stdio.h>
#include <curl/curl.h>
#include <fcntl.h>
#include "email.h"
#include "io.h"
#include "io_curl.h"
#include "io_test.h"
#include "logging.h"
u8 starting_email = 0;
u64 now_ms() {
if (starting_email) {
return 0;
} else {
return real_now_ms();
}
}
IO_TIMEOUT_CALLBACK(idle) {}
IO_TIMEOUT_CALLBACK(logging_send) {
INFO();
IO_TIMER_MS_SET(logging_send, -1);
events_pending--;
}
typedef struct {
FILE* f;
enum _io_curl_type curl_type;
int id;
} DLCtx;
typedef struct {
enum _io_curl_type curl_type;
int id;
struct email_Send email;
char *body;
} EmailCtx;
IO_CURL_SETUP(test, DLCtx, curl_type);
IO_CURL_SETUP(logging, EmailCtx, curl_type);
void test_io_curl_complete(CURL* easy, CURLcode result, DLCtx * ctx) {
INFO("test_sort:A%02d result: %s", ctx->id, curl_easy_strerror(result));
int r = fclose(ctx->f); error_check(r);
events_pending--;
curl_easy_cleanup(easy);
}
void logging_io_curl_complete(CURL* easy, CURLcode result, EmailCtx * ctx) {
INFO("test_sort:B%02d result: %s", ctx->id, curl_easy_strerror(result));
events_pending--;
email_free(&ctx->email);
free(ctx->body); ctx->body = 0;
}
DLCtx dl_ctx[10];
EmailCtx email_ctx[10];
char * email_from = "[email protected]";
char * email_host = "smtp://127.0.0.1:8025";
char * email_user_pass = "username:password";
void test_main() {
io_initialize();
io_curl_initialize();
for (int i=0; i<COUNT(dl_ctx); i++) {
char buf[1024];
snprintf(buf, sizeof buf, "/build/testfile-%02d", i);
unlink(buf);
int fd = open(buf, O_CREAT | O_WRONLY, 0644);
error_check(fd);
int r = dprintf(fd, "%s", buf);
error_check(r);
r = close(fd);
error_check(r);
}
system("rm -f /build/io_test_full_* /build/email_mock_io_test_full*");
start_time = IO_NOW_MS() + 50;
starting_email = 1;
for (int i=0; i<COUNT(dl_ctx); i++) {
char buf[1024];
CURL*easy = test_io_curl_create_handle(&dl_ctx[i]);
snprintf(buf, sizeof buf, "/build/io_test_full_%02d", i);
dl_ctx[i].f = fopen(buf, "w"); error_check(dl_ctx[i].f?0:-1);
dl_ctx[i].id = i;
CURLESET(WRITEDATA, dl_ctx[i].f);
snprintf(buf, sizeof buf, "http://127.0.0.1:9161/build/testfile-%02d", i);
CURLESET(URL, buf);
//CURLESET(VERBOSE, 1);
events_pending++;
}
memset(email_ctx, 0, sizeof email_ctx);
for (int i=0; i<COUNT(email_ctx); i++) {
asprintf(&email_ctx[i].body, "body: io_test_full%[email protected]", i);
CURL*easy = logging_io_curl_create_handle(&email_ctx[i]);
email_init(&email_ctx[i].email, easy, email_ctx[i].body+6,
email_ctx[i].body, strlen(email_ctx[i].body), "asdasdf");
// CURLESET(VERBOSE, 1);
events_pending++;
}
starting_email = 0;
IO_TIMER_MS_SET(logging_send, start_time);
events_pending++;
INFO("Running all events:"); { LOGCTX("\t");
while (events_pending > 0) {
io_process_events();
io_curl_process_events();
}
}
for (int i=0; i<COUNT(dl_ctx); i++) {
char buf[1024];
// im not sure why we need a grep here, but without it cat isn't dumping the file? maybe buffering?
snprintf(buf, sizeof buf, "cat /build/io_test_full_%02d | grep -v 'asdfasdf' ", i);
INFO("%s", buf);
int r = system(buf); error_check(r);
}
for (int i=0; i<COUNT(email_ctx); i++) {
char buf[1024];
snprintf(buf, sizeof buf, "cat /build/email_mock_io_test_full%[email protected]", i);
INFO("%s", buf);
int r = system(buf); error_check(r);
}
curl_global_cleanup();
}