From a20951ca7ffd937197e066299ab70c86b3ad4b89 Mon Sep 17 00:00:00 2001 From: Jeffrey Picard Date: Sat, 11 Dec 2021 19:24:37 -0500 Subject: [PATCH] fixes and arg test --- db/db.go | 8 +++++--- db/db_test.go | 10 +++++----- server/args_test.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 server/args_test.go diff --git a/db/db.go b/db/db.go index 4d169a3..14748c1 100644 --- a/db/db.go +++ b/db/db.go @@ -117,6 +117,7 @@ func (pr *PrefixRow) Iter(options *IterOptions) <-chan *PrefixRowKV { var prevKey []byte = nil go func() { + defer it.Close() for ; terminateFunc(prevKey); it.Next() { key := it.Key() prevKey = key.Data() @@ -260,11 +261,12 @@ func OpenDB(name string) int { // Read db opts := grocksdb.NewDefaultOptions() db, err := grocksdb.OpenDb(opts, name) - ro := grocksdb.NewDefaultReadOptions() - ro.SetFillCache(false) if err != nil { log.Println(err) } + defer db.Close() + ro := grocksdb.NewDefaultReadOptions() + ro.SetFillCache(false) log.Println(db.Name()) @@ -277,7 +279,7 @@ func OpenDB(name string) int { key := it.Key() value := it.Value() - fmt.Printf("Key: %v Value: %v\n", key.Data(), value.Data()) + fmt.Printf("Key: %v Value: %v\n", hex.EncodeToString(key.Data()), hex.EncodeToString(value.Data())) key.Free() value.Free() diff --git a/db/db_test.go b/db/db_test.go index 41cee3b..28f9e9b 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -27,6 +27,7 @@ func TestReadUTXO2(t *testing.T) { if err != nil { t.Errorf("err not nil: %+v\n", err) } + defer db.Close() utxoRow := &PrefixRow{ // KeyStruct: UTXOKey{}, // ValueStruct: UTXOValue{}, @@ -62,9 +63,6 @@ func TestReadUTXO2(t *testing.T) { t.Errorf("got: %d, want: %d\n", got, tt.want) } i++ - if i >= 10 { - break - } } }) } @@ -89,6 +87,8 @@ func TestReadUTXO(t *testing.T) { if err != nil { t.Errorf("err not nil: %+v\n", err) } + defer db.Close() + data := ReadPrefixN(db, prefixes.UTXO, tt.want) got := len(data) @@ -147,13 +147,13 @@ func TestUTXOKey_String(t *testing.T) { hashx: []byte("AAAAAAAAAA"), txnum: 0, nout: 0, - want: "db.UTXOKey(hashX=41414141414141414141, tx_num=0, nout=0)", + want: "*db.UTXOKey(hashX=41414141414141414141, tx_num=0, nout=0)", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - key := UTXOKey{ + key := &UTXOKey{ Prefix: tt.prefix, HashX: tt.hashx, TxNum: tt.txnum, diff --git a/server/args_test.go b/server/args_test.go new file mode 100644 index 0000000..5e60b33 --- /dev/null +++ b/server/args_test.go @@ -0,0 +1,35 @@ +package server + +import ( + "os" + "testing" + + pb "github.com/lbryio/hub/protobuf/go" +) + +// TestParseArgs +func TestParseArgs(t *testing.T) { + + tests := []struct { + name string + want bool + }{ + { + name: "Correctly disables elastic search", + want: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + os.Args = []string{"serve", "--disable-es"} + searchRequest := new(pb.SearchRequest) + args := ParseArgs(searchRequest) + got := args.DisableEs + if got != tt.want { + t.Errorf("flags: got: %v, want: %v\n", got, tt.want) + } + }) + } + +}