github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/kvstore/tx_test.go (about) 1 // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . 2 3 package kvstore_test 4 5 import ( 6 "fmt" 7 "github.com/TeaOSLab/EdgeNode/internal/utils/kvstore" 8 "testing" 9 ) 10 11 func TestTable_ReadTx(t *testing.T) { 12 store, err := kvstore.OpenStore("test") 13 if err != nil { 14 t.Fatal(err) 15 } 16 defer func() { 17 _ = store.Close() 18 }() 19 20 db, err := store.NewDB("TEST_DB") 21 if err != nil { 22 t.Fatal(err) 23 } 24 25 table, err := kvstore.NewCounterTable[uint64]("users_counter") 26 if err != nil { 27 t.Fatal(err) 28 } 29 30 db.AddTable(table) 31 32 err = table.WriteTx(func(tx *kvstore.Tx[uint64]) error { 33 for i := 0; i < 1000; i++ { 34 var key = fmt.Sprintf("a%03d", i) 35 setErr := tx.Set(key, uint64(i)) 36 if setErr != nil { 37 return setErr 38 } 39 40 value, getErr := tx.Get(key) 41 if getErr != nil { 42 return getErr 43 } 44 t.Log("write:", key, "=>", value) 45 } 46 return nil 47 }) 48 if err != nil { 49 t.Fatal(err) 50 } 51 52 err = table.ReadTx(func(tx *kvstore.Tx[uint64]) error { 53 for _, key := range []string{"a100", "a101", "a102"} { 54 value, getErr := tx.Get(key) 55 if getErr != nil { 56 return getErr 57 } 58 t.Log("read:", key, "=>", value) 59 } 60 return nil 61 }) 62 if err != nil { 63 t.Fatal(err) 64 } 65 }