[grilo] net: Set config file and throttling via GRL_NET_MOCKED
- From: Juan A. Suarez Romero <jasuarez src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [grilo] net: Set config file and throttling via GRL_NET_MOCKED
- Date: Mon, 22 Oct 2012 17:06:36 +0000 (UTC)
commit c5e6f4457749e2719d3d96c02e7e3fd9b5ace1e3
Author: Mathias Hasselmann <mathias openismus com>
Date: Thu Oct 18 23:47:22 2012 +0200
net: Set config file and throttling via GRL_NET_MOCKED
Also rename grl-mock-data.ini to grl-net-mock-data.ini, to make it clear
this data is for grl-net mocking.
doc/grilo/plugins-testing.xml | 24 +++++---
libs/net/grl-net-mock.c | 128 ++++++++++++++++++++++------------------
libs/net/grl-net-mock.h | 2 +-
libs/net/grl-net-wc.c | 10 ++-
4 files changed, 93 insertions(+), 71 deletions(-)
---
diff --git a/doc/grilo/plugins-testing.xml b/doc/grilo/plugins-testing.xml
index 0fa72e9..ce5f378 100644
--- a/doc/grilo/plugins-testing.xml
+++ b/doc/grilo/plugins-testing.xml
@@ -107,9 +107,21 @@ $ export GRL_DEBUG="registry:*"
</para>
<para>
- To enable mocking set the environment variable GRL_NET_MOCKED.
- If that variable is set to the value "unthrottled", request throttling
- is disabled too.
+ To enable mocking set the environment variable GRL_NET_MOCKED. The value
+ of this variable is interpreted as colon separated list of configuration
+ settings, e.g. <code>config=mock-my-plugin.ini:throttle=0</code>
+
+ <itemizedlist>
+ <listitem>
+ <varname>config</varname> selects a different file than
+ "grl-mock-data.ini" for configuring mock answers.
+ </listitem>
+
+ <listitem>
+ <varname>throttle</varname> overrides any throttling requests from
+ plugins. Set this variable to zero to disable any throttling.
+ </listitem>
+ </itemizedlist>
</para>
<para>
@@ -128,12 +140,6 @@ timeout = 500
</programlisting>
<para>
- The name of the configuration file is either "grl-mock-data.ini" which is
- expected to be in the current directory or can be overridden by setting
- the environment variable GRL_REQUEST_MOCK_FILE.
- </para>
-
- <para>
An easy way to capture the responses is to run your application with the
environment variable GRL_NET_CAPTURE_DIR. GrlNetWc will then write all
each response into a file following the pattern "url-timestamp". If the
diff --git a/libs/net/grl-net-mock.c b/libs/net/grl-net-mock.c
index a1007af..e0d3c61 100644
--- a/libs/net/grl-net-mock.c
+++ b/libs/net/grl-net-mock.c
@@ -40,6 +40,7 @@ static GKeyFile *config = NULL;
static GRegex *ignored_parameters = NULL;
static char *base_path = NULL;
static gboolean enable_mocking = FALSE;
+static guint throttle_override = G_MAXUINT;
gboolean
is_mocked (void)
@@ -48,12 +49,13 @@ is_mocked (void)
}
gboolean
-is_unthrottled (void)
+override_throttling (guint *throttling)
{
- /* Reusing the GRL_NET_MOCKED variable to ensure that throttling
- * only can be disabled in mocked sessions. */
- const char *const env = g_getenv (GRL_NET_MOCKED_VAR);
- return env && g_ascii_strcasecmp(env, "unthrottled") == 0;
+ if (throttle_override == G_MAXUINT)
+ return FALSE;
+
+ *throttling = throttle_override;
+ return TRUE;
}
void
@@ -160,77 +162,83 @@ get_content_mocked (GrlNetWc *self,
void init_mock_requester (GrlNetWc *self)
{
- const char *env;
- GError *error = NULL;
- int version;
- GFile *file, *parent;
-
+ char *config_filename = NULL;
base_path = NULL;
- config = g_key_file_new ();
-
- env = g_getenv (GRL_NET_MOCKED_VAR);
+ /* Parse environment variable. */
+ {
+ const char *const env = g_getenv (GRL_NET_MOCKED_VAR);
- enable_mocking = env
- && strcmp(env, "0")
- && g_ascii_strcasecmp(env, "no")
- && g_ascii_strcasecmp(env, "off")
- && g_ascii_strcasecmp(env, "false");
+ enable_mocking = env
+ && strcmp(env, "0")
+ && g_ascii_strcasecmp(env, "no")
+ && g_ascii_strcasecmp(env, "off")
+ && g_ascii_strcasecmp(env, "false");
- if (!enable_mocking)
+ if (!enable_mocking)
return;
- env = g_getenv ("GRL_REQUEST_MOCK_FILE");
- if (env) {
- GRL_DEBUG ("Trying to load mock file %s", env);
- g_key_file_load_from_file (config,
- env,
- G_KEY_FILE_NONE,
- &error);
- }
- if (error) {
- GRL_WARNING ("Failed to load mock file %s: %s", env, error->message);
- g_error_free (error);
- error = NULL;
- }
+ char **tokens = g_strsplit (env, ":", -1);
- /* Check if we managed to load a file */
- version = g_key_file_get_integer (config, "default", "version", &error);
- if (error || version < GRL_MOCK_VERSION) {
- if (error) {
- g_error_free (error);
- error = NULL;
- } else {
- GRL_WARNING ("Unsupported mock version %d, trying default file.", version);
+ for (int i = 0; tokens[i]; ++i) {
+ if (1 == sscanf (tokens[i], "throttle=%u", &throttle_override))
+ continue;
+
+ if (g_str_has_prefix (tokens[i], "config=")) {
+ g_free (config_filename);
+ config_filename = g_strdup (tokens[i] + strlen ("config="));
+ continue;
+ }
+
+ GRL_WARNING ("Unknown token in \"%s\" variable: \"%s\"",
+ GRL_NET_MOCKED_VAR, tokens[i]);
}
- env = "grl-mock-data.ini";
+ g_strfreev (tokens);
+ }
+
+ /* Read configuration file. */
+ if (config_filename)
+ GRL_DEBUG ("Trying to load mock file \"%s\"", config_filename);
+ else
+ config_filename = g_strdup ("grl-net-mock-data.ini");
- g_key_file_load_from_file (config,
- env,
- G_KEY_FILE_NONE,
- &error);
- if (error) {
- if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
- GRL_WARNING ("Failed to load default mock file: %s", error->message);
+ GError *error = NULL;
+ config = g_key_file_new ();
- g_error_free (error);
+ g_key_file_load_from_file (config, config_filename, G_KEY_FILE_NONE, &error);
+
+ int version = 0;
+
+ if (error) {
+ GRL_WARNING ("Failed to load mock file \"%s\": %s",
+ config_filename, error->message);
+ g_clear_error (&error);
+ } else {
+ /* Check if we managed to load a file */
+ version = g_key_file_get_integer (config, "default", "version", &error);
- g_key_file_unref (config);
- config = NULL;
+ if (error || version < GRL_MOCK_VERSION) {
+ GRL_WARNING ("Unsupported mock version %d.", version);
+ g_clear_error (&error);
}
}
- if (!config) {
+ if (version < GRL_MOCK_VERSION) {
+ g_key_file_unref (config);
+ config = NULL;
return;
}
- char **parameter_names = g_key_file_get_string_list (config, "default", "ignored-parameters", NULL, &error);
+ char **parameter_names = g_key_file_get_string_list (config, "default",
+ "ignored-parameters",
+ NULL, &error);
if (error) {
parameter_names = NULL;
- g_error_free (error);
+ g_clear_error (&error);
}
+ /* Build regular expressions for ignored query parameters. */
if (parameter_names) {
GString *pattern = g_string_new ("(?:^|\\&)");
@@ -254,17 +262,21 @@ void init_mock_requester (GrlNetWc *self)
ignored_parameters = g_regex_new (pattern->str, G_REGEX_OPTIMIZE, 0, &error);
if (error) {
- GRL_WARNING ("Failed to compile ignored parameters pattern: %s", error->message);
+ GRL_WARNING ("Failed to compile regular expression "
+ "for ignored query parameters: %s", error->message);
g_clear_error (&error);
}
}
- file = g_file_new_for_commandline_arg (env);
- parent = g_file_get_parent (file);
- g_object_unref (file);
+ /* Find base path for mock data. */
+ GFile *file = g_file_new_for_commandline_arg (config_filename);
+ GFile *parent = g_file_get_parent (file);
base_path = g_file_get_path (parent);
+
g_object_unref (parent);
+ g_object_unref (file);
+ g_free (config_filename);
}
void finalize_mock_requester (GrlNetWc *self)
diff --git a/libs/net/grl-net-mock.h b/libs/net/grl-net-mock.h
index cead55d..ae231cc 100644
--- a/libs/net/grl-net-mock.h
+++ b/libs/net/grl-net-mock.h
@@ -31,7 +31,7 @@ G_GNUC_INTERNAL
gboolean is_mocked (void);
G_GNUC_INTERNAL
-gboolean is_unthrottled (void);
+gboolean override_throttling (guint *throttling);
G_GNUC_INTERNAL
void get_url_mocked (GrlNetWc *self,
diff --git a/libs/net/grl-net-wc.c b/libs/net/grl-net-wc.c
index e167895..0ad1b5c 100644
--- a/libs/net/grl-net-wc.c
+++ b/libs/net/grl-net-wc.c
@@ -192,6 +192,8 @@ grl_net_wc_init (GrlNetWc *wc)
set_thread_context (wc);
init_requester (wc);
init_mock_requester (wc);
+
+ override_throttling (&wc->priv->throttling);
}
static void
@@ -325,8 +327,7 @@ get_url (GrlNetWc *self,
g_get_current_time (&now);
- if ((now.tv_sec - priv->last_request.tv_sec) > priv->throttling
- || is_unthrottled ()) {
+ if ((now.tv_sec - priv->last_request.tv_sec) > priv->throttling) {
if (is_mocked ())
get_url_mocked (self, url, headers, result, cancellable);
else
@@ -336,7 +337,7 @@ get_url (GrlNetWc *self,
return;
}
- GRL_DEBUG ("delaying web request %d", is_unthrottled ());
+ GRL_DEBUG ("delaying web request");
/* closure */
c = g_new (struct request_clos, 1);
@@ -582,6 +583,9 @@ grl_net_wc_set_throttling (GrlNetWc *self,
{
g_return_if_fail (GRL_IS_NET_WC (self));
+ if (override_throttling (&throttling))
+ GRL_WARNING ("Overriding throttle setting for mocking");
+
if (throttling > 0) {
/* max conns per host = 1 */
g_object_set (self->priv->session,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]