NAWA 0.9
Web Application Framework for C++
sessiontest.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 Tobias Flaig.
3 *
4 * This file is part of nawa.
5 *
6 * nawa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License,
8 * version 3, as published by the Free Software Foundation.
9 *
10 * nawa is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25#include <nawa/application.h>
26#include <random>
27
28using namespace nawa;
29using namespace std;
30
31int init(AppInit& appInit) {
32
33 return 0;
34}
35
36int 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}
This file will be configured by CMake and contains the necessary properties to ensure that a loaded a...
nawa::Session & session() noexcept
Definition: Connection.cpp:361
std::ostream & responseStream() noexcept
Definition: Connection.cpp:377
std::string start(std::string sessionId, std::optional< unsigned long > keepalive=std::nullopt)
Definition: Session.cpp:127
Definition: AppInit.h:31
int handleRequest(Connection &connection)
Definition: sessiontest.cpp:36
int init(AppInit &appInit)
Definition: sessiontest.cpp:31