Mercurial > hg > orthanc
annotate UnitTestsSources/MemoryCache.cpp @ 925:a4362c5263e2 mac
note
author | jodogne |
---|---|
date | Wed, 26 Feb 2014 15:12:50 +0100 |
parents | 17815b9d4280 |
children | 3d6f9b7d0add |
rev | line source |
---|---|
282 | 1 #include "gtest/gtest.h" |
2 | |
283 | 3 #include <glog/logging.h> |
282 | 4 #include <memory> |
283 | 5 #include <boost/thread.hpp> |
6 #include <boost/lexical_cast.hpp> | |
282 | 7 #include "../Core/IDynamicObject.h" |
284
06aa7b7b6723
implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
8 #include "../Core/Cache/MemoryCache.h" |
282 | 9 |
10 | |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
285
diff
changeset
|
11 TEST(LRU, Basic) |
282 | 12 { |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
285
diff
changeset
|
13 Orthanc::LeastRecentlyUsedIndex<std::string> r; |
282 | 14 |
15 r.Add("d"); | |
16 r.Add("a"); | |
17 r.Add("c"); | |
18 r.Add("b"); | |
19 | |
509
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
20 r.MakeMostRecent("a"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
21 r.MakeMostRecent("d"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
22 r.MakeMostRecent("b"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
23 r.MakeMostRecent("c"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
24 r.MakeMostRecent("d"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
25 r.MakeMostRecent("c"); |
282 | 26 |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
27 ASSERT_EQ("a", r.GetOldest()); |
282 | 28 ASSERT_EQ("a", r.RemoveOldest()); |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
29 ASSERT_EQ("b", r.GetOldest()); |
282 | 30 ASSERT_EQ("b", r.RemoveOldest()); |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
31 ASSERT_EQ("d", r.GetOldest()); |
282 | 32 ASSERT_EQ("d", r.RemoveOldest()); |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
33 ASSERT_EQ("c", r.GetOldest()); |
282 | 34 ASSERT_EQ("c", r.RemoveOldest()); |
35 | |
36 ASSERT_TRUE(r.IsEmpty()); | |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
37 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
38 ASSERT_THROW(r.GetOldest(), Orthanc::OrthancException); |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
39 ASSERT_THROW(r.RemoveOldest(), Orthanc::OrthancException); |
282 | 40 } |
41 | |
42 | |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
285
diff
changeset
|
43 TEST(LRU, Payload) |
282 | 44 { |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
285
diff
changeset
|
45 Orthanc::LeastRecentlyUsedIndex<std::string, int> r; |
282 | 46 |
47 r.Add("a", 420); | |
48 r.Add("b", 421); | |
49 r.Add("c", 422); | |
50 r.Add("d", 423); | |
51 | |
509
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
52 r.MakeMostRecent("a"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
53 r.MakeMostRecent("d"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
54 r.MakeMostRecent("b"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
55 r.MakeMostRecent("c"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
56 r.MakeMostRecent("d"); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
57 r.MakeMostRecent("c"); |
282 | 58 |
59 ASSERT_TRUE(r.Contains("b")); | |
60 ASSERT_EQ(421, r.Invalidate("b")); | |
61 ASSERT_FALSE(r.Contains("b")); | |
62 | |
63 int p; | |
283 | 64 ASSERT_TRUE(r.Contains("a", p)); ASSERT_EQ(420, p); |
65 ASSERT_TRUE(r.Contains("c", p)); ASSERT_EQ(422, p); | |
66 ASSERT_TRUE(r.Contains("d", p)); ASSERT_EQ(423, p); | |
67 | |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
68 ASSERT_EQ("a", r.GetOldest()); |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
69 ASSERT_EQ(420, r.GetOldestPayload()); |
282 | 70 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
|
71 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
72 ASSERT_EQ("d", r.GetOldest()); |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
73 ASSERT_EQ(423, r.GetOldestPayload()); |
282 | 74 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
|
75 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
76 ASSERT_EQ("c", r.GetOldest()); |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
77 ASSERT_EQ(422, r.GetOldestPayload()); |
282 | 78 ASSERT_EQ("c", r.RemoveOldest(p)); ASSERT_EQ(422, p); |
79 | |
80 ASSERT_TRUE(r.IsEmpty()); | |
81 } | |
82 | |
83 | |
509
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
84 TEST(LRU, PayloadUpdate) |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
85 { |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
86 Orthanc::LeastRecentlyUsedIndex<std::string, int> r; |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
87 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
88 r.Add("a", 420); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
89 r.Add("b", 421); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
90 r.Add("d", 423); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
91 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
92 r.MakeMostRecent("a", 424); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
93 r.MakeMostRecent("d", 421); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
94 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
95 ASSERT_EQ("b", r.GetOldest()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
96 ASSERT_EQ(421, r.GetOldestPayload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
97 r.RemoveOldest(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
98 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
99 ASSERT_EQ("a", r.GetOldest()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
100 ASSERT_EQ(424, r.GetOldestPayload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
101 r.RemoveOldest(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
102 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
103 ASSERT_EQ("d", r.GetOldest()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
104 ASSERT_EQ(421, r.GetOldestPayload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
105 r.RemoveOldest(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
106 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
107 ASSERT_TRUE(r.IsEmpty()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
108 } |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
109 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
110 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
111 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
112 TEST(LRU, PayloadUpdateBis) |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
113 { |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
114 Orthanc::LeastRecentlyUsedIndex<std::string, int> r; |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
115 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
116 r.AddOrMakeMostRecent("a", 420); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
117 r.AddOrMakeMostRecent("b", 421); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
118 r.AddOrMakeMostRecent("d", 423); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
119 r.AddOrMakeMostRecent("a", 424); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
120 r.AddOrMakeMostRecent("d", 421); |
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 ASSERT_EQ("b", r.GetOldest()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
123 ASSERT_EQ(421, r.GetOldestPayload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
124 r.RemoveOldest(); |
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 ASSERT_EQ("a", r.GetOldest()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
127 ASSERT_EQ(424, r.GetOldestPayload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
128 r.RemoveOldest(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
129 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
130 ASSERT_EQ("d", r.GetOldest()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
131 ASSERT_EQ(421, r.GetOldestPayload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
132 r.RemoveOldest(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
133 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
134 ASSERT_TRUE(r.IsEmpty()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
135 } |
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 |
283 | 138 |
139 | |
140 namespace | |
141 { | |
142 class Integer : public Orthanc::IDynamicObject | |
143 { | |
144 private: | |
145 std::string& log_; | |
146 int value_; | |
282 | 147 |
148 public: | |
283 | 149 Integer(std::string& log, int v) : log_(log), value_(v) |
150 { | |
151 } | |
152 | |
153 virtual ~Integer() | |
154 { | |
155 LOG(INFO) << "Removing cache entry for " << value_; | |
156 log_ += boost::lexical_cast<std::string>(value_) + " "; | |
157 } | |
158 | |
159 int GetValue() const | |
160 { | |
161 return value_; | |
162 } | |
163 }; | |
164 | |
284
06aa7b7b6723
implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
165 class IntegerProvider : public Orthanc::ICachePageProvider |
283 | 166 { |
167 public: | |
168 std::string log_; | |
169 | |
170 Orthanc::IDynamicObject* Provide(const std::string& s) | |
171 { | |
172 LOG(INFO) << "Providing " << s; | |
173 return new Integer(log_, boost::lexical_cast<int>(s)); | |
174 } | |
282 | 175 }; |
176 } | |
283 | 177 |
178 | |
179 TEST(MemoryCache, Basic) | |
180 { | |
181 IntegerProvider provider; | |
182 | |
183 { | |
184 Orthanc::MemoryCache cache(provider, 3); | |
285
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
185 cache.Access("42"); // 42 -> exit |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
186 cache.Access("43"); // 43, 42 -> exit |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
187 cache.Access("45"); // 45, 43, 42 -> exit |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
188 cache.Access("42"); // 42, 45, 43 -> exit |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
189 cache.Access("43"); // 43, 42, 45 -> exit |
4031f73fe0e4
access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
284
diff
changeset
|
190 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
|
191 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
|
192 cache.Access("42"); // 43 is removed; 42, 44, 47 -> exit |
283 | 193 // Closing the cache: 47, 44, 42 are successively removed |
194 } | |
195 | |
196 ASSERT_EQ("45 42 43 47 44 42 ", provider.log_); | |
197 } |