test_start_stop.c (4898B)
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 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this 31 test into a marked, classifiable test error (TESTING.md, P5). */ 32 #include "mhd_panic_tripwire.h" 33 34 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2 35 #undef MHD_CPU_COUNT 36 #endif 37 #if ! defined(MHD_CPU_COUNT) 38 #define MHD_CPU_COUNT 2 39 #endif 40 41 42 static enum MHD_Result 43 ahc_echo (void *cls, 44 struct MHD_Connection *connection, 45 const char *url, 46 const char *method, 47 const char *version, 48 const char *upload_data, size_t *upload_data_size, 49 void **req_cls) 50 { 51 (void) cls; (void) connection; (void) url; /* Unused. Silent compiler warning. */ 52 (void) method; (void) version; (void) upload_data; /* Unused. Silent compiler warning. */ 53 (void) upload_data_size; (void) req_cls; /* Unused. Silent compiler warning. */ 54 55 return MHD_NO; 56 } 57 58 59 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 60 static unsigned int 61 testInternalGet (unsigned int poll_flag) 62 { 63 struct MHD_Daemon *d; 64 65 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG 66 | poll_flag, 67 0, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); 68 if (d == NULL) 69 return 1; 70 MHD_stop_daemon (d); 71 return 0; 72 } 73 74 75 static unsigned int 76 testMultithreadedGet (unsigned int poll_flag) 77 { 78 struct MHD_Daemon *d; 79 80 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 81 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG 82 | poll_flag, 83 0, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); 84 if (d == NULL) 85 return 2; 86 MHD_stop_daemon (d); 87 return 0; 88 } 89 90 91 static unsigned int 92 testMultithreadedPoolGet (unsigned int poll_flag) 93 { 94 struct MHD_Daemon *d; 95 96 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG 97 | poll_flag, 98 0, NULL, NULL, &ahc_echo, NULL, 99 MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT, 100 MHD_OPTION_END); 101 if (d == NULL) 102 return 4; 103 MHD_stop_daemon (d); 104 return 0; 105 } 106 107 108 static unsigned int 109 testExternalGet (void) 110 { 111 struct MHD_Daemon *d; 112 113 d = MHD_start_daemon (MHD_USE_ERROR_LOG, 114 0, NULL, NULL, 115 &ahc_echo, NULL, 116 MHD_OPTION_END); 117 if (NULL == d) 118 return 8; 119 MHD_stop_daemon (d); 120 return 0; 121 } 122 123 124 #endif 125 126 127 static unsigned int 128 testExternalGetSingleThread (void) 129 { 130 struct MHD_Daemon *d; 131 132 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY, 133 0, NULL, NULL, 134 &ahc_echo, NULL, 135 MHD_OPTION_END); 136 if (NULL == d) 137 return 8; 138 MHD_stop_daemon (d); 139 return 0; 140 } 141 142 143 int 144 main (int argc, 145 char *const *argv) 146 { 147 unsigned int errorCount = 0; 148 (void) argc; 149 (void) argv; /* Unused. Silence compiler warning. */ 150 151 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 152 errorCount += testInternalGet (0); 153 errorCount += testMultithreadedGet (0); 154 errorCount += testMultithreadedPoolGet (0); 155 errorCount += testExternalGet (); 156 #endif 157 errorCount += testExternalGetSingleThread (); 158 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 159 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_POLL)) 160 { 161 errorCount += testInternalGet (MHD_USE_POLL); 162 errorCount += testMultithreadedGet (MHD_USE_POLL); 163 errorCount += testMultithreadedPoolGet (MHD_USE_POLL); 164 } 165 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_EPOLL)) 166 { 167 errorCount += testInternalGet (MHD_USE_EPOLL); 168 errorCount += testMultithreadedPoolGet (MHD_USE_EPOLL); 169 } 170 #endif 171 if (0 != errorCount) 172 fprintf (stderr, 173 "Error (code: %u)\n", 174 errorCount); 175 return (errorCount == 0) ? 0 : 1; /* 0 == pass */ 176 }