summaryrefslogtreecommitdiff
path: root/tests/client.c
blob: 4727e0cda65531dff157ac9c43921a1343d27274 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#define _DEFAULT_SOURCE
#include "../client/dconf-client.h"
#include "../engine/dconf-engine.h"
#include "dconf-mock.h"
#include <string.h>
#include <stdlib.h>

static GThread *main_thread;

static void
test_lifecycle (void)
{
  DConfClient *client;
  GWeakRef weak;

  client = dconf_client_new ();
  g_weak_ref_init (&weak, client);
  g_object_unref (client);

  g_assert (g_weak_ref_get (&weak) == NULL);
  g_weak_ref_clear (&weak);
}

static gboolean changed_was_called;

static void
changed (DConfClient         *client,
         const gchar         *prefix,
         const gchar * const *changes,
         const gchar         *tag,
         gpointer             user_data)
{
  g_assert (g_thread_self () == main_thread);

  changed_was_called = TRUE;
}

static void
check_and_free (GVariant *to_check,
                GVariant *expected)
{
  if (expected)
    {
      g_variant_ref_sink (expected);
      g_assert (to_check);

      g_assert (g_variant_equal (to_check, expected));
      g_variant_unref (to_check);
      g_variant_unref (expected);
    }
  else
    g_assert (to_check == NULL);
}

static void
queue_up_100_writes (DConfClient *client)
{
  gint i;

  /* We send 100 writes, letting them pile up.
   * At no time should there be more than 2 writes on the wire.
   */
  for (i = 0; i < 100; i++)
    {
      changed_was_called = FALSE;
      dconf_client_write_fast (client, "/test/value", g_variant_new_int32 (i), NULL);
      g_assert (changed_was_called);

      /* We should always see the most recently written value. */
      check_and_free (dconf_client_read (client, "/test/value"), g_variant_new_int32 (i));
      check_and_free (dconf_client_read_full (client, "/test/value", DCONF_READ_DEFAULT_VALUE, NULL), NULL);
    }

  g_assert_cmpint (g_queue_get_length (&dconf_mock_dbus_outstanding_call_handles), ==, 2);
}

static void
fail_one_call (void)
{
  DConfEngineCallHandle *handle;
  GError *error;

  error = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_NOENT, "--expected error from testcase--");
  handle = g_queue_pop_head (&dconf_mock_dbus_outstanding_call_handles);
  dconf_engine_call_handle_reply (handle, NULL, error);
  g_error_free (error);
}

static GLogWriterOutput
log_writer_cb (GLogLevelFlags   log_level,
               const GLogField *fields,
               gsize            n_fields,
               gpointer         user_data)
{
  gsize i;

  for (i = 0; i < n_fields; i++)
    {
      if (g_strcmp0 (fields[i].key, "MESSAGE") == 0 &&
          strstr (fields[i].value, "--expected error from testcase--"))
        return G_LOG_WRITER_HANDLED;
    }

  return G_LOG_WRITER_UNHANDLED;
}

static void
test_fast (void)
{
  DConfClient *client;
  gint i;

  g_log_set_writer_func (log_writer_cb, NULL, NULL);

  client = dconf_client_new ();
  g_signal_connect (client, "changed", G_CALLBACK (changed), NULL);

  queue_up_100_writes (client);

  /* Start indicating that the writes failed.
   *
   * For the first failures, we should continue to see the most recently
   * written value (99).
   *
   * After we fail that last one, we should see NULL returned.
   *
   * Each time, we should see a change notify.
   */

  for (i = 0; g_queue_get_length (&dconf_mock_dbus_outstanding_call_handles) > 1; i++)
    {
      changed_was_called = FALSE;
      fail_one_call ();
      g_assert (changed_was_called);

      check_and_free (dconf_client_read (client, "/test/value"), g_variant_new_int32 (99));
      check_and_free (dconf_client_read_full (client, "/test/value", DCONF_READ_DEFAULT_VALUE, NULL), NULL);
    }

  /* Because of the pending-merging logic, we should only have had to
   * fail two calls.
   */
  g_assert (i == 2);

  /* Fail the last call. */
  changed_was_called = FALSE;
  fail_one_call ();
  g_assert (changed_was_called);

  /* Should read back now as NULL */
  check_and_free (dconf_client_read (client, "/test/value"), NULL);
  check_and_free (dconf_client_read_full (client, "/test/value", DCONF_READ_DEFAULT_VALUE, NULL), NULL);

  /* Cleanup */
  g_signal_handlers_disconnect_by_func (client, changed, NULL);
  g_object_unref (client);
}

int
main (int argc, char **argv)
{
  setenv ("DCONF_PROFILE", SRCDIR "/profile/will-never-exist", TRUE);

  main_thread = g_thread_self ();

  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/client/lifecycle", test_lifecycle);
  g_test_add_func ("/client/basic-fast", test_fast);

  return g_test_run ();
}