annotate OrthancFramework/UnitTestsSources/MemoryCacheTests.cpp @ 5853:4d932683049d get-scu tip

very first implementation of C-Get SCU
author Alain Mazy <am@orthanc.team>
date Tue, 29 Oct 2024 17:25:49 +0100
parents f7adfb22e20e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
827
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
1 /**
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1654
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
4 * Department, University Hospital of Liege, Belgium
5640
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5420
diff changeset
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
827
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
8 *
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
9 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
10 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
11 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
12 * the License, or (at your option) any later version.
827
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
13 *
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
14 * This program is distributed in the hope that it will be useful, but
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
17 * Lesser General Public License for more details.
827
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
18 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
19 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
20 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
21 * <http://www.gnu.org/licenses/>.
827
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
22 **/
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
23
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
24
4059
e241e5f3f088 moved LuaTests and MemoryCacheTests from OrthancServer to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
25 #if ORTHANC_UNIT_TESTS_LINK_FRAMEWORK == 1
4063
e00f3d089991 shared library of orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4062
diff changeset
26 // Must be the first to be sure to use the Orthanc framework shared library
4059
e241e5f3f088 moved LuaTests and MemoryCacheTests from OrthancServer to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
27 # include <OrthancFramework.h>
e241e5f3f088 moved LuaTests and MemoryCacheTests from OrthancServer to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
28 #endif
e241e5f3f088 moved LuaTests and MemoryCacheTests from OrthancServer to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
29
4062
0953b3dc3261 pretty-printing
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4059
diff changeset
30 #include <gtest/gtest.h>
1486
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1367
diff changeset
31
4059
e241e5f3f088 moved LuaTests and MemoryCacheTests from OrthancServer to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
32 #include "../Sources/Cache/MemoryCache.h"
e241e5f3f088 moved LuaTests and MemoryCacheTests from OrthancServer to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
33 #include "../Sources/Cache/MemoryStringCache.h"
e241e5f3f088 moved LuaTests and MemoryCacheTests from OrthancServer to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
34 #include "../Sources/Cache/SharedArchive.h"
e241e5f3f088 moved LuaTests and MemoryCacheTests from OrthancServer to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
35 #include "../Sources/IDynamicObject.h"
e241e5f3f088 moved LuaTests and MemoryCacheTests from OrthancServer to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
36 #include "../Sources/Logging.h"
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
37 #include "../Sources/SystemToolbox.h"
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
4062
0953b3dc3261 pretty-printing
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4059
diff changeset
39 #include <memory>
0953b3dc3261 pretty-printing
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4059
diff changeset
40 #include <algorithm>
0953b3dc3261 pretty-printing
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4059
diff changeset
41 #include <boost/thread.hpp>
0953b3dc3261 pretty-printing
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4059
diff changeset
42 #include <boost/lexical_cast.hpp>
0953b3dc3261 pretty-printing
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4059
diff changeset
43
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 285
diff changeset
45 TEST(LRU, Basic)
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 285
diff changeset
47 Orthanc::LeastRecentlyUsedIndex<std::string> r;
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 r.Add("d");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 r.Add("a");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 r.Add("c");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 r.Add("b");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
54 r.MakeMostRecent("a");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
55 r.MakeMostRecent("d");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
56 r.MakeMostRecent("b");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
57 r.MakeMostRecent("c");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
58 r.MakeMostRecent("d");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
59 r.MakeMostRecent("c");
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
61 ASSERT_EQ("a", r.GetOldest());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 ASSERT_EQ("a", r.RemoveOldest());
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
63 ASSERT_EQ("b", r.GetOldest());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 ASSERT_EQ("b", r.RemoveOldest());
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
65 ASSERT_EQ("d", r.GetOldest());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 ASSERT_EQ("d", r.RemoveOldest());
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
67 ASSERT_EQ("c", r.GetOldest());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 ASSERT_EQ("c", r.RemoveOldest());
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 ASSERT_TRUE(r.IsEmpty());
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
71
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
72 ASSERT_THROW(r.GetOldest(), Orthanc::OrthancException);
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
73 ASSERT_THROW(r.RemoveOldest(), Orthanc::OrthancException);
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 285
diff changeset
77 TEST(LRU, Payload)
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 {
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 285
diff changeset
79 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 r.Add("a", 420);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 r.Add("b", 421);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 r.Add("c", 422);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 r.Add("d", 423);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
86 r.MakeMostRecent("a");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
87 r.MakeMostRecent("d");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
88 r.MakeMostRecent("b");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
89 r.MakeMostRecent("c");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
90 r.MakeMostRecent("d");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
91 r.MakeMostRecent("c");
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 ASSERT_TRUE(r.Contains("b"));
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 ASSERT_EQ(421, r.Invalidate("b"));
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 ASSERT_FALSE(r.Contains("b"));
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 int p;
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
98 ASSERT_TRUE(r.Contains("a", p)); ASSERT_EQ(420, p);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
99 ASSERT_TRUE(r.Contains("c", p)); ASSERT_EQ(422, p);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
100 ASSERT_TRUE(r.Contains("d", p)); ASSERT_EQ(423, p);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
101
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
102 ASSERT_EQ("a", r.GetOldest());
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
103 ASSERT_EQ(420, r.GetOldestPayload());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 ASSERT_EQ("a", r.RemoveOldest(p)); ASSERT_EQ(420, p);
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
105
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
106 ASSERT_EQ("d", r.GetOldest());
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
107 ASSERT_EQ(423, r.GetOldestPayload());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 ASSERT_EQ("d", r.RemoveOldest(p)); ASSERT_EQ(423, p);
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
109
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
110 ASSERT_EQ("c", r.GetOldest());
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
111 ASSERT_EQ(422, r.GetOldestPayload());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 ASSERT_EQ("c", r.RemoveOldest(p)); ASSERT_EQ(422, p);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 ASSERT_TRUE(r.IsEmpty());
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
118 TEST(LRU, PayloadUpdate)
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
119 {
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
120 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
121
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
122 r.Add("a", 420);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
123 r.Add("b", 421);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
124 r.Add("d", 423);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
125
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
126 r.MakeMostRecent("a", 424);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
127 r.MakeMostRecent("d", 421);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
128
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
129 ASSERT_EQ("b", r.GetOldest());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
130 ASSERT_EQ(421, r.GetOldestPayload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
131 r.RemoveOldest();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
132
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
133 ASSERT_EQ("a", r.GetOldest());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
134 ASSERT_EQ(424, r.GetOldestPayload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
135 r.RemoveOldest();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
136
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
137 ASSERT_EQ("d", r.GetOldest());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
138 ASSERT_EQ(421, r.GetOldestPayload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
139 r.RemoveOldest();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
140
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
141 ASSERT_TRUE(r.IsEmpty());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
142 }
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
143
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
144
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
145
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
146 TEST(LRU, PayloadUpdateBis)
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
147 {
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
148 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
149
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
150 r.AddOrMakeMostRecent("a", 420);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
151 r.AddOrMakeMostRecent("b", 421);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
152 r.AddOrMakeMostRecent("d", 423);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
153 r.AddOrMakeMostRecent("a", 424);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
154 r.AddOrMakeMostRecent("d", 421);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
155
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
156 ASSERT_EQ("b", r.GetOldest());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
157 ASSERT_EQ(421, r.GetOldestPayload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
158 r.RemoveOldest();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
159
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
160 ASSERT_EQ("a", r.GetOldest());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
161 ASSERT_EQ(424, r.GetOldestPayload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
162 r.RemoveOldest();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
163
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
164 ASSERT_EQ("d", r.GetOldest());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
165 ASSERT_EQ(421, r.GetOldestPayload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
166 r.RemoveOldest();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
167
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
168 ASSERT_TRUE(r.IsEmpty());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
169 }
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
170
3454
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
171 TEST(LRU, GetAllKeys)
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
172 {
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
173 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
174 std::vector<std::string> keys;
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
175
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
176 r.AddOrMakeMostRecent("a", 420);
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
177 r.GetAllKeys(keys);
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
178
3497
db71bd11affc fix unit tests if dcmtk != 3.6.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3454
diff changeset
179 ASSERT_EQ(1u, keys.size());
3454
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
180 ASSERT_EQ("a", keys[0]);
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
181
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
182 r.AddOrMakeMostRecent("b", 421);
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
183 r.GetAllKeys(keys);
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
184
3497
db71bd11affc fix unit tests if dcmtk != 3.6.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3454
diff changeset
185 ASSERT_EQ(2u, keys.size());
3454
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
186 ASSERT_TRUE(std::find(keys.begin(), keys.end(),"a") != keys.end());
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
187 ASSERT_TRUE(std::find(keys.begin(), keys.end(),"b") != keys.end());
0e3c26e4b8ec LeastRecentlyUsedIndex::GetAllKeys (test)
Alain Mazy <alain@mazy.be>
parents: 3060
diff changeset
188 }
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
189
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
190
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
191
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
192 namespace
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
193 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
194 class Integer : public Orthanc::IDynamicObject
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
195 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
196 private:
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
197 std::string& log_;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
198 int value_;
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 public:
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
201 Integer(std::string& log, int v) : log_(log), value_(v)
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
202 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
203 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
204
2924
22524fd06225 macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
205 virtual ~Integer() ORTHANC_OVERRIDE
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
206 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
207 LOG(INFO) << "Removing cache entry for " << value_;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
208 log_ += boost::lexical_cast<std::string>(value_) + " ";
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
209 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
210 };
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
211
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
212 class IntegerProvider : public Orthanc::Deprecated::ICachePageProvider
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
213 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
214 public:
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
215 std::string log_;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
216
2924
22524fd06225 macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
217 virtual Orthanc::IDynamicObject* Provide(const std::string& s) ORTHANC_OVERRIDE
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
218 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
219 LOG(INFO) << "Providing " << s;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
220 return new Integer(log_, boost::lexical_cast<int>(s));
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
221 }
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 };
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 }
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
224
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
225
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
226 TEST(MemoryCache, Basic)
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
227 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
228 IntegerProvider provider;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
229
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
230 {
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
231 Orthanc::Deprecated::MemoryCache cache(provider, 3);
285
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
232 cache.Access("42"); // 42 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
233 cache.Access("43"); // 43, 42 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
234 cache.Access("45"); // 45, 43, 42 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
235 cache.Access("42"); // 42, 45, 43 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
236 cache.Access("43"); // 43, 42, 45 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
237 cache.Access("47"); // 45 is removed; 47, 43, 42 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
238 cache.Access("44"); // 42 is removed; 44, 47, 43 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
239 cache.Access("42"); // 43 is removed; 42, 44, 47 -> exit
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
240 // Closing the cache: 47, 44, 42 are successively removed
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
241 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
242
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
243 ASSERT_EQ("45 42 43 47 44 42 ", provider.log_);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
244 }
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
245
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
246
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
247
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
248
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
249
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
250 namespace
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
251 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
252 class S : public Orthanc::IDynamicObject
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
253 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
254 private:
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
255 std::string value_;
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
256
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
257 public:
4204
318c16cfccab cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
258 explicit S(const std::string& value) : value_(value)
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
259 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
260 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
261
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
262 const std::string& GetValue() const
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
263 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
264 return value_;
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
265 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
266 };
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
267 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
268
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
269
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
270 TEST(LRU, SharedArchive)
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
271 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
272 std::string first, second;
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
273 Orthanc::SharedArchive a(3);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
274 first = a.Add(new S("First item"));
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
275 second = a.Add(new S("Second item"));
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
276
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
277 for (int i = 1; i < 100; i++)
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
278 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
279 a.Add(new S("Item " + boost::lexical_cast<std::string>(i)));
2976
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
280
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
281 // Continuously protect the two first items
2976
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
282 {
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
283 Orthanc::SharedArchive::Accessor accessor(a, first);
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
284 ASSERT_TRUE(accessor.IsValid());
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
285 ASSERT_EQ("First item", dynamic_cast<S&>(accessor.GetItem()).GetValue());
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
286 }
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
287
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
288 {
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
289 Orthanc::SharedArchive::Accessor accessor(a, second);
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
290 ASSERT_TRUE(accessor.IsValid());
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
291 ASSERT_EQ("Second item", dynamic_cast<S&>(accessor.GetItem()).GetValue());
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
292 }
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
293
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
294 {
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
295 Orthanc::SharedArchive::Accessor accessor(a, "nope");
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
296 ASSERT_FALSE(accessor.IsValid());
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
297 ASSERT_THROW(accessor.GetItem(), Orthanc::OrthancException);
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2924
diff changeset
298 }
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
299 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
300
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
301 std::list<std::string> i;
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
302 a.List(i);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
303
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
304 size_t count = 0;
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
305 for (std::list<std::string>::const_iterator
4204
318c16cfccab cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
306 it = i.begin(); it != i.end(); ++it)
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
307 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
308 if (*it == first ||
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
309 *it == second)
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
310 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
311 count++;
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
312 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
313 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
314
1492
9f66a12eb8fc fix warnings for gcc 5
jodogne
parents: 1486
diff changeset
315 ASSERT_EQ(2u, count);
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
316 }
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
317
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
318
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
319 TEST(MemoryStringCache, Basic)
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
320 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
321 Orthanc::MemoryStringCache c;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
322 ASSERT_THROW(c.SetMaximumSize(0), Orthanc::OrthancException);
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
323
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
324 c.SetMaximumSize(3);
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
325
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
326 std::string v;
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
327 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
328 Orthanc::MemoryStringCache::Accessor a(c);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
329 ASSERT_FALSE(a.Fetch(v, "key1"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
330 }
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
331
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
332 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
333 Orthanc::MemoryStringCache::Accessor a(c);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
334 ASSERT_FALSE(a.Fetch(v, "key1"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
335 a.Add("key1", "a");
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
336 ASSERT_TRUE(a.Fetch(v, "key1"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
337 ASSERT_EQ("a", v);
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
338
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
339 ASSERT_FALSE(a.Fetch(v, "key2"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
340 ASSERT_FALSE(a.Fetch(v, "key3"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
341
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
342 a.Add("key2", "b");
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
343 ASSERT_TRUE(a.Fetch(v, "key1"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
344 ASSERT_EQ("a", v);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
345 ASSERT_TRUE(a.Fetch(v, "key2"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
346 ASSERT_EQ("b", v);
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
347
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
348 a.Add("key3", "too-large-value");
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
349 ASSERT_TRUE(a.Fetch(v, "key1"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
350 ASSERT_EQ("a", v);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
351 ASSERT_TRUE(a.Fetch(v, "key2"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
352 ASSERT_EQ("b", v);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
353 ASSERT_FALSE(a.Fetch(v, "key3"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
354
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
355 a.Add("key3", "c");
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
356 ASSERT_TRUE(a.Fetch(v, "key2"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
357 ASSERT_EQ("b", v);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
358 ASSERT_TRUE(a.Fetch(v, "key1"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
359 ASSERT_EQ("a", v);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
360 ASSERT_TRUE(a.Fetch(v, "key3"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
361 ASSERT_EQ("c", v);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
362
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
363 // adding a fourth value should remove the oldest accessed value (key2)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
364 a.Add("key4", "d");
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
365 ASSERT_FALSE(a.Fetch(v, "key2"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
366 ASSERT_TRUE(a.Fetch(v, "key1"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
367 ASSERT_EQ("a", v);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
368 ASSERT_TRUE(a.Fetch(v, "key3"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
369 ASSERT_EQ("c", v);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
370 ASSERT_TRUE(a.Fetch(v, "key4"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
371 ASSERT_EQ("d", v);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
372
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
373 }
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
374 }
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
375
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
376 TEST(MemoryStringCache, Invalidate)
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
377 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
378 Orthanc::MemoryStringCache c;
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
379 Orthanc::MemoryStringCache::Accessor a(c);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
380
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
381 a.Add("hello", "a");
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
382 a.Add("hello2", "b");
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
383
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
384 std::string v;
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
385 ASSERT_TRUE(a.Fetch(v, "hello"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
386 ASSERT_EQ("a", v);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
387 ASSERT_TRUE(a.Fetch(v, "hello2"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
388 ASSERT_EQ("b", v);
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
389
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
390 c.Invalidate("hello");
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
391 ASSERT_FALSE(a.Fetch(v, "hello"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
392 ASSERT_TRUE(a.Fetch(v, "hello2"));
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
393 ASSERT_EQ("b", v);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
394 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
395
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
396
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
397 static int ThreadingScenarioHappyStep = 0;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
398 static Orthanc::MemoryStringCache ThreadingScenarioHappyCache;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
399
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
400 void ThreadingScenarioHappyThread1()
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
401 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
402 // the first thread to call Fetch (will be in charge of adding)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
403 Orthanc::MemoryStringCache::Accessor a(ThreadingScenarioHappyCache);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
404 std::string v;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
405
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
406 LOG(INFO) << "Thread1 will fetch";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
407 if (!a.Fetch(v, "key1"))
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
408 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
409 LOG(INFO) << "Thread1 has fetch";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
410 ThreadingScenarioHappyStep = 1;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
411
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
412 // wait for the other thread to fetch too
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
413 while (ThreadingScenarioHappyStep < 2)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
414 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
415 Orthanc::SystemToolbox::USleep(10000);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
416 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
417 LOG(INFO) << "Thread1 will add after a short sleep";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
418 Orthanc::SystemToolbox::USleep(100000);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
419 LOG(INFO) << "Thread1 will add";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
421 a.Add("key1", "value1");
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
422
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
423 LOG(INFO) << "Thread1 has added";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
424 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
425 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
426
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
427 void ThreadingScenarioHappyThread2()
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
428 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
429 Orthanc::MemoryStringCache::Accessor a(ThreadingScenarioHappyCache);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
430 std::string v;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
431
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
432 // nobody has added key2 -> go
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
433 if (!a.Fetch(v, "key2"))
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
434 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
435 a.Add("key2", "value2");
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
436 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
437
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
438 // wait until thread 1 has completed its "Fetch" but not added yet
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
439 while (ThreadingScenarioHappyStep < 1)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
440 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
441 Orthanc::SystemToolbox::USleep(10000);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
442 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
443
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
444 ThreadingScenarioHappyStep = 2;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
445 LOG(INFO) << "Thread2 will fetch";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
446 // this should wait until thread 1 has added
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
447 if (!a.Fetch(v, "key1"))
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
448 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
449 ASSERT_FALSE(true); // this thread should not add since thread1 should have done it
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
450 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
451 LOG(INFO) << "Thread2 has fetched the value";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
452 ASSERT_EQ("value1", v);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
453 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
454
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
455
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
456 TEST(MemoryStringCache, ThreadingScenarioHappy)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
457 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
458 boost::thread thread1 = boost::thread(ThreadingScenarioHappyThread1);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
459 boost::thread thread2 = boost::thread(ThreadingScenarioHappyThread2);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
460
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
461 thread1.join();
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
462 thread2.join();
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3497
diff changeset
463 }
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
464
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
465
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
466 static int ThreadingScenarioFailureStep = 0;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
467 static Orthanc::MemoryStringCache ThreadingScenarioFailureCache;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
468
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
469 void ThreadingScenarioFailureThread1()
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
470 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
471 // the first thread to call Fetch (will be in charge of adding)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
472 Orthanc::MemoryStringCache::Accessor a(ThreadingScenarioFailureCache);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
473 std::string v;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
474
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
475 LOG(INFO) << "Thread1 will fetch";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
476 if (!a.Fetch(v, "key1"))
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
477 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
478 LOG(INFO) << "Thread1 has fetch";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
479 ThreadingScenarioFailureStep = 1;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
480
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
481 // wait for the other thread to fetch too
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
482 while (ThreadingScenarioFailureStep < 2)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
483 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
484 Orthanc::SystemToolbox::USleep(10000);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
485 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
486 LOG(INFO) << "Thread1 will add after a short sleep";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
487 Orthanc::SystemToolbox::USleep(100000);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
488 LOG(INFO) << "Thread1 fails to add because of an error";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
489 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
490 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
491
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
492 void ThreadingScenarioFailureThread2()
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
493 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
494 Orthanc::MemoryStringCache::Accessor a(ThreadingScenarioFailureCache);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
495 std::string v;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
496
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
497 // wait until thread 1 has completed its "Fetch" but not added yet
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
498 while (ThreadingScenarioFailureStep < 1)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
499 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
500 Orthanc::SystemToolbox::USleep(10000);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
501 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
502
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
503 ThreadingScenarioFailureStep = 2;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
504 LOG(INFO) << "Thread2 will fetch and wait for thread1 to add";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
505 // this should wait until thread 1 has added
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
506 if (!a.Fetch(v, "key1"))
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
507 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
508 LOG(INFO) << "Thread2 has been awaken and will add since Thread1 has failed to add";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
509 a.Add("key1", "value1");
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
510 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
511 LOG(INFO) << "Thread2 has added the value";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
512 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
513
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
514
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
515 TEST(MemoryStringCache, ThreadingScenarioFailure)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
516 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
517 boost::thread thread1 = boost::thread(ThreadingScenarioFailureThread1);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
518 boost::thread thread2 = boost::thread(ThreadingScenarioFailureThread2);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
519
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
520 thread1.join();
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
521 thread2.join();
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
522 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
523
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
524
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
525 static int ThreadingScenarioInvalidateStep = 0;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
526 static Orthanc::MemoryStringCache ThreadingScenarioInvalidateCache;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
527
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
528 void ThreadingScenarioInvalidateThread1()
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
529 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
530 // the first thread to call Fetch (will be in charge of adding)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
531 Orthanc::MemoryStringCache::Accessor a(ThreadingScenarioInvalidateCache);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
532 std::string v;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
533
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
534 LOG(INFO) << "Thread1 will fetch";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
535 if (!a.Fetch(v, "key1"))
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
536 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
537 LOG(INFO) << "Thread1 has fetch";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
538 ThreadingScenarioInvalidateStep = 1;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
539
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
540 // wait for the other thread to fetch too
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
541 while (ThreadingScenarioInvalidateStep < 2)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
542 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
543 Orthanc::SystemToolbox::USleep(10000);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
544 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
545 LOG(INFO) << "Thread1 will invalidate after a short sleep";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
546 Orthanc::SystemToolbox::USleep(100000);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
547 LOG(INFO) << "Thread1 is invalidating";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
548 ThreadingScenarioInvalidateCache.Invalidate("key1");
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
549 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
550 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
551
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
552 void ThreadingScenarioInvalidateThread2()
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
553 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
554 Orthanc::MemoryStringCache::Accessor a(ThreadingScenarioInvalidateCache);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
555 std::string v;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
556
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
557 // wait until thread 1 has completed its "Fetch" but not added yet
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
558 while (ThreadingScenarioInvalidateStep < 1)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
559 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
560 Orthanc::SystemToolbox::USleep(10000);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
561 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
562
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
563 ThreadingScenarioInvalidateStep = 2;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
564 LOG(INFO) << "Thread2 will fetch and wait for thread1 to add";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
565 // this should wait until thread 1 has added
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
566 if (!a.Fetch(v, "key1"))
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
567 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
568 LOG(INFO) << "Thread2 has been awaken because thread1 has invalidated the key";
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
569 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
570 }
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
571
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
572
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
573 TEST(MemoryStringCache, ThreadingScenarioInvalidate)
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
574 {
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
575 boost::thread thread1 = boost::thread(ThreadingScenarioInvalidateThread1);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
576 boost::thread thread2 = boost::thread(ThreadingScenarioInvalidateThread2);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
577
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
578 thread1.join();
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
579 thread2.join();
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
580 }