-remove trailing whitespace
[oweals/gnunet.git] / src / consensus / test_consensus_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file consensus/test_consensus_api.c
23  * @brief testcase for consensus_api.c
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_consensus_service.h"
28 #include "gnunet_testing_lib.h"
29
30
31 static struct GNUNET_CONSENSUS_Handle *consensus;
32
33 static struct GNUNET_HashCode session_id;
34
35 static unsigned int elements_received;
36
37
38 static void
39 conclude_done (void *cls)
40 {
41   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "conclude over\n");
42   if (2 != elements_received)
43     GNUNET_abort ();
44   GNUNET_SCHEDULER_shutdown ();
45 }
46
47 static void
48 on_new_element (void *cls,
49                 const struct GNUNET_SET_Element *element)
50 {
51   elements_received++;
52 }
53
54 static void
55 insert_done (void *cls, int success)
56 {
57   /* make sure cb is only called once */
58   static int called = GNUNET_NO;
59   GNUNET_assert (GNUNET_NO == called);
60   called = GNUNET_YES;
61   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "insert done\n");
62   GNUNET_CONSENSUS_conclude (consensus, GNUNET_TIME_UNIT_SECONDS, &conclude_done, NULL);
63 }
64
65
66 /**
67  * Signature of the main function of a task.
68  *
69  * @param cls closure
70  * @param tc context information (why was this task triggered now)
71  */
72 static void
73 on_shutdown (void *cls,
74           const struct GNUNET_SCHEDULER_TaskContext *tc)
75 {
76   if (NULL != consensus)
77   {
78     GNUNET_CONSENSUS_destroy (consensus);
79     consensus = NULL;
80   }
81 }
82
83
84 static void
85 run (void *cls,
86      const struct GNUNET_CONFIGURATION_Handle *cfg,
87      struct GNUNET_TESTING_Peer *peer)
88 {
89   char *str = "foo";
90
91   struct GNUNET_SET_Element el1 = {4, 0, "foo"};
92   struct GNUNET_SET_Element el2 = {5, 0, "quux"};
93
94   GNUNET_log_setup ("test_consensus_api",
95                     "INFO",
96                     NULL);
97
98   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "testing consensus api\n");
99
100   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &on_shutdown, NULL);
101
102   GNUNET_CRYPTO_hash (str, strlen (str), &session_id);
103   consensus = GNUNET_CONSENSUS_create (cfg, 0, NULL, &session_id, on_new_element, &consensus);
104   GNUNET_assert (consensus != NULL);
105
106   GNUNET_CONSENSUS_insert (consensus, &el1, NULL, &consensus);
107   GNUNET_CONSENSUS_insert (consensus, &el2, &insert_done, &consensus);
108 }
109
110
111 int
112 main (int argc, char **argv)
113 {
114   int ret;
115
116   ret = GNUNET_TESTING_peer_run ("test_consensus_api",
117                                  "test_consensus.conf",
118                                  &run, NULL);
119   return ret;
120 }
121