github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/internal/base/options_test.go (about) 1 // Copyright 2018 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package base 6 7 import ( 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestLevelOptions(t *testing.T) { 14 var opts *Options 15 opts = opts.EnsureDefaults() 16 17 testCases := []struct { 18 level int 19 targetFileSize int64 20 }{ 21 {0, 2 << 20}, 22 {1, (2 * 2) << 20}, 23 {2, (4 * 2) << 20}, 24 {3, (8 * 2) << 20}, 25 {4, (16 * 2) << 20}, 26 {5, (32 * 2) << 20}, 27 {6, (64 * 2) << 20}, 28 } 29 for _, c := range testCases { 30 l := opts.Level(c.level) 31 if c.targetFileSize != l.TargetFileSize { 32 t.Fatalf("%d: expected target-file-size %d, but found %d", 33 c.level, c.targetFileSize, l.TargetFileSize) 34 } 35 } 36 } 37 38 func TestOptionsString(t *testing.T) { 39 const expected = `[Version] 40 pebble_version=0.1 41 42 [Options] 43 bytes_per_sync=524288 44 cache_size=8388608 45 comparer=leveldb.BytewiseComparator 46 disable_wal=false 47 l0_compaction_threshold=4 48 l0_stop_writes_threshold=12 49 lbase_max_bytes=67108864 50 max_manifest_file_size=134217728 51 max_open_files=1000 52 mem_table_size=4194304 53 mem_table_stop_writes_threshold=2 54 min_compaction_rate=4194304 55 min_flush_rate=1048576 56 merger=pebble.concatenate 57 table_property_collectors=[] 58 wal_dir= 59 60 [Level "0"] 61 block_restart_interval=16 62 block_size=4096 63 compression=Snappy 64 filter_policy=none 65 filter_type=table 66 index_block_size=4096 67 target_file_size=2097152 68 ` 69 70 var opts *Options 71 opts = opts.EnsureDefaults() 72 if v := opts.String(); expected != v { 73 t.Fatalf("expected\n%s\nbut found\n%s", expected, v) 74 } 75 } 76 77 func TestOptionsCheck(t *testing.T) { 78 var opts *Options 79 opts = opts.EnsureDefaults() 80 s := opts.String() 81 require.NoError(t, opts.Check(s)) 82 require.Regexp(t, `invalid key=value syntax`, opts.Check("foo\n")) 83 84 tmp := *opts 85 tmp.Comparer = &Comparer{Name: "foo"} 86 require.Regexp(t, `comparer name from file.*!=.*`, tmp.Check(s)) 87 88 tmp = *opts 89 tmp.Merger = &Merger{Name: "foo"} 90 require.Regexp(t, `merger name from file.*!=.*`, tmp.Check(s)) 91 92 // RocksDB uses a similar (INI-style) syntax for the OPTIONS file, but 93 // different section names and keys. 94 s = ` 95 [CFOptions "default"] 96 comparator=rocksdb-comparer 97 merge_operator=rocksdb-merger 98 ` 99 tmp = *opts 100 tmp.Comparer = &Comparer{Name: "foo"} 101 require.Regexp(t, `comparer name from file.*!=.*`, tmp.Check(s)) 102 103 tmp.Comparer = &Comparer{Name: "rocksdb-comparer"} 104 tmp.Merger = &Merger{Name: "foo"} 105 require.Regexp(t, `merger name from file.*!=.*`, tmp.Check(s)) 106 107 tmp.Merger = &Merger{Name: "rocksdb-merger"} 108 require.NoError(t, tmp.Check(s)) 109 110 // RocksDB allows the merge operator to be unspecified, in which case it 111 // shows up as "nullptr". 112 s = ` 113 [CFOptions "default"] 114 merge_operator=nullptr 115 ` 116 tmp = *opts 117 require.NoError(t, tmp.Check(s)) 118 }