Use SSH more safely from configure
[oweals/gnunet.git] / src / util / test_scheduler.c
index fe15e987b01c2914c5de54c5fb03871d3a732fe9..4b6a8a80f939a986895cfedabd63fdce23fa3ef7 100644 (file)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet.
 /*
      This file is part of GNUnet.
-     (C) 2009 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
 
      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 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
 
      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., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 /**
  * @file util/test_scheduler.c
  * @brief tests for the scheduler
  */
 #include "platform.h"
 */
 /**
  * @file util/test_scheduler.c
  * @brief tests for the scheduler
  */
 #include "platform.h"
-#include "gnunet_common.h"
-#include "gnunet_scheduler_lib.h"
-#include "gnunet_time_lib.h"
+#include "gnunet_util_lib.h"
+
+
+struct GNUNET_DISK_PipeHandle *p;
+
+static const struct GNUNET_DISK_FileHandle *fds[2];
 
 
-#define VERBOSE GNUNET_NO
 
 static void
 task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int *ok = cls;
 
 static void
 task2 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int *ok = cls;
+
+  /* t3 should be ready (albeit with lower priority) */
+  GNUNET_assert (1 ==
+                 GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_COUNT));
   GNUNET_assert (2 == *ok);
   (*ok) = 3;
 }
 
   GNUNET_assert (2 == *ok);
   (*ok) = 3;
 }
 
+
 static void
 task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int *ok = cls;
 static void
 task3 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int *ok = cls;
-  /* t4 should be ready (albeit with lower priority) */
-  GNUNET_assert (1 == GNUNET_SCHEDULER_get_load (tc->sched,
-                                                 GNUNET_SCHEDULER_PRIORITY_COUNT));
+
   GNUNET_assert (3 == *ok);
   (*ok) = 4;
 }
 
   GNUNET_assert (3 == *ok);
   (*ok) = 4;
 }
 
-static void
-task4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  int *ok = cls;
-  GNUNET_assert (4 == *ok);
-  (*ok) = 5;
-}
-
-static int fds[2];
-
 
 static void
 taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   static char c;
   int *ok = cls;
 
 static void
 taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   static char c;
   int *ok = cls;
+
   GNUNET_assert (6 == *ok);
   GNUNET_assert (6 == *ok);
-  GNUNET_assert (FD_ISSET (fds[1], tc->write_ready));
+  GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1]));
   (*ok) = 7;
   (*ok) = 7;
-  GNUNET_assert (1 == WRITE (fds[1], &c, 1));
-  GNUNET_break (0 == CLOSE (fds[1]));
+  GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1));
 }
 
 
 }
 
 
@@ -77,56 +72,49 @@ taskNeverRun (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_assert (0);
 }
 
   GNUNET_assert (0);
 }
 
+
 static void
 taskLast (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int *ok = cls;
 static void
 taskLast (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int *ok = cls;
+
   /* t4 should be ready (albeit with lower priority) */
   GNUNET_assert (8 == *ok);
   (*ok) = 0;
 }
 
   /* t4 should be ready (albeit with lower priority) */
   GNUNET_assert (8 == *ok);
   (*ok) = 0;
 }
 
+
 static void
 taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   static char c;
   int *ok = cls;
 static void
 taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   static char c;
   int *ok = cls;
+
   GNUNET_assert (7 == *ok);
   GNUNET_assert (7 == *ok);
-  GNUNET_assert (FD_ISSET (fds[0], tc->read_ready));
-  GNUNET_assert (1 == READ (fds[0], &c, 1));
-  GNUNET_break (0 == CLOSE (fds[0]));
+  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;
   (*ok) = 8;
-  GNUNET_SCHEDULER_add_after (tc->sched,
-                              GNUNET_NO,
-                              GNUNET_SCHEDULER_PRIORITY_UI,
-                              0, &taskNeverRun, NULL);
-  GNUNET_SCHEDULER_add_after (tc->sched,
-                              GNUNET_YES,
-                              GNUNET_SCHEDULER_PRIORITY_IDLE,
-                              0, &taskLast, cls);
-  GNUNET_SCHEDULER_shutdown (tc->sched);
+  GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, &taskLast,
+                                      cls);
+  GNUNET_SCHEDULER_shutdown ();
 }
 
 
 static void
 }
 
 
 static void
-task5 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+task4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int *ok = cls;
 {
   int *ok = cls;
-  GNUNET_assert (5 == *ok);
+
+  GNUNET_assert (4 == *ok);
   (*ok) = 6;
   (*ok) = 6;
-  GNUNET_assert (0 == pipe (fds));
-  GNUNET_SCHEDULER_add_read (tc->sched,
-                             GNUNET_NO,
-                             GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                             GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
-                             GNUNET_TIME_UNIT_FOREVER_REL,
-                             fds[0], &taskRd, cls);
-  GNUNET_SCHEDULER_add_write (tc->sched,
-                              GNUNET_NO,
-                              GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                              GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
-                              GNUNET_TIME_UNIT_FOREVER_REL,
-                              fds[1], &taskWrt, cls);
+  p = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
+  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,
+                                  cls);
+  GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, fds[1],
+                                   &taskWrt, cls);
 }
 
 
 }
 
 
