Permalink
Browse files

added redirects facility that allows us to mock various functions

  • Loading branch information...
1 parent a646a8a commit 71bd8d41397d2ebcdde9e2278ffba871f02fbf1c Aliaksey Kandratsenka committed Sep 24, 2009
Showing with 115 additions and 2 deletions.
  1. +2 −2 Makefile.am
  2. +3 −0 agent.h
  3. +12 −0 agent_stats.c
  4. +1 −0 check_moxi_agent.c
  5. +61 −0 redirects.c
  6. +36 −0 redirects.h
View
@@ -163,8 +163,8 @@ check_moxi_CFLAGS = @CHECK_CFLAGS@ $(moxi_debug_CPPFLAGS) -DMAIN_CHECK
check_moxi_LDADD = @CHECK_LIBS@ $(moxi_debug_LDADD)
if BUILD_WITH_CONFLATE
-check_moxi_agent_SOURCES = check_moxi_agent.c $(moxi_debug_SOURCES)
-check_moxi_agent_CFLAGS = @CHECK_CFLAGS@ $(moxi_debug_CPPFLAGS) -DMAIN_CHECK
+check_moxi_agent_SOURCES = check_moxi_agent.c redirects.c $(moxi_debug_SOURCES)
+check_moxi_agent_CFLAGS = @CHECK_CFLAGS@ $(moxi_debug_CPPFLAGS) -DMAIN_CHECK -DREDIRECTS_FOR_MOCKS
check_moxi_agent_LDADD = @CHECK_LIBS@ $(moxi_debug_LDADD)
endif
View
@@ -4,6 +4,9 @@
#define AGENT_H
#include <conflate.h>
+#ifdef REDIRECTS_FOR_MOCKS
+#include "redirects.h"
+#endif
int cproxy_init_agent(char *cfg_str,
proxy_behavior behavior,
View
@@ -15,6 +15,10 @@
#include "work.h"
#include "agent.h"
+#ifdef REDIRECTS_FOR_MOCKS
+#include "redirects.h"
+#endif
+
// Local declarations.
//
static void add_stat_prefix(const void *dump_opaque,
@@ -118,7 +122,11 @@ struct stats_gathering_pair {
genhash_t *map_key_stats; // maps "<proxy-name>:<port>" strings to (genhash that maps key names to (struct key_stats *))
};
+#ifndef REDIRECTS_FOR_MOCKS
static
+#else
+#undef collect_memcached_stats_for_proxy
+#endif
void collect_memcached_stats_for_proxy(struct main_stats_collect_info *msci, const char *proxy_name, int proxy_port) {
memcached_st mst;
@@ -157,6 +165,10 @@ void collect_memcached_stats_for_proxy(struct main_stats_collect_info *msci, con
out_free:
memcached_free(&mst);
}
+#ifdef REDIRECTS_FOR_MOCKS
+#define collect_memcached_stats_for_proxy redirected_collect_memcached_stats_for_proxy
+#endif
+
/* This callback is invoked by conflate on a conflate thread
* when it wants proxy stats.
View
@@ -207,6 +207,7 @@ END_TEST
static
void setup(void)
{
+ reset_redirections();
start_main("moxi", NULL);
assert(!is_listen_thread());
proxy_behavior pbg = behavior_default_g;
View
@@ -0,0 +1,61 @@
+#include "redirects.h"
+
+#ifdef HAVE_CONFLATE_H
+
+#undef conflate_add_field
+#undef conflate_add_field_multi
+#undef conflate_next_fieldset
+#undef conflate_init_form
+
+void (*redirected_conflate_add_field_target)(conflate_form_result *r, const char *k, const char *v) = conflate_add_field;
+void (*redirected_conflate_add_field_multi_target)(conflate_form_result *r, const char *k, const char **v) = conflate_add_field_multi;
+void (*redirected_conflate_next_fieldset_target)(conflate_form_result *r) = conflate_next_fieldset;
+void (*redirected_conflate_init_form_target)(conflate_form_result *r) = conflate_init_form;
+
+#endif
+
+#undef collect_memcached_stats_for_proxy
+
+void (*redirected_collect_memcached_stats_for_proxy_target)(struct main_stats_collect_info *msci,
+ const char *proxy_name,
+ int proxy_port) = collect_memcached_stats_for_proxy;
+void redirected_collect_memcached_stats_for_proxy(struct main_stats_collect_info *msci,
+ const char *proxy_name,
+ int proxy_port)
+{
+ (*redirected_collect_memcached_stats_for_proxy_target)(msci, proxy_name, proxy_port);
+}
+
+
+void reset_redirections(void)
+{
+ redirected_collect_memcached_stats_for_proxy_target = collect_memcached_stats_for_proxy;
+#ifdef HAVE_CONFLATE_H
+ redirected_conflate_add_field_target = conflate_add_field;
+ redirected_conflate_add_field_multi_target = conflate_add_field_multi;
+ redirected_conflate_next_fieldset_target = conflate_next_fieldset;
+ redirected_conflate_init_form_target = conflate_init_form;
+#endif
+}
+
+#ifdef HAVE_CONFLATE_H
+void redirected_conflate_add_field(conflate_form_result *r, const char *k, const char *v)
+{
+ (*redirected_conflate_add_field_target)(r,k,v);
+}
+
+void redirected_conflate_add_field_multi(conflate_form_result *r, const char *k, const char **v)
+{
+ (*redirected_conflate_add_field_multi_target)(r,k,v);
+}
+
+void redirected_conflate_next_fieldset(conflate_form_result *r)
+{
+ (*redirected_conflate_next_fieldset_target)(r);
+}
+
+void redirected_conflate_init_form(conflate_form_result *r)
+{
+ (*redirected_conflate_init_form_target)(r);
+}
+#endif
View
@@ -0,0 +1,36 @@
+#ifndef REDIRECTS_H
+#define REDIRECTS_H
+#include "config.h"
+
+#ifdef HAVE_CONFLATE_H
+#include <conflate.h>
+
+#define conflate_add_field redirected_conflate_add_field
+#define conflate_add_field_multi redirected_conflate_add_field_multi
+#define conflate_next_fieldset redirected_conflate_next_fieldset
+#define conflate_init_form redirected_conflate_init_form
+
+extern void (*redirected_conflate_add_field_target)(conflate_form_result *r, const char *k, const char *v);
+extern void (*redirected_conflate_add_field_multi_target)(conflate_form_result *r, const char *k, const char **v);
+extern void (*redirected_conflate_next_fieldset_target)(conflate_form_result *r);
+extern void (*redirected_conflate_init_form_target)(conflate_form_result *r);
+
+void redirected_conflate_add_field(conflate_form_result *r, const char *k, const char *v);
+void redirected_conflate_add_field_multi(conflate_form_result *r, const char *k, const char **v);
+void redirected_conflate_next_fieldset(conflate_form_result *r);
+void redirected_conflate_init_form(conflate_form_result *r);
+
+#endif // HAVE_CONFLATE_H
+
+struct main_stats_collect_info;
+
+void collect_memcached_stats_for_proxy(struct main_stats_collect_info *msci, const char *proxy_name, int proxy_port);
+void redirected_collect_memcached_stats_for_proxy(struct main_stats_collect_info *msci, const char *proxy_name, int proxy_port);
+extern void (*redirected_collect_memcached_stats_for_proxy_target)(struct main_stats_collect_info *msci, const char *proxy_name, int proxy_port);
+
+#define collect_memcached_stats_for_proxy redirected_collect_memcached_stats_for_proxy
+
+void reset_redirections(void);
+
+
+#endif

0 comments on commit 71bd8d4

Please sign in to comment.