github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/search/bleve_leveldb_store_test.go (about) 1 // Copyright 2019 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package search 6 7 import ( 8 "testing" 9 10 "github.com/keybase/client/go/kbfs/data" 11 "github.com/keybase/client/go/kbfs/libcontext" 12 "github.com/keybase/client/go/kbfs/libfs" 13 "github.com/keybase/client/go/kbfs/libkbfs" 14 "github.com/keybase/client/go/kbfs/tlf" 15 "github.com/keybase/client/go/kbfs/tlfhandle" 16 "github.com/keybase/client/go/protocol/keybase1" 17 "github.com/stretchr/testify/require" 18 ) 19 20 func TestBleveLevelDBStore(t *testing.T) { 21 ctx := libcontext.BackgroundContextWithCancellationDelayer() 22 config := libkbfs.MakeTestConfigOrBust(t, "user1", "user2") 23 h, err := tlfhandle.ParseHandle( 24 ctx, config.KBPKI(), config.MDOps(), nil, "user1", tlf.Private) 25 require.NoError(t, err) 26 fs, err := libfs.NewFS( 27 ctx, config, h, data.MasterBranch, "", "", keybase1.MDPriorityNormal) 28 require.NoError(t, err) 29 defer libkbfs.CheckConfigAndShutdown(ctx, t, config) 30 31 t.Log("Open a leveldb using a KBFS billy filesystem") 32 s, err := newBleveLevelDBStore(fs, false, nil) 33 require.NoError(t, err) 34 sNeedsClose := true 35 defer func() { 36 if sNeedsClose { 37 err := s.Close() 38 require.NoError(t, err) 39 } 40 }() 41 42 t.Log("Put some stuff into the db") 43 w, err := s.Writer() 44 require.NoError(t, err) 45 b := w.NewBatch() 46 key1 := []byte("key1") 47 val1 := []byte("val1") 48 key2 := []byte("key2") 49 val2 := []byte("val2") 50 key3 := []byte("otherkey3") 51 val3 := []byte("val3") 52 b.Set(key1, val1) 53 b.Set(key2, val2) 54 b.Set(key3, val3) 55 err = w.ExecuteBatch(b) 56 require.NoError(t, err) 57 58 t.Log("Close the db to release the lock") 59 err = w.Close() 60 require.NoError(t, err) 61 err = s.Close() 62 require.NoError(t, err) 63 sNeedsClose = false 64 65 t.Log("Read from another device") 66 config2 := libkbfs.ConfigAsUser(config, "user1") 67 defer libkbfs.CheckConfigAndShutdown(ctx, t, config2) 68 fs2, err := libfs.NewFS( 69 ctx, config2, h, data.MasterBranch, "", "", keybase1.MDPriorityNormal) 70 require.NoError(t, err) 71 s2, err := newBleveLevelDBStore(fs2, false, nil) 72 require.NoError(t, err) 73 defer func() { 74 err := s2.Close() 75 require.NoError(t, err) 76 }() 77 r, err := s2.Reader() 78 require.NoError(t, err) 79 defer func() { 80 err := r.Close() 81 require.NoError(t, err) 82 }() 83 84 gotVal1, err := r.Get(key1) 85 require.NoError(t, err) 86 require.Equal(t, val1, gotVal1) 87 gotVal2, err := r.Get(key2) 88 require.NoError(t, err) 89 require.Equal(t, val2, gotVal2) 90 gotVal3, err := r.Get(key3) 91 require.NoError(t, err) 92 require.Equal(t, val3, gotVal3) 93 94 t.Log("Check the iterator, should see two keys") 95 i := r.PrefixIterator([]byte("k")) 96 k1 := i.Key() 97 require.Equal(t, key1, k1) 98 v1 := i.Value() 99 require.Equal(t, val1, v1) 100 require.True(t, i.Valid()) 101 i.Next() 102 k2 := i.Key() 103 require.Equal(t, key2, k2) 104 v2 := i.Value() 105 require.Equal(t, val2, v2) 106 require.True(t, i.Valid()) 107 i.Next() 108 require.False(t, i.Valid()) 109 }