2 * Copyright (C) 1996-2014 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"
34 #define TESTDIR "testRock_Store"
36 CPPUNIT_TEST_SUITE_REGISTRATION( testRock
);
38 extern REMOVALPOLICYCREATE createRemovalPolicy_lru
;
40 static char cwd
[MAXPATHLEN
];
43 addSwapDir(testRock::SwapDirPointer aStore
)
45 allocate_new_swapdir(&Config
.cacheSwap
);
46 Config
.cacheSwap
.swapDirs
[Config
.cacheSwap
.n_configured
] = aStore
.getRaw();
47 ++Config
.cacheSwap
.n_configured
;
53 CPPUNIT_NS::TestFixture::setUp();
55 if (0 > system ("rm -rf " TESTDIR
))
56 throw std::runtime_error("Failed to clean test work directory");
58 // use current directory for shared segments (on path-based OSes)
59 Ipc::Mem::Segment::BasePath
= getcwd(cwd
,MAXPATHLEN
);
60 if (Ipc::Mem::Segment::BasePath
== NULL
)
61 Ipc::Mem::Segment::BasePath
= ".";
63 Store::Root(new StoreController
);
65 store
= new Rock::SwapDir();
71 char *path
=xstrdup(TESTDIR
);
73 char *config_line
=xstrdup("10 max-size=16384");
75 ConfigParser::SetCfgLine(config_line
);
77 store
->parse(0, path
);
78 store_maxobjsize
= 1024*1024*2;
82 safe_free(config_line
);
84 /* ok, ready to create */
87 rr
= new Rock::SwapDirRr
;
94 CPPUNIT_NS::TestFixture::tearDown();
100 free_cachedir(&Config
.cacheSwap
);
102 rr
->finishShutdown(); // deletes rr
105 // TODO: do this once, or each time.
106 // safe_free(Config.replPolicy->type);
107 // delete Config.replPolicy;
109 if (0 > system ("rm -rf " TESTDIR
))
110 throw std::runtime_error("Failed to clean test work directory");
114 testRock::commonInit()
116 static bool inited
= false;
121 StoreFileSystem::SetupAllFs();
123 Config
.Store
.avgObjectSize
= 1024;
124 Config
.Store
.objectsPerBucket
= 20;
125 Config
.Store
.maxObjectSize
= 2048;
127 Config
.store_dir_select_algorithm
= xstrdup("round-robin");
129 Config
.replPolicy
= new RemovalPolicySettings
;
130 Config
.replPolicy
->type
= xstrdup("lru");
131 Config
.replPolicy
->args
= NULL
;
134 storeReplAdd("lru", createRemovalPolicy_lru
);
136 visible_appname_string
= xstrdup(APP_FULLNAME
);
142 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
144 httpReplyInitModule(); /* must go before accepting replies */
146 mem_policy
= createRemovalPolicy(Config
.replPolicy
);
152 testRock::storeInit()
154 /* ok, ready to use */
155 Store::Root().init();
157 /* rebuild is a scheduled event */
160 /* our swapdir must be scheduled to rebuild */
161 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding
);
165 /* cannot use loop.run(); as the loop will never idle: the store-dir
166 * clean() scheduled event prevents it
169 /* nothing left to rebuild */
170 CPPUNIT_ASSERT_EQUAL(0, StoreController::store_dirs_rebuilding
);
177 snprintf(buf
, sizeof(buf
), "dummy url %i", i
);
178 buf
[sizeof(buf
) - 1] = '\0';
183 testRock::createEntry(const int i
)
186 flags
.cachable
= true;
187 StoreEntry
*const pe
=
188 storeCreateEntry(storeId(i
), "dummy log url", flags
, Http::METHOD_GET
);
189 HttpReply
*const rep
= const_cast<HttpReply
*>(pe
->getReply());
190 rep
->setHeaders(Http::scOkay
, "dummy test object", "x-squid-internal/test", 0, -1, squid_curtime
+ 100000);
198 testRock::addEntry(const int i
)
200 StoreEntry
*const pe
= createEntry(i
);
203 /* TODO: remove this when the metadata is separated */
206 packerToStoreInit(&p
, pe
);
207 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
);