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
|
#include "../test.h"
#include "../../src/alloc.h"
#include "../../src/asfd.h"
#include "../../src/async.h"
#include "../../src/iobuf.h"
#include "../../src/client/monitor.h"
#include "../builders/build_asfd_mock.h"
static struct ioevent_list areads;
static struct ioevent_list awrites;
static struct ioevent_list ireads;
static struct ioevent_list iwrites;
static struct ioevent_list oreads;
static struct ioevent_list owrites;
static struct async *setup(void)
{
struct async *as;
fail_unless((as=async_alloc())!=NULL);
as->init(as, 0 /* estimate */);
return as;
}
static void tear_down(struct async **as)
{
async_free(as);
alloc_check();
}
static void setup_in_error(struct asfd *sfd,
struct asfd *in, struct asfd *out)
{
int a=0;
int r=0;
asfd_mock_read_no_op(sfd, &a, 1);
asfd_mock_read(in, &r, -1, CMD_GEN, "blah");
}
static void setup_input_to_sfd(struct asfd *sfd,
struct asfd *in, struct asfd *out)
{
int r=0;
int ar=0;
int aw=0;
asfd_mock_read_no_op(sfd, &ar, 3);
asfd_mock_read(in, &r, 0, CMD_GEN, "blah");
asfd_mock_read(in, &r, 0, CMD_GEN, "halb");
asfd_assert_write(sfd, &aw, 0, CMD_GEN, "blah");
asfd_assert_write(sfd, &aw, 0, CMD_GEN, "halb");
asfd_mock_read(in, &r, -1, CMD_GEN, "blah2");
}
static void setup_sfd_to_output(struct asfd *sfd,
struct asfd *in, struct asfd *out)
{
int a=0;
int r=0;
int w=0;
asfd_mock_read_no_op(in, &r, 3);
asfd_mock_read(sfd, &a, 0, CMD_GEN, "blah");
asfd_mock_read(sfd, &a, 0, CMD_GEN, "halb");
asfd_assert_write(out, &w, 0, CMD_GEN, "blah");
asfd_assert_write(out, &w, 0, CMD_GEN, "halb");
asfd_mock_read(sfd, &a, -1, CMD_GEN, "blah2");
}
static int async_rw_both(struct async *as)
{
int ret=0;
struct asfd *sfd=as->asfd;
struct asfd *in=sfd->next;
ret|=sfd->read(sfd);
ret|=in->read(in);
return ret;
}
static void run_test(int expected_ret,
void setup_asfds_callback(struct asfd *sfd,
struct asfd *in, struct asfd *out))
{
struct async *as;
struct asfd *sfd;
struct asfd *in;
struct asfd *out;
as=setup();
sfd=asfd_mock_setup(&areads, &awrites);
in=asfd_mock_setup(&ireads, &iwrites);
out=asfd_mock_setup(&oreads, &owrites);
fail_unless((sfd->desc=strdup_w("main_socket", __func__))!=NULL);
fail_unless((in->desc=strdup_w("stdin", __func__))!=NULL);
fail_unless((out->desc=strdup_w("stdout", __func__))!=NULL);
as->asfd_add(as, sfd);
as->asfd_add(as, in);
as->asfd_add(as, out);
as->read_write=async_rw_both;
setup_asfds_callback(sfd, in, out);
fail_unless(monitor_client_main_loop(as)==expected_ret);
asfd_free(&in);
asfd_free(&out);
asfd_free(&sfd);
asfd_mock_teardown(&areads, &awrites);
asfd_mock_teardown(&ireads, &iwrites);
asfd_mock_teardown(&oreads, &owrites);
tear_down(&as);
}
START_TEST(test_client_monitor_main_loop)
{
run_test(-1, setup_in_error);
run_test(-1, setup_input_to_sfd);
run_test(-1, setup_sfd_to_output);
}
END_TEST
Suite *suite_client_monitor(void)
{
Suite *s;
TCase *tc_core;
s=suite_create("client_monitor");
tc_core=tcase_create("Core");
tcase_set_timeout(tc_core, 60);
tcase_add_test(tc_core, test_client_monitor_main_loop);
suite_add_tcase(s, tc_core);
return s;
}
|