Mercurial > hg > orthanc
annotate UnitTestsSources/MemoryCacheTests.cpp @ 2778:8d8d2dbeef11
back to mainline
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 17 Jul 2018 14:06:32 +0200 |
parents | 878b59270859 |
children | 22524fd06225 |
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 | 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 |
2447
878b59270859
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
6 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
8 * modify it under the terms of the GNU General Public License as |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
9 * published by the Free Software Foundation, either version 3 of the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
10 * License, or (at your option) any later version. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
11 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
12 * In addition, as a special exception, the copyright holders of this |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
13 * program give permission to link the code of its release with the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
15 * that use the same license as the "OpenSSL" library), and distribute |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
16 * the linked executables. You must obey the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
17 * in all respects for all of the code used other than "OpenSSL". If you |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
18 * modify file(s) with this exception, you may extend this exception to |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
19 * your version of the file(s), but you are not obligated to do so. If |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
20 * you do not wish to do so, delete this exception statement from your |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
21 * version. If you delete this exception statement from all source files |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
22 * in the program, then also delete it here. |
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 * 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
|
25 * 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
|
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
27 * General Public License for more details. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
28 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
29 * You should have received a copy of the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
31 **/ |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
32 |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
33 |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
827
diff
changeset
|
34 #include "PrecompiledHeadersUnitTests.h" |
282 | 35 #include "gtest/gtest.h" |
36 | |
37 #include <memory> | |
283 | 38 #include <boost/thread.hpp> |
39 #include <boost/lexical_cast.hpp> | |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1367
diff
changeset
|
40 |
284
06aa7b7b6723
implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
41 #include "../Core/Cache/MemoryCache.h" |
1367 | 42 #include "../Core/Cache/SharedArchive.h" |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1367
diff
changeset
|
43 #include "../Core/IDynamicObject.h" |
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1367
diff
changeset
|
44 #include "../Core/Logging.h" |
282 | 45 |
46 | |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
285
diff
changeset
|
47 TEST(LRU, Basic) |
282 | 48 { |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
285
diff
changeset
|
49 Orthanc::LeastRecentlyUsedIndex<std::string> r; |
282 | 50 |
51 r.Add("d"); | |
52 r.Add("a"); | |
53 r.Add("c"); | |
54 r.Add("b"); | |
55 | |
509
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
56 r.MakeMostRecent("a"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
57 r.MakeMostRecent("d"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
58 r.MakeMostRecent("b"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
59 r.MakeMostRecent("c"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
60 r.MakeMostRecent("d"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
61 r.MakeMostRecent("c"); |
282 | 62 |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
63 ASSERT_EQ("a", r.GetOldest()); |
282 | 64 ASSERT_EQ("a", r.RemoveOldest()); |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
65 ASSERT_EQ("b", r.GetOldest()); |
282 | 66 ASSERT_EQ("b", r.RemoveOldest()); |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
67 ASSERT_EQ("d", r.GetOldest()); |
282 | 68 ASSERT_EQ("d", r.RemoveOldest()); |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
69 ASSERT_EQ("c", r.GetOldest()); |
282 | 70 ASSERT_EQ("c", r.RemoveOldest()); |
71 | |
72 ASSERT_TRUE(r.IsEmpty()); | |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
73 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
74 ASSERT_THROW(r.GetOldest(), Orthanc::OrthancException); |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
75 ASSERT_THROW(r.RemoveOldest(), Orthanc::OrthancException); |
282 | 76 } |
77 | |
78 | |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
285
diff
changeset
|
79 TEST(LRU, Payload) |
282 | 80 { |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
285
diff
changeset
|
81 Orthanc::LeastRecentlyUsedIndex<std::string, int> r; |
282 | 82 |
83 r.Add("a", 420); | |
84 r.Add("b", 421); | |
85 r.Add("c", 422); | |
86 r.Add("d", 423); | |
87 | |
509
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
88 r.MakeMostRecent("a"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
89 r.MakeMostRecent("d"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
90 r.MakeMostRecent("b"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
91 r.MakeMostRecent("c"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
92 r.MakeMostRecent("d"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
93 r.MakeMostRecent("c"); |
282 | 94 |
95 ASSERT_TRUE(r.Contains("b")); | |
96 ASSERT_EQ(421, r.Invalidate("b")); | |
97 ASSERT_FALSE(r.Contains("b")); | |
98 | |
99 int p; | |
283 | 100 ASSERT_TRUE(r.Contains("a", p)); ASSERT_EQ(420, p); |
101 ASSERT_TRUE(r.Contains("c", p)); ASSERT_EQ(422, p); | |
102 ASSERT_TRUE(r.Contains("d", p)); ASSERT_EQ(423, p); | |
103 | |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
104 ASSERT_EQ("a", r.GetOldest()); |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
105 ASSERT_EQ(420, r.GetOldestPayload()); |
282 | 106 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
|
107 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
108 ASSERT_EQ("d", r.GetOldest()); |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
109 ASSERT_EQ(423, r.GetOldestPayload()); |
282 | 110 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
|
111 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
112 ASSERT_EQ("c", r.GetOldest()); |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
113 ASSERT_EQ(422, r.GetOldestPayload()); |
282 | 114 ASSERT_EQ("c", r.RemoveOldest(p)); ASSERT_EQ(422, p); |
115 | |
116 ASSERT_TRUE(r.IsEmpty()); | |
117 } | |
118 | |
119 | |
509
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
120 TEST(LRU, PayloadUpdate) |
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 Orthanc::LeastRecentlyUsedIndex<std::string, int> r; |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
123 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
124 r.Add("a", 420); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
125 r.Add("b", 421); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
126 r.Add("d", 423); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
127 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
128 r.MakeMostRecent("a", 424); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
129 r.MakeMostRecent("d", 421); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
130 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
131 ASSERT_EQ("b", r.GetOldest()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
132 ASSERT_EQ(421, r.GetOldestPayload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
133 r.RemoveOldest(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
134 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
135 ASSERT_EQ("a", r.GetOldest()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
136 ASSERT_EQ(424, r.GetOldestPayload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
137 r.RemoveOldest(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
138 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
139 ASSERT_EQ("d", r.GetOldest()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
140 ASSERT_EQ(421, r.GetOldestPayload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
141 r.RemoveOldest(); |
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 ASSERT_TRUE(r.IsEmpty()); |
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 |
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 TEST(LRU, PayloadUpdateBis) |
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 Orthanc::LeastRecentlyUsedIndex<std::string, int> r; |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
151 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
152 r.AddOrMakeMostRecent("a", 420); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
153 r.AddOrMakeMostRecent("b", 421); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
154 r.AddOrMakeMostRecent("d", 423); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
155 r.AddOrMakeMostRecent("a", 424); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
156 r.AddOrMakeMostRecent("d", 421); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
157 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
158 ASSERT_EQ("b", r.GetOldest()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
159 ASSERT_EQ(421, r.GetOldestPayload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
160 r.RemoveOldest(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
161 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
162 ASSERT_EQ("a", r.GetOldest()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
163 ASSERT_EQ(424, r.GetOldestPayload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
164 r.RemoveOldest(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
165 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
166 ASSERT_EQ("d", r.GetOldest()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
167 ASSERT_EQ(421, r.GetOldestPayload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
168 r.RemoveOldest(); |
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 ASSERT_TRUE(r.IsEmpty()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
171 } |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
172 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
173 |
283 | 174 |
175 | |
176 namespace | |
177 { | |
178 class Integer : public Orthanc::IDynamicObject | |
179 { | |
180 private: | |
181 std::string& log_; | |
182 int value_; | |
282 | 183 |
184 public: | |
283 | 185 Integer(std::string& log, int v) : log_(log), value_(v) |
186 { | |
187 } | |
188 | |
189 virtual ~Integer() | |
190 { | |
191 LOG(INFO) << "Removing cache entry for " << value_; | |
192 log_ += boost::lexical_cast<std::string>(value_) + " "; | |
193 } | |
194 }; | |
195 | |
284
06aa7b7b6723
implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
196 class IntegerProvider : public Orthanc::ICachePageProvider |
283 | 197 { |
198 public: | |
199 std::string log_; | |
200 | |
201 Orthanc::IDynamicObject* Provide(const std::string& s) | |
202 { | |
203 LOG(INFO) << "Providing " << s; | |
204 return new Integer(log_, boost::lexical_cast<int>(s)); | |
205 } | |
282 | 206 }; |
207 } | |
283 | 208 |
209 | |
210 TEST(MemoryCache, Basic) | |
211 { | |
212 IntegerProvider provider; | |
213 | |
214 { | |
215 Orthanc::MemoryCache cache(provider, 3); | |
285
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
216 cache.Access("42"); // 42 -> exit |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
217 cache.Access("43"); // 43, 42 -> exit |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
218 cache.Access("45"); // 45, 43, 42 -> exit |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
219 cache.Access("42"); // 42, 45, 43 -> exit |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
220 cache.Access("43"); // 43, 42, 45 -> exit |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
221 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
|
222 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
|
223 cache.Access("42"); // 43 is removed; 42, 44, 47 -> exit |
283 | 224 // Closing the cache: 47, 44, 42 are successively removed |
225 } | |
226 | |
227 ASSERT_EQ("45 42 43 47 44 42 ", provider.log_); | |
228 } | |
1367 | 229 |
230 | |
231 | |
232 | |
233 | |
234 namespace | |
235 { | |
236 class S : public Orthanc::IDynamicObject | |
237 { | |
238 private: | |
239 std::string value_; | |
240 | |
241 public: | |
242 S(const std::string& value) : value_(value) | |
243 { | |
244 } | |
245 | |
246 const std::string& GetValue() const | |
247 { | |
248 return value_; | |
249 } | |
250 }; | |
251 } | |
252 | |
253 | |
254 TEST(LRU, SharedArchive) | |
255 { | |
256 std::string first, second; | |
257 Orthanc::SharedArchive a(3); | |
258 first = a.Add(new S("First item")); | |
259 second = a.Add(new S("Second item")); | |
260 | |
261 for (int i = 1; i < 100; i++) | |
262 { | |
263 a.Add(new S("Item " + boost::lexical_cast<std::string>(i))); | |
264 // Continuously protect the two first items | |
265 try { Orthanc::SharedArchive::Accessor(a, first); } catch (Orthanc::OrthancException&) {} | |
266 try { Orthanc::SharedArchive::Accessor(a, second); } catch (Orthanc::OrthancException&) {} | |
267 } | |
268 | |
269 std::list<std::string> i; | |
270 a.List(i); | |
271 | |
272 size_t count = 0; | |
273 for (std::list<std::string>::const_iterator | |
274 it = i.begin(); it != i.end(); it++) | |
275 { | |
276 if (*it == first || | |
277 *it == second) | |
278 { | |
279 count++; | |
280 } | |
281 } | |
282 | |
1492 | 283 ASSERT_EQ(2u, count); |
1367 | 284 } |