]> arthur.barton.de Git - netatalk.git/blob - test/netalockd/test.c
Merge master
[netatalk.git] / test / netalockd / test.c
1 /*
2  * Copyright (c) 2010 Frank Lahm
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <sys/queue.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <errno.h>
15
16 #include <atalk/logger.h>
17 #include <atalk/errchk.h>
18 #include <atalk/locking.h>
19 #include <atalk/adouble.h>
20 #include <atalk/bstrlib.h>
21 #include <atalk/bstradd.h>
22
23 #include "event2/event-config.h"
24 #include "event2/event.h"
25 #include "event2/http.h"
26 #include "event2/event_compat.h"
27 #include "event2/http_compat.h"
28 #include "event2/rpc.h"
29 #include "event2/rpc_struct.h"
30
31 #include <atalk/lockrpc.gen.h>
32
33 #define NUM_RPCS 10000
34 int count = NUM_RPCS;
35
36 EVRPC_HEADER(lock_msg, lock_req, lock_rep)
37 EVRPC_GENERATE(lock_msg, lock_req, lock_rep)
38
39 static struct event_base *ev_base;
40 static struct evrpc_pool *rpc_pool;
41
42 static void ev_log_cb(int severity, const char *msg)
43 {
44     LOG(log_warning, logtype_default, (char *)msg);
45 }
46
47 static void do_one_rpc(const char *name);
48
49 static void msg_rep_cb(struct evrpc_status *status,
50                        struct lock_req *req,
51                        struct lock_rep *rep,
52                        void *arg)
53 {
54     char buf[32];
55
56         if (status->error != EVRPC_STATUS_ERR_NONE) {
57         LOG(log_warning, logtype_default, "msg_rep_cb: RPC error: %i", status->error);
58         event_base_loopexit(ev_base, NULL);
59         goto exit;
60     }
61
62         if (--count == 0) {
63         LOG(log_warning, logtype_default, "msg_rep_cb: count: %i", count);
64         event_base_loopexit(ev_base, NULL);
65         goto exit;
66     }
67
68     LOG(log_warning, logtype_default, "msg_rep_cb: count: %i", count);
69
70     snprintf(buf, 32, "count: %i", count);
71     do_one_rpc(buf);
72
73 exit:
74     return;
75 }
76
77 static void do_one_rpc(const char *name)
78 {
79     static struct lock *lock = NULL;
80         static struct lock_req *lock_req = NULL;
81         static struct lock_rep *lock_rep = NULL;
82
83     if (lock == NULL) {
84         lock = lock_new();
85         lock_req = lock_req_new();
86         lock_rep = lock_rep_new();
87     } else {
88         lock_clear(lock);
89         lock_req_clear(lock_req);
90         lock_rep_clear(lock_rep);
91     }
92
93     EVTAG_ASSIGN(lock_req, req_lock, lock);
94     EVTAG_ASSIGN(lock_req, req_filename, name);
95
96     EVRPC_MAKE_REQUEST(lock_msg, rpc_pool, lock_req, lock_rep, msg_rep_cb, NULL);
97 }
98
99 static int my_rpc_init(const char *addr, unsigned short port)
100 {
101     EC_INIT;
102     struct evhttp_connection *evcon;
103
104     EC_NULL_LOG(ev_base = event_init());
105     event_set_log_callback(ev_log_cb);
106         EC_NULL_LOG(rpc_pool = evrpc_pool_new(NULL));
107         EC_NULL_LOG(evcon = evhttp_connection_new(addr, port));
108         evrpc_pool_add_connection(rpc_pool, evcon);
109
110 EC_CLEANUP:
111     EC_EXIT;
112 }
113
114 int main(int argc, char **argv)
115 {
116     set_processname("test");
117     setuplog("default log_maxdebug test.log");
118     if (my_rpc_init("127.0.0.1", 4702) != 0)
119         return 1;
120     do_one_rpc("test");
121     event_dispatch();
122     return 0;
123 }