@@ -134,37 +122,16 @@ static void
 task1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int *ok = cls;
 task1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int *ok = cls;
-  GNUNET_SCHEDULER_TaskIdentifier t2;
-  GNUNET_SCHEDULER_TaskIdentifier t3;
-  GNUNET_SCHEDULER_TaskIdentifier t4;
 
   GNUNET_assert (1 == *ok);
   (*ok) = 2;
 
   GNUNET_assert (1 == *ok);
   (*ok) = 2;
-  /* t2 will go first -- prereq for all */
-  t2 = GNUNET_SCHEDULER_add_after (tc->sched,
-                                   GNUNET_NO,
-                                   GNUNET_SCHEDULER_PRIORITY_IDLE,
-                                   GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
-                                   &task2, cls);
-  /* t3 will go before t4: higher priority */
-  t4 = GNUNET_SCHEDULER_add_after (tc->sched,
-                                   GNUNET_NO,
-                                   GNUNET_SCHEDULER_PRIORITY_IDLE,
-                                   t2, &task4, cls);
-  t3 = GNUNET_SCHEDULER_add_delayed (tc->sched,
-                                     GNUNET_NO,
-                                     GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                                     t2,
-                                     GNUNET_TIME_relative_get_zero (),
-                                     &task3, cls);
-  /* t4 will go first: lower prio, but prereq! */
-  GNUNET_SCHEDULER_add_after (tc->sched,
-                              GNUNET_NO,
-                              GNUNET_SCHEDULER_PRIORITY_UI, t4, &task5, cls);
+  GNUNET_SCHEDULER_add_now (&task3, cls);
+  GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI, &task2,
+                                      cls);
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &task4, cls);
 }
 
 
 }
 
 
-
 /**
  * Main method, starts scheduler with task1,
  * checks that "ok" is correct at the end.
 /**
  * Main method, starts scheduler with task1,
  * checks that "ok" is correct at the end.
@@ -180,21 +147,43 @@ check ()
 }
 
 
 }
 
 
+static void
+taskShutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  int *ok = cls;
+
+  GNUNET_assert (1 == *ok);
+  *ok = 8;
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
+  GNUNET_SCHEDULER_shutdown ();
+}
+
+
+/**
+ * Main method, starts scheduler with task1,
+ * checks that "ok" is correct at the end.
+ */
+static int
+checkShutdown ()
+{
+  int ok;
+
+  ok = 1;
+  GNUNET_SCHEDULER_run (&taskShutdown, &ok);
+  return ok;
+}
+
+
+#ifndef MINGW
 static void
 taskSig (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int *ok = cls;
 static void
 taskSig (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int *ok = cls;
+
   GNUNET_assert (1 == *ok);
   *ok = 8;
   GNUNET_assert (1 == *ok);
   *ok = 8;
-  GNUNET_SCHEDULER_add_after (tc->sched,
-                              GNUNET_NO,
-                              GNUNET_SCHEDULER_PRIORITY_UI,
-                              0, &taskNeverRun, NULL);
-  GNUNET_SCHEDULER_add_after (tc->sched,
-                              GNUNET_YES,
-                              GNUNET_SCHEDULER_PRIORITY_UI,
-                              0, &taskLast, cls);
-  GNUNET_break (0 == PLIBC_KILL (getpid (), SIGTERM));
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &taskLast, cls);
+  GNUNET_break (0 == PLIBC_KILL (getpid (), GNUNET_TERM_SIG));
 }
 
 
 }
 
 
@@ -211,8 +200,7 @@ checkSignal ()
   GNUNET_SCHEDULER_run (&taskSig, &ok);
   return ok;
 }
   GNUNET_SCHEDULER_run (&taskSig, &ok);
   return ok;
 }
-
-
+#endif
 
 
 static void
 
 
 static void
@@ -222,12 +210,8 @@ taskCancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   GNUNET_assert (1 == *ok);
   *ok = 0;
 
   GNUNET_assert (1 == *ok);
   *ok = 0;
-  GNUNET_SCHEDULER_cancel (tc->sched,
-                           GNUNET_SCHEDULER_add_after (tc->sched,
-                                                       GNUNET_NO,
-                                                       GNUNET_SCHEDULER_PRIORITY_UI,
-                                                       0,
-                                                       &taskNeverRun, NULL));
+  GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_add_now
+                           (&taskNeverRun, NULL));
 }
 
 
 }
 
 
@@ -246,7 +230,6 @@ checkCancel ()
 }
 
 
 }
 
 
-
 int
 main (int argc, char *argv[])
 {
 int
 main (int argc, char *argv[])
 {
@@ -254,8 +237,12 @@ main (int argc, char *argv[])
 
   GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
   ret += check ();
 
   GNUNET_log_setup ("test_scheduler", "WARNING", NULL);
   ret += check ();
+#ifndef MINGW
   ret += checkSignal ();
   ret += checkSignal ();
+#endif
+  ret += checkShutdown ();
   ret += checkCancel ();
   ret += checkCancel ();
+  GNUNET_DISK_pipe_close (p);
 
   return ret;
 }
 
   return ret;
 }