1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
4 import org
.junit
.ClassRule
;
7 import org
.junit
.rules
.TemporaryFolder
;
8 import org
.rocksdb
.util
.Environment
;
10 import java
.io
.IOException
;
12 import static java
.nio
.file
.Files
.readAllBytes
;
13 import static java
.nio
.file
.Paths
.get
;
14 import static org
.assertj
.core
.api
.Assertions
.assertThat
;
16 public class InfoLogLevelTest
{
19 public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE
=
20 new RocksNativeLibraryResource();
23 public TemporaryFolder dbFolder
= new TemporaryFolder();
26 public void testInfoLogLevel() throws RocksDBException
,
28 try (final RocksDB db
=
29 RocksDB
.open(dbFolder
.getRoot().getAbsolutePath())) {
30 db
.put("key".getBytes(), "value".getBytes());
31 db
.flush(new FlushOptions().setWaitForFlush(true));
32 assertThat(getLogContentsWithoutHeader()).isNotEmpty();
37 public void testFatalLogLevel() throws RocksDBException
,
39 try (final Options options
= new Options().
40 setCreateIfMissing(true).
41 setInfoLogLevel(InfoLogLevel
.FATAL_LEVEL
);
42 final RocksDB db
= RocksDB
.open(options
,
43 dbFolder
.getRoot().getAbsolutePath())) {
44 assertThat(options
.infoLogLevel()).
45 isEqualTo(InfoLogLevel
.FATAL_LEVEL
);
46 db
.put("key".getBytes(), "value".getBytes());
47 // As InfoLogLevel is set to FATAL_LEVEL, here we expect the log
48 // content to be empty.
49 assertThat(getLogContentsWithoutHeader()).isEmpty();
54 public void testFatalLogLevelWithDBOptions()
55 throws RocksDBException
, IOException
{
56 try (final DBOptions dbOptions
= new DBOptions().
57 setInfoLogLevel(InfoLogLevel
.FATAL_LEVEL
);
58 final Options options
= new Options(dbOptions
,
59 new ColumnFamilyOptions()).
60 setCreateIfMissing(true);
62 RocksDB
.open(options
, dbFolder
.getRoot().getAbsolutePath())) {
63 assertThat(dbOptions
.infoLogLevel()).
64 isEqualTo(InfoLogLevel
.FATAL_LEVEL
);
65 assertThat(options
.infoLogLevel()).
66 isEqualTo(InfoLogLevel
.FATAL_LEVEL
);
67 db
.put("key".getBytes(), "value".getBytes());
68 assertThat(getLogContentsWithoutHeader()).isEmpty();
72 @Test(expected
= IllegalArgumentException
.class)
73 public void failIfIllegalByteValueProvided() {
74 InfoLogLevel
.getInfoLogLevel((byte) -1);
78 public void valueOf() {
79 assertThat(InfoLogLevel
.valueOf("DEBUG_LEVEL")).
80 isEqualTo(InfoLogLevel
.DEBUG_LEVEL
);
84 * Read LOG file contents into String.
86 * @return LOG file contents as String.
87 * @throws IOException if file is not found.
89 private String
getLogContentsWithoutHeader() throws IOException
{
90 final String separator
= Environment
.isWindows() ?
91 "\n" : System
.getProperty("line.separator");
92 final String
[] lines
= new String(readAllBytes(get(
93 dbFolder
.getRoot().getAbsolutePath() + "/LOG"))).split(separator
);
95 int first_non_header
= lines
.length
;
96 // Identify the last line of the header
97 for (int i
= lines
.length
- 1; i
>= 0; --i
) {
98 if (lines
[i
].indexOf("DB pointer") >= 0) {
99 first_non_header
= i
+ 1;
103 StringBuilder builder
= new StringBuilder();
104 for (int i
= first_non_header
; i
< lines
.length
; ++i
) {
105 builder
.append(lines
[i
]).append(separator
);
107 return builder
.toString();