2 * Copyright (C) 1996-2015 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 "StoreFileSystem.h"
21 #include "StoreSearch.h"
24 #include "testStoreSupport.h"
25 #include "unitTestMain.h"
35 #define TESTDIR "testRock_Store"
37 CPPUNIT_TEST_SUITE_REGISTRATION( testRock
);
39 extern REMOVALPOLICYCREATE createRemovalPolicy_lru
;
41 static char cwd
[MAXPATHLEN
];
44 addSwapDir(testRock::SwapDirPointer aStore
)
46 allocate_new_swapdir(&Config
.cacheSwap
);
47 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
48 ++Config
.cacheSwap
.n_configured
;
54 CPPUNIT_NS::TestFixture::setUp();
56 if (0 > system ("rm -rf " TESTDIR
))
57 throw std::runtime_error("Failed to clean test work directory");
59 // use current directory for shared segments (on path-based OSes)
60 Ipc::Mem::Segment::BasePath
= getcwd(cwd
,MAXPATHLEN
);
61 if (Ipc::Mem::Segment::BasePath
== NULL
)
62 Ipc::Mem::Segment::BasePath
= ".";
64 Store::Root(new StoreController
);
66 store
= new Rock::SwapDir();
72 char *path
=xstrdup(TESTDIR
);
74 char *config_line
=xstrdup("10 max-size=16384");
76 ConfigParser::SetCfgLine(config_line
);
78 store
->parse(0, path
);
79 store_maxobjsize
= 1024*1024*2;
83 safe_free(config_line
);
85 /* ok, ready to create */
88 rr
= new Rock::SwapDirRr
;
95 CPPUNIT_NS::TestFixture::tearDown();
101 free_cachedir(&Config
.cacheSwap
);
103 rr
->finishShutdown(); // deletes rr
106 // TODO: do this once, or each time.
107 // safe_free(Config.replPolicy->type);
108 // delete Config.replPolicy;
110 if (0 > system ("rm -rf " TESTDIR
))
111 throw std::runtime_error("Failed to clean test work directory");
115 testRock::commonInit()
117 static bool inited
= false;
122 StoreFileSystem::SetupAllFs();
124 Config
.Store
.avgObjectSize
= 1024;
125 Config
.Store
.objectsPerBucket
= 20;
126 Config
.Store
.maxObjectSize
= 2048;
128 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
130 Config
.replPolicy
= new RemovalPolicySettings
;
131 Config
.replPolicy
->type
= xstrdup("lru");
132 Config
.replPolicy
->args
= NULL
;
135 storeReplAdd("lru", createRemovalPolicy_lru
);
137 visible_appname_string
= xstrdup(APP_FULLNAME
);
143 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
145 httpReplyInitModule(); /* must go before accepting replies */
147 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
153 testRock::storeInit()
155 /* ok, ready to use */
156 Store::Root().init();
158 /* rebuild is a scheduled event */
161 /* our swapdir must be scheduled to rebuild */
162 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding
);
166 /* cannot use loop.run(); as the loop will never idle: the store-dir
167 * clean() scheduled event prevents it
170 /* nothing left to rebuild */
171 CPPUNIT_ASSERT_EQUAL(0, StoreController::store_dirs_rebuilding
);
178 snprintf(buf
, sizeof(buf
), "dummy url %i", i
);
179 buf
[sizeof(buf
) - 1] = '\0';
184 testRock::createEntry(const int i
)
187 flags
.cachable
= true;
188 StoreEntry
*const pe
=
189 storeCreateEntry(storeId(i
), "dummy log url", flags
, Http::METHOD_GET
);
190 HttpReply
*const rep
= const_cast<HttpReply
*>(pe
->getReply());
191 rep
->setHeaders(Http::scOkay
, "dummy test object", "x-squid-internal/test", 0, -1, squid_curtime
+ 100000);
199 testRock::addEntry(const int i
)
201 StoreEntry
*const pe
= createEntry(i
);
204 /* TODO: remove this when the metadata is separated */
207 packerToStoreInit(&p
, pe
);
208 pe
->getReply()->packHeadersInto(&p
);
220 testRock::getEntry(const int i
)
222 return storeGetPublic(storeId(i
), Http::METHOD_GET
);
226 testRock::testRockCreate()
230 CPPUNIT_ASSERT_EQUAL(0, ::stat(TESTDIR
, &sb
));
232 /* TODO: check the size */
234 /* TODO: test rebuild */
238 testRock::testRockSwapOut()
242 // add few entries to prime the database
243 for (int i
= 0; i
< 5; ++i
) {
244 CPPUNIT_ASSERT_EQUAL((uint64_t)i
, store
->currentCount());
246 StoreEntry
*const pe
= addEntry(i
);
248 CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING
, pe
->swap_status
);
249 CPPUNIT_ASSERT_EQUAL(0, pe
->swap_dirn
);
250 CPPUNIT_ASSERT(pe
->swap_filen
>= 0);
252 // Rock::IoState::finishedWriting() schedules an AsyncCall
253 // storeSwapOutFileClosed(). Let it fire.
257 CPPUNIT_ASSERT_EQUAL(SWAPOUT_DONE
, pe
->swap_status
);
259 pe
->unlock("testRock::testRockSwapOut priming");
262 CPPUNIT_ASSERT_EQUAL((uint64_t)5, store
->currentCount());
264 // try to swap out entry to a used unlocked slot
266 StoreEntry
*const pe
= addEntry(4);
268 CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING
, pe
->swap_status
);
269 CPPUNIT_ASSERT_EQUAL(0, pe
->swap_dirn
);
270 CPPUNIT_ASSERT(pe
->swap_filen
>= 0);
275 CPPUNIT_ASSERT_EQUAL(SWAPOUT_DONE
, pe
->swap_status
);
277 pe
->unlock("testRock::testRockSwapOut e#4");
280 // try to swap out entry to a used locked slot
282 StoreEntry
*const pe
= addEntry(5);
284 CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING
, pe
->swap_status
);
285 CPPUNIT_ASSERT_EQUAL(0, pe
->swap_dirn
);
286 CPPUNIT_ASSERT(pe
->swap_filen
>= 0);
288 // the slot is locked here because the async calls have not run yet
289 StoreEntry
*const pe2
= addEntry(5);
290 CPPUNIT_ASSERT_EQUAL(SWAPOUT_NONE
, pe2
->swap_status
);
291 CPPUNIT_ASSERT_EQUAL(MemObject::SwapOut::swImpossible
, pe2
->mem_obj
->swapout
.decision
);
292 CPPUNIT_ASSERT_EQUAL(-1, pe2
->swap_dirn
);
293 CPPUNIT_ASSERT_EQUAL(-1, pe2
->swap_filen
);
298 pe
->unlock("testRock::testRockSwapOut e#5.1");
299 pe2
->unlock("testRock::testRockSwapOut e#5.2");
301 // pe2 has the same public key as pe so it marks old pe for release
302 // here, we add another entry #5 into the now-available slot
303 StoreEntry
*const pe3
= addEntry(5);
304 CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING
, pe3
->swap_status
);
305 CPPUNIT_ASSERT_EQUAL(0, pe3
->swap_dirn
);
306 CPPUNIT_ASSERT(pe3
->swap_filen
>= 0);
308 CPPUNIT_ASSERT_EQUAL(SWAPOUT_DONE
, pe3
->swap_status
);
309 pe3
->unlock("testRock::testRockSwapOut e#5.3");
312 CPPUNIT_ASSERT_EQUAL((uint64_t)6, store
->currentCount());
314 // try to get and release all entries
315 for (int i
= 0; i
< 6; ++i
) {
316 StoreEntry
*const pe
= getEntry(i
);
317 CPPUNIT_ASSERT(pe
!= NULL
);
319 pe
->release(); // destroys pe
321 StoreEntry
*const pe2
= getEntry(i
);
322 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry
*>(NULL
), pe2
);