libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

test_start_stop.c (4731B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2011 Christian Grothoff
      4 
      5      libmicrohttpd 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 2, or (at your
      8      option) any later version.
      9 
     10      libmicrohttpd 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 libmicrohttpd; see the file COPYING.  If not, write to the
     17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18      Boston, MA 02110-1301, USA.
     19 */
     20 
     21 /**
     22  * @file test_start_stop.c
     23  * @brief  test for #1901 (start+stop)
     24  * @author Christian Grothoff
     25  */
     26 #include "mhd_options.h"
     27 #include "platform.h"
     28 #include <microhttpd.h>
     29 
     30 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2
     31 #undef MHD_CPU_COUNT
     32 #endif
     33 #if ! defined(MHD_CPU_COUNT)
     34 #define MHD_CPU_COUNT 2
     35 #endif
     36 
     37 
     38 static enum MHD_Result
     39 ahc_echo (void *cls,
     40           struct MHD_Connection *connection,
     41           const char *url,
     42           const char *method,
     43           const char *version,
     44           const char *upload_data, size_t *upload_data_size,
     45           void **req_cls)
     46 {
     47   (void) cls; (void) connection; (void) url;         /* Unused. Silent compiler warning. */
     48   (void) method; (void) version; (void) upload_data; /* Unused. Silent compiler warning. */
     49   (void) upload_data_size; (void) req_cls;           /* Unused. Silent compiler warning. */
     50 
     51   return MHD_NO;
     52 }
     53 
     54 
     55 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
     56 static unsigned int
     57 testInternalGet (unsigned int poll_flag)
     58 {
     59   struct MHD_Daemon *d;
     60 
     61   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
     62                         | poll_flag,
     63                         0, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
     64   if (d == NULL)
     65     return 1;
     66   MHD_stop_daemon (d);
     67   return 0;
     68 }
     69 
     70 
     71 static unsigned int
     72 testMultithreadedGet (unsigned int poll_flag)
     73 {
     74   struct MHD_Daemon *d;
     75 
     76   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
     77                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
     78                         | poll_flag,
     79                         0, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
     80   if (d == NULL)
     81     return 2;
     82   MHD_stop_daemon (d);
     83   return 0;
     84 }
     85 
     86 
     87 static unsigned int
     88 testMultithreadedPoolGet (unsigned int poll_flag)
     89 {
     90   struct MHD_Daemon *d;
     91 
     92   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
     93                         | poll_flag,
     94                         0, NULL, NULL, &ahc_echo, NULL,
     95                         MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT,
     96                         MHD_OPTION_END);
     97   if (d == NULL)
     98     return 4;
     99   MHD_stop_daemon (d);
    100   return 0;
    101 }
    102 
    103 
    104 static unsigned int
    105 testExternalGet (void)
    106 {
    107   struct MHD_Daemon *d;
    108 
    109   d = MHD_start_daemon (MHD_USE_ERROR_LOG,
    110                         0, NULL, NULL,
    111                         &ahc_echo, NULL,
    112                         MHD_OPTION_END);
    113   if (NULL == d)
    114     return 8;
    115   MHD_stop_daemon (d);
    116   return 0;
    117 }
    118 
    119 
    120 #endif
    121 
    122 
    123 static unsigned int
    124 testExternalGetSingleThread (void)
    125 {
    126   struct MHD_Daemon *d;
    127 
    128   d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY,
    129                         0, NULL, NULL,
    130                         &ahc_echo, NULL,
    131                         MHD_OPTION_END);
    132   if (NULL == d)
    133     return 8;
    134   MHD_stop_daemon (d);
    135   return 0;
    136 }
    137 
    138 
    139 int
    140 main (int argc,
    141       char *const *argv)
    142 {
    143   unsigned int errorCount = 0;
    144   (void) argc;
    145   (void) argv; /* Unused. Silence compiler warning. */
    146 
    147 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
    148   errorCount += testInternalGet (0);
    149   errorCount += testMultithreadedGet (0);
    150   errorCount += testMultithreadedPoolGet (0);
    151   errorCount += testExternalGet ();
    152 #endif
    153   errorCount += testExternalGetSingleThread ();
    154 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
    155   if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_POLL))
    156   {
    157     errorCount += testInternalGet (MHD_USE_POLL);
    158     errorCount += testMultithreadedGet (MHD_USE_POLL);
    159     errorCount += testMultithreadedPoolGet (MHD_USE_POLL);
    160   }
    161   if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_EPOLL))
    162   {
    163     errorCount += testInternalGet (MHD_USE_EPOLL);
    164     errorCount += testMultithreadedPoolGet (MHD_USE_EPOLL);
    165   }
    166 #endif
    167   if (0 != errorCount)
    168     fprintf (stderr,
    169              "Error (code: %u)\n",
    170              errorCount);
    171   return (errorCount == 0) ? 0 : 1;       /* 0 == pass */
    172 }