NAWA  0.8
Web Application Framework for C++
sessiontest.cpp
Go to the documentation of this file.
1 
7 /*
8  * Copyright (C) 2019-2021 Tobias Flaig.
9  *
10  * This file is part of nawa.
11  *
12  * nawa is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License,
14  * version 3, as published by the Free Software Foundation.
15  *
16  * nawa is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with nawa. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
25 #include <nawa/application.h>
26 #include <random>
27 
28 using namespace nawa;
29 using namespace std;
30 
31 int init(AppInit& appInit) {
32 
33  return 0;
34 }
35 
36 int handleRequest(Connection& connection) {
37  // run this a lot of times in parallel (flood requests) to test multi-threading
38  // for example with curl: $ curl -b SESSION=... http://t1.local/test?it=[1-100]
39  // see sessiontest.sh for an example script
40  random_device rd;
41  unsigned int cm[1000];
42 
43  auto& session = connection.session();
44 
45  session.start();
46 
47  // set session variables
48  for (int i = 0; i < 1000; ++i) {
49  // fill with a random uint from urandom
50  cm[i] = rd();
51  session.set("sessiontest" + to_string(i), cm[i]);
52  }
53 
54  // set some other session variables in an alternating fashion
55  bool desc = false;
56  if (session.isSet("descending") && any_cast<bool>(session["descending"])) {
57  desc = true;
58  session.set("descending", false);
59  } else {
60  session.set("descending", true);
61  }
62  for (int i = 0; i < 1000; ++i) {
63  int val = desc ? 1000 - i : i;
64  session.set("sessioncount" + to_string(i), val);
65  }
66 
67  // get sessiontest variables and compare - this should actually fail now and then in a multithreading env
68  // so matchcount does not necessarily have to be 1000
69  int matchcount = 0;
70  for (int i = 0; i < 1000; ++i) {
71  if (any_cast<unsigned int>(session["sessiontest" + to_string(i)]) == cm[i]) {
72  ++matchcount;
73  }
74  }
75  connection.responseStream() << "Match count for sessiontest vars: " << matchcount << "\n";
76 
77  // consistency check for the sessioncount variables - failcount should definitely be zero!
78  int failcount = 0;
79  for (int i = 0; i < 1000; ++i) {
80  auto val = any_cast<int>(session["sessioncount" + to_string(i)]);
81  if (val != i && val != 1000 - i) {
82  ++failcount;
83  }
84  }
85  connection.responseStream() << "Fail count for sessioncount vars: " << failcount << "\n";
86 
87  return 0;
88 }
nawa::Session & session() noexcept
Definition: Connection.cpp:361
std::ostream & responseStream() noexcept
Definition: Connection.cpp:377
void start(nawa::Cookie properties=Cookie())
Definition: Session.cpp:127
Definition: AppInit.h:31
int handleRequest(Connection &connection)
Definition: sessiontest.cpp:36
int init(AppInit &appInit)
Definition: sessiontest.cpp:31