[Spice-devel] [PATCH spice-server v3 3/6] Add a base test for statistic file
Frediano Ziglio
fziglio at redhat.com
Wed Nov 16 15:39:13 UTC 2016
Create a file and add/remove some nodes.
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
server/tests/Makefile.am | 1 +
server/tests/stat-file-test.c | 104 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 105 insertions(+)
create mode 100644 server/tests/stat-file-test.c
diff --git a/server/tests/Makefile.am b/server/tests/Makefile.am
index 8580a9a..8351253 100644
--- a/server/tests/Makefile.am
+++ b/server/tests/Makefile.am
@@ -43,6 +43,7 @@ check_PROGRAMS = \
test-agent-msg-filter \
test-loop \
test-qxl-parsing \
+ stat-file-test \
$(NULL)
noinst_PROGRAMS = \
diff --git a/server/tests/stat-file-test.c b/server/tests/stat-file-test.c
new file mode 100644
index 0000000..8e0c2f6
--- /dev/null
+++ b/server/tests/stat-file-test.c
@@ -0,0 +1,104 @@
+/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+ Copyright (C) 2016 Red Hat, Inc.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+#include <config.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <glib.h>
+#include <spice.h>
+
+#include "stat-file.h"
+
+/* GLIB_CHECK_VERSION(2, 40, 0) */
+#ifndef g_assert_nonnull
+#define g_assert_nonnull g_assert
+#endif
+#ifndef g_assert_null
+#define g_assert_null(ptr) g_assert((ptr) == NULL)
+#endif
+
+static void stat_file(void)
+{
+ RedStatFile stat_file;
+ StatNodeRef ref, refs[10];
+ uint64_t *counter, *counters[10];
+ int i;
+ char *filename = NULL;
+ char name[20];
+
+ /* create */
+ stat_file_init(&stat_file, 10);
+
+ g_assert_nonnull(stat_file.shm_name);
+ filename = strdup(stat_file.shm_name);
+ g_assert(access(stat_file.shm_name, R_OK));
+
+ /* fill all nodes */
+ for (i = 0; i < 10; ++i) {
+ sprintf(name, "node %d", i);
+ ref = stat_file_add_node(&stat_file, INVALID_STAT_REF, name, TRUE);
+ refs[i] = ref;
+ g_assert_cmpuint(ref,!=,INVALID_STAT_REF);
+ }
+
+ /* should fail */
+ ref = stat_file_add_node(&stat_file, INVALID_STAT_REF, "invalid", TRUE);
+ g_assert_cmpuint(ref,==,INVALID_STAT_REF);
+
+ /* we should find already present nodes */
+ for (i = 0; i < 10; ++i) {
+ /* the formula used here is to take nodes here and there.
+ * As 17 and 10 are coprime numbers you'll get the same numbers
+ * after 10 iterations */
+ sprintf(name, "node %d", (i * 17 + 5) % 10);
+ ref = stat_file_add_node(&stat_file, INVALID_STAT_REF, name, TRUE);
+ g_assert_cmpuint(ref,!=,INVALID_STAT_REF);
+ }
+
+ /* delete some nodes */
+ for (i = 0; i < 6; ++i) {
+ /* see above why the formula is used */
+ int n = (i * 23 + 3) % 10;
+ stat_file_remove_node(&stat_file, refs[n]);
+ refs[n] = INVALID_STAT_REF;
+ }
+
+ /* now there should be some place for some counters */
+ for (i = 0; i < 6; ++i) {
+ sprintf(name, "counter %d", i);
+ counter = stat_file_add_counter(&stat_file, INVALID_STAT_REF, name, TRUE);
+ counters[i] = counter;
+ g_assert_nonnull(counter);
+ }
+ counter = stat_file_add_counter(&stat_file, INVALID_STAT_REF, "invalid", TRUE);
+ g_assert_null(counter);
+
+ stat_file_unlink(&stat_file);
+ g_assert_null(stat_file.shm_name);
+ g_assert_cmpint(access(filename, F_OK),==,-1);
+ free(filename);
+}
+
+int main(int argc, char *argv[])
+{
+ g_test_init(&argc, &argv, NULL);
+
+ g_test_add_func("/server/stat-file", stat_file);
+
+ return g_test_run();
+}
--
2.7.4
More information about the Spice-devel
mailing list