2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #include <gtest/gtest.h>
18 #include "hphp/util/log-file-flusher.h"
22 struct MockLogFileFlusher
: LogFileFlusher
{
23 MockLogFileFlusher() : last_flush(0) {}
27 void dropCache(int /*fd*/, off_t off
) override
{ last_flush
= off
; }
30 static void doWrite(LogFileFlusher
* flusher
, int fd
, int bytes
) {
31 char* buf
= new char[bytes
];
32 write(fd
, buf
, bytes
);
34 flusher
->recordWriteAndMaybeDropCaches(fd
, bytes
);
37 TEST(LogFileFlusherTest
, flush
) {
38 MockLogFileFlusher flusher
;
40 LogFileFlusher::DropCacheChunkSize
= 1 << 19;
42 ASSERT_EQ(LogFileFlusher::kDropCacheTail
, 1 << 20);
44 char tmpl
[] = "/tmp/LogFileFlusherTest.XXXXXX";
45 int fd
= mkstemp(tmpl
);
49 // 100 bytes written, not enough to flush
50 doWrite(&flusher
, fd
, 100);
51 EXPECT_EQ(flusher
.last_flush
, 0);
53 // 512KB written, but file is not 1 MB large yet so no fadvise
54 doWrite(&flusher
, fd
, (1 << 19));
55 EXPECT_EQ(flusher
.last_flush
, 0);
57 // Another 512KB, after one more byte a fadvise will be called
58 doWrite(&flusher
, fd
, (1 << 19));
59 EXPECT_EQ(flusher
.last_flush
, 0);
62 doWrite(&flusher
, fd
, 1);
63 EXPECT_EQ(flusher
.last_flush
, 101);
65 // Almost ready for another flush
66 doWrite(&flusher
, fd
, (1 << 19));
67 EXPECT_EQ(flusher
.last_flush
, 101);
69 // Another flush occurs
70 doWrite(&flusher
, fd
, 1);
71 EXPECT_EQ(flusher
.last_flush
, (1 << 19) + 100 + 2);