Skip to content

Commit

Permalink
lib-http: test-http-client: Run several clients simultaneously.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanbosch authored and cmouse committed Dec 7, 2017
1 parent 27a2e59 commit faa4f87
Showing 1 changed file with 50 additions and 8 deletions.
58 changes: 50 additions & 8 deletions src/lib-http/test-http-client.c
Expand Up @@ -19,6 +19,8 @@ struct http_test_request {
bool write_output;
};

static struct ioloop *ioloop;

static void payload_input(struct http_test_request *req)
{
const unsigned char *data;
Expand Down Expand Up @@ -53,6 +55,8 @@ static void
got_request_response(const struct http_response *response,
struct http_test_request *req)
{
io_loop_stop(ioloop);

if (response->status / 100 != 2) {
i_error("HTTP Request failed: %s", response->reason);
i_free(req);
Expand Down Expand Up @@ -340,10 +344,10 @@ int main(int argc, char *argv[])
struct dns_client *dns_client;
struct dns_lookup_settings dns_set;
struct http_client_settings http_set;
struct http_client *http_client;
struct http_client_context *http_cctx;
struct http_client *http_client1, *http_client2, *http_client3, *http_client4;
struct ssl_iostream_settings ssl_set;
const char *error;
struct ioloop *ioloop;

lib_init();
#ifdef HAVE_OPENSSL
Expand Down Expand Up @@ -382,24 +386,62 @@ int main(int argc, char *argv[])
http_set.debug = TRUE;
http_set.rawlog_dir = "/tmp/http-test";

http_client = http_client_init(&http_set);
http_cctx = http_client_context_create(&http_set);

http_client1 = http_client_init_shared(http_cctx, NULL);
http_client2 = http_client_init_shared(http_cctx, NULL);
http_client3 = http_client_init_shared(http_cctx, NULL);
http_client4 = http_client_init_shared(http_cctx, NULL);

switch (argc) {
case 1:
run_tests(http_client);
run_tests(http_client1);
run_tests(http_client2);
run_tests(http_client3);
run_tests(http_client4);
break;
case 2:
run_http_get(http_client, argv[1]);
run_http_get(http_client1, argv[1]);
run_http_get(http_client2, argv[1]);
run_http_get(http_client3, argv[1]);
run_http_get(http_client4, argv[1]);
break;
case 3:
run_http_post(http_client, argv[1], argv[2]);
run_http_post(http_client1, argv[1], argv[2]);
run_http_post(http_client2, argv[1], argv[2]);
run_http_post(http_client3, argv[1], argv[2]);
run_http_post(http_client4, argv[1], argv[2]);
break;
default:
i_fatal("Too many parameters");
}

http_client_wait(http_client);
http_client_deinit(&http_client);
for (;;) {
bool pending = FALSE;

if (http_client_get_pending_request_count(http_client1) > 0) {
i_debug("Requests still pending in client 1");
pending = TRUE;
} else if (http_client_get_pending_request_count(http_client2) > 0) {
i_debug("Requests still pending in client 2");
pending = TRUE;
} else if (http_client_get_pending_request_count(http_client3) > 0) {
i_debug("Requests still pending in client 3");
pending = TRUE;
} else if (http_client_get_pending_request_count(http_client4) > 0) {
i_debug("Requests still pending in client 4");
pending = TRUE;
}
if (!pending)
break;
io_loop_run(ioloop);
}
http_client_deinit(&http_client1);
http_client_deinit(&http_client2);
http_client_deinit(&http_client3);
http_client_deinit(&http_client4);

http_client_context_unref(&http_cctx);

dns_client_deinit(&dns_client);

Expand Down

0 comments on commit faa4f87

Please sign in to comment.