2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include "ConfigParser.h"
11 #include "DiskIO/DiskIOModule.h"
12 #include "fs/rock/RockSwapDir.h"
14 #include "HttpHeader.h"
15 #include "HttpReply.h"
16 #include "MemObject.h"
17 #include "RequestFlags.h"
18 #include "SquidConfig.h"
20 #include "store/Disk.h"
21 #include "store/Disks.h"
22 #include "StoreFileSystem.h"
23 #include "StoreSearch.h"
25 #include "testStoreSupport.h"
26 #include "unitTestMain.h"
38 CPPUNIT_TEST_SUITE_REGISTRATION( testRock
);
40 extern REMOVALPOLICYCREATE createRemovalPolicy_lru
;
42 static char cwd
[MAXPATHLEN
];
45 addSwapDir(testRock::SwapDirPointer aStore
)
47 allocate_new_swapdir(&Config
.cacheSwap
);
48 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
49 ++Config
.cacheSwap
.n_configured
;
55 CPPUNIT_NS::TestFixture::setUp();
57 if (0 > system ("rm -rf " TESTDIR
))
58 throw std::runtime_error("Failed to clean test work directory");
60 Config
.memShared
.defaultTo(false);
61 Config
.shmLocking
.defaultTo(false);
63 // use current directory for shared segments (on path-based OSes)
64 Ipc::Mem::Segment::BasePath
= getcwd(cwd
,MAXPATHLEN
);
65 if (Ipc::Mem::Segment::BasePath
== NULL
)
66 Ipc::Mem::Segment::BasePath
= ".";
70 store
= new Rock::SwapDir();
76 char *path
=xstrdup(TESTDIR
);
78 char *config_line
=xstrdup("10 max-size=16384");
80 ConfigParser::SetCfgLine(config_line
);
82 store
->parse(0, path
);
83 store_maxobjsize
= 1024*1024*2;
87 safe_free(config_line
);
89 /* ok, ready to create */
92 rr
= new Rock::SwapDirRr
;
99 CPPUNIT_NS::TestFixture::tearDown();
105 free_cachedir(&Config
.cacheSwap
);
107 rr
->finishShutdown(); // deletes rr
110 // TODO: do this once, or each time.
111 // safe_free(Config.replPolicy->type);
112 // delete Config.replPolicy;
114 if (0 > system ("rm -rf " TESTDIR
))
115 throw std::runtime_error("Failed to clean test work directory");
119 testRock::commonInit()
121 static bool inited
= false;
126 StoreFileSystem::SetupAllFs();
128 Config
.Store
.avgObjectSize
= 1024;
129 Config
.Store
.objectsPerBucket
= 20;
130 Config
.Store
.maxObjectSize
= 2048;
132 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
134 Config
.replPolicy
= new RemovalPolicySettings
;
135 Config
.replPolicy
->type
= xstrdup("lru");
136 Config
.replPolicy
->args
= NULL
;
139 storeReplAdd("lru", createRemovalPolicy_lru
);
141 visible_appname_string
= xstrdup(APP_FULLNAME
);
147 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
149 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
155 testRock::storeInit()
157 /* ok, ready to use */
158 Store::Root().init();
160 /* rebuild is a scheduled event */
163 /* our swapdir must be scheduled to rebuild */
164 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding
);
168 /* cannot use loop.run(); as the loop will never idle: the store-dir
169 * clean() scheduled event prevents it
172 /* nothing left to rebuild */
173 CPPUNIT_ASSERT_EQUAL(0, StoreController::store_dirs_rebuilding
);
180 snprintf(buf
, sizeof(buf
), "dummy url %i", i
);
181 buf
[sizeof(buf
) - 1] = '\0';
186 testRock::createEntry(const int i
)
189 flags
.cachable
= true;
190 StoreEntry
*const pe
=
191 storeCreateEntry(storeId(i
), "dummy log url", flags
, Http::METHOD_GET
);
192 HttpReply
*const rep
= const_cast<HttpReply
*>(pe
->getReply());
193 rep
->setHeaders(Http::scOkay
, "dummy test object", "x-squid-internal/test", 0, -1, squid_curtime
+ 100000);
201 testRock::addEntry(const int i
)
203 StoreEntry
*const pe
= createEntry(i
);
206 pe
->getReply()->packHeadersInto(pe
);
216 testRock::getEntry(const int i
)
218 return storeGetPublic(storeId(i
), Http::METHOD_GET
);
222 testRock::testRockCreate()
226 CPPUNIT_ASSERT_EQUAL(0, ::stat(TESTDIR
, &sb
));
228 /* TODO: check the size */
230 /* TODO: test rebuild */
234 testRock::testRockSwapOut()
238 // add few entries to prime the database
239 for (int i
= 0; i
< 5; ++i
) {
240 CPPUNIT_ASSERT_EQUAL((uint64_t)i
, store
->currentCount());
242 StoreEntry
*const pe
= addEntry(i
);
244 CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING
, pe
->swap_status
);
245 CPPUNIT_ASSERT_EQUAL(0, pe
->swap_dirn
);
246 CPPUNIT_ASSERT(pe
->swap_filen
>= 0);
248 // Rock::IoState::finishedWriting() schedules an AsyncCall
249 // storeSwapOutFileClosed(). Let it fire.
253 CPPUNIT_ASSERT_EQUAL(SWAPOUT_DONE
, pe
->swap_status
);
255 pe
->unlock("testRock::testRockSwapOut priming");
258 CPPUNIT_ASSERT_EQUAL((uint64_t)5, store
->currentCount());
260 // try to swap out entry to a used unlocked slot
262 StoreEntry
*const pe
= addEntry(4);
264 CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING
, pe
->swap_status
);
265 CPPUNIT_ASSERT_EQUAL(0, pe
->swap_dirn
);
266 CPPUNIT_ASSERT(pe
->swap_filen
>= 0);
271 CPPUNIT_ASSERT_EQUAL(SWAPOUT_DONE
, pe
->swap_status
);
273 pe
->unlock("testRock::testRockSwapOut e#4");
276 // try to swap out entry to a used locked slot
278 StoreEntry
*const pe
= addEntry(5);
280 CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING
, pe
->swap_status
);
281 CPPUNIT_ASSERT_EQUAL(0, pe
->swap_dirn
);
282 CPPUNIT_ASSERT(pe
->swap_filen
>= 0);
284 // the slot is locked here because the async calls have not run yet
285 StoreEntry
*const pe2
= addEntry(5);
286 CPPUNIT_ASSERT_EQUAL(SWAPOUT_NONE
, pe2
->swap_status
);
287 CPPUNIT_ASSERT_EQUAL(MemObject::SwapOut::swImpossible
, pe2
->mem_obj
->swapout
.decision
);
288 CPPUNIT_ASSERT_EQUAL(-1, pe2
->swap_dirn
);
289 CPPUNIT_ASSERT_EQUAL(-1, pe2
->swap_filen
);
294 pe
->unlock("testRock::testRockSwapOut e#5.1");
295 pe2
->unlock("testRock::testRockSwapOut e#5.2");
297 // pe2 has the same public key as pe so it marks old pe for release
298 // here, we add another entry #5 into the now-available slot
299 StoreEntry
*const pe3
= addEntry(5);
300 CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING
, pe3
->swap_status
);
301 CPPUNIT_ASSERT_EQUAL(0, pe3
->swap_dirn
);
302 CPPUNIT_ASSERT(pe3
->swap_filen
>= 0);
304 CPPUNIT_ASSERT_EQUAL(SWAPOUT_DONE
, pe3
->swap_status
);
305 pe3
->unlock("testRock::testRockSwapOut e#5.3");
308 CPPUNIT_ASSERT_EQUAL((uint64_t)6, store
->currentCount());
310 // try to get and release all entries
311 for (int i
= 0; i
< 6; ++i
) {
312 StoreEntry
*const pe
= getEntry(i
);
313 CPPUNIT_ASSERT(pe
!= NULL
);
315 pe
->release(); // destroys pe
317 StoreEntry
*const pe2
= getEntry(i
);
318 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), pe2
);