7
7
#include < gtest/gtest.h>
8
8
#include < http/server/simple_sessions.hpp>
9
9
#include < future>
10
+ #include < map>
10
11
11
12
namespace http = network::http;
12
13
namespace net = network;
@@ -20,42 +21,39 @@ TEST(simple_sessions_test, constructor) {
20
21
21
22
TEST (simple_sessions_test, lookup) {
22
23
http::simple_sessions simple_sessions;
23
- http::session & session = simple_sessions.lookup (" sessionid" );
24
+ http::session session = simple_sessions.lookup (" sessionid" );
24
25
(void ) session;
25
26
}
26
27
27
28
TEST (simple_sessions_test, update) {
28
29
http::simple_sessions simple_sessions;
29
30
{
30
- http::session & session = simple_sessions.lookup (" sessionid" );
31
+ http::session session = simple_sessions.lookup (" sessionid" );
31
32
session.set (" user" , " some-user" );
33
+ simple_sessions.update (" sessionid" , std::move (session));
32
34
}
33
35
{
34
- http::session & session = simple_sessions.lookup (" sessionid" );
36
+ http::session session = simple_sessions.lookup (" sessionid" );
35
37
EXPECT_EQ (" some-user" , session[" user" ]);
36
38
}
37
39
}
38
40
39
41
TEST (simple_sessions_test, threaded_sessions) {
40
42
http::simple_sessions simple_sessions;
41
- auto f0 = std::async ([&]() {
42
- http::session &session = simple_sessions.lookup (" " );
43
- return session[" id" ];
44
- });
45
- auto f1 = std::async ([&]() {
46
- http::session &session = simple_sessions.lookup (" " );
47
- return session[" id" ];
48
- });
49
- auto f2 = std::async ([&]() {
50
- http::session &session = simple_sessions.lookup (" " );
51
- return session[" id" ];
52
- });
53
- std::string session0 = f0.get (),
54
- session1 = f1.get (),
55
- session2 = f2.get ();
56
- EXPECT_NE (session0, session1);
57
- EXPECT_NE (session1, session2);
58
- EXPECT_NE (session0, session2);
43
+ // Run many asynchronous functions (hoping that there will be many threads
44
+ // created) so that we may be able to exercise the atomics and the
45
+ // concurrency of the simple session manager.
46
+ std::vector<std::future<std::string>> futures;
47
+ constexpr size_t max_sessions = 1000 ;
48
+ for (size_t counter = 0 ; counter < max_sessions; ++counter) {
49
+ futures.emplace_back (std::async (std::launch::async, [&]() {
50
+ return simple_sessions.lookup (" " )[" id" ];
51
+ }));
52
+ }
53
+ std::set<std::string> ids;
54
+ for (auto &future : futures)
55
+ ids.emplace (future.get ());
56
+ EXPECT_EQ (max_sessions, ids.size ());
59
57
}
60
58
61
59
} // namespace
0 commit comments