github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/search/doc_db_test.go (about)

     1  // Copyright 2020 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 TestDocDb(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  	db, err := newDocDb(config, fs)
    33  	require.NoError(t, err)
    34  	dbNeedsShutdown := true
    35  	defer func() {
    36  		if dbNeedsShutdown {
    37  			db.Shutdown(ctx)
    38  		}
    39  	}()
    40  
    41  	t.Log("Put some stuff into the db")
    42  	d1 := "1"
    43  	n1 := "a"
    44  	d2 := "2"
    45  	n2 := "b"
    46  	d3 := "3"
    47  	n3 := "b"
    48  	err = db.Put(ctx, d1, "", n1)
    49  	require.NoError(t, err)
    50  	err = db.Put(ctx, d2, d1, n2)
    51  	require.NoError(t, err)
    52  	err = db.Put(ctx, d3, d1, n3)
    53  	require.NoError(t, err)
    54  
    55  	t.Log("Close the db to release the lock")
    56  	db.Shutdown(ctx)
    57  	dbNeedsShutdown = false
    58  
    59  	t.Log("Read from another device")
    60  	config2 := libkbfs.ConfigAsUser(config, "user1")
    61  	defer libkbfs.CheckConfigAndShutdown(ctx, t, config2)
    62  	fs2, err := libfs.NewFS(
    63  		ctx, config2, h, data.MasterBranch, "", "", keybase1.MDPriorityNormal)
    64  	require.NoError(t, err)
    65  	db2, err := newDocDb(config, fs2)
    66  	require.NoError(t, err)
    67  	defer db2.Shutdown(ctx)
    68  
    69  	gotP1, gotN1, err := db2.Get(ctx, d1)
    70  	require.NoError(t, err)
    71  	require.Equal(t, "", gotP1)
    72  	require.Equal(t, n1, gotN1)
    73  	gotP2, gotN2, err := db2.Get(ctx, d2)
    74  	require.NoError(t, err)
    75  	require.Equal(t, d1, gotP2)
    76  	require.Equal(t, n2, gotN2)
    77  	gotP3, gotN3, err := db2.Get(ctx, d3)
    78  	require.NoError(t, err)
    79  	require.Equal(t, d1, gotP3)
    80  	require.Equal(t, n3, gotN3)
    81  }