avoid failing hard if 'gnunetcheck' db does not exist
[oweals/gnunet.git] / src / util / test_scheduler.c
index a246d75c5947faba08e00ee8e945cf15af2412e1..c38af8a0a71b03d1240db8558dfee19627a4af0e 100644 (file)
@@ -2,20 +2,20 @@
      This file is part of GNUnet.
      Copyright (C) 2009 GNUnet e.V.
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet 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
-     General Public License for more details.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 /**
  * @file util/test_scheduler.c
 #include "gnunet_util_lib.h"
 
 
-struct GNUNET_DISK_PipeHandle *p;
+static struct GNUNET_DISK_PipeHandle *p;
 
 static const struct GNUNET_DISK_FileHandle *fds[2];
 
+static struct GNUNET_SCHEDULER_Task *never_run_task;
+
 
 static void
 task2 (void *cls)
@@ -76,16 +78,36 @@ taskNeverRun (void *cls)
 
 
 static void
-taskLast (void *cls)
+taskLastRd (void *cls)
 {
   int *ok = cls;
 
-  /* t4 should be ready (albeit with lower priority) */
   GNUNET_assert (8 == *ok);
   (*ok) = 0;
 }
 
 
+static void
+taskLastSig (void *cls)
+{
+  int *ok = cls;
+
+  GNUNET_SCHEDULER_cancel (never_run_task);
+  GNUNET_assert (9 == *ok);
+  (*ok) = 0;
+}
+
+
+static void
+taskLastShutdown (void *cls)
+{
+  int *ok = cls;
+
+  GNUNET_assert (10 == *ok);
+  (*ok) = 0;
+}
+
+
 static void
 taskRd (void *cls)
 {
@@ -98,8 +120,8 @@ taskRd (void *cls)
   GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0]));
   GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1));
   (*ok) = 8;
-  GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, &taskLast,
-                                      cls);
+  GNUNET_SCHEDULER_add_shutdown (&taskLastRd,
+                                cls);
   GNUNET_SCHEDULER_shutdown ();
 }
 
@@ -115,10 +137,14 @@ task4 (void *cls)
   GNUNET_assert (NULL != p);
   fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ);
   fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE);
-  GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[0], &taskRd,
+  GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
+                                 fds[0],
+                                 &taskRd,
                                   cls);
-  GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[1],
-                                   &taskWrt, cls);
+  GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
+                                  fds[1],
+                                   &taskWrt,
+                                  cls);
 }
 
 
@@ -130,9 +156,12 @@ task1 (void *cls)
   GNUNET_assert (1 == *ok);
   (*ok) = 2;
   GNUNET_SCHEDULER_add_now (&task3, cls);
-  GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI, &task2,
+  GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
+                                     &task2,
                                       cls);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &task4, cls);
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+                               &task4,
+                               cls);
 }
 
 
@@ -145,6 +174,8 @@ check ()
 {
   int ok;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "[Check scheduling]\n");
   ok = 1;
   GNUNET_SCHEDULER_run (&task1, &ok);
   return ok;
@@ -157,8 +188,8 @@ taskShutdown (void *cls)
   int *ok = cls;
 
   GNUNET_assert (1 == *ok);
-  *ok = 8;
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
+  *ok = 10;
+  GNUNET_SCHEDULER_add_shutdown (&taskLastShutdown, cls);
   GNUNET_SCHEDULER_shutdown ();
 }
 
@@ -172,6 +203,8 @@ checkShutdown ()
 {
   int ok;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "[Check shutdown]\n");
   ok = 1;
   GNUNET_SCHEDULER_run (&taskShutdown, &ok);
   return ok;
@@ -185,9 +218,14 @@ taskSig (void *cls)
   int *ok = cls;
 
   GNUNET_assert (1 == *ok);
-  *ok = 8;
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
-  GNUNET_break (0 == PLIBC_KILL (getpid (), GNUNET_TERM_SIG));
+  *ok = 9;
+  GNUNET_SCHEDULER_add_shutdown (&taskLastSig, cls);
+  never_run_task = 
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
+                                 &taskNeverRun,
+                                 NULL);
+  GNUNET_break (0 == PLIBC_KILL (getpid (),
+                                GNUNET_TERM_SIG));
 }
 
 
@@ -200,6 +238,8 @@ checkSignal ()
 {
   int ok;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "[Check signal handling]\n");
   ok = 1;
   GNUNET_SCHEDULER_run (&taskSig, &ok);
   return ok;
@@ -214,8 +254,7 @@ taskCancel (void *cls)
 
   GNUNET_assert (1 == *ok);
   *ok = 0;
-  GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_now
-                           (&taskNeverRun, NULL));
+  GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_now (&taskNeverRun, NULL));
 }
 
 
@@ -228,6 +267,8 @@ checkCancel ()
 {
   int ok;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "[Check task cancellation]\n");
   ok = 1;
   GNUNET_SCHEDULER_run (&taskCancel, &ok);
   return ok;
@@ -241,11 +282,11 @@ main (int argc, char *argv[])
 
   GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
   ret += check ();
+  ret += checkCancel ();
 #ifndef MINGW
   ret += checkSignal ();
 #endif
   ret += checkShutdown ();
-  ret += checkCancel ();
   GNUNET_DISK_pipe_close (p);
 
   return ret;