MCPcopy
hub / github.com/nats-io/nats.go / TestKeyValueListKeys

Function TestKeyValueListKeys

test/kv_test.go:866–986  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

864}
865
866func TestKeyValueListKeys(t *testing.T) {
867 s := RunBasicJetStreamServer()
868 defer shutdownJSServerAndRemoveStorage(t, s)
869
870 nc, js := jsClient(t, s)
871 defer nc.Close()
872
873 kv, err := js.CreateKeyValue(&nats.KeyValueConfig{Bucket: "KVS", History: 2})
874 expectOk(t, err)
875
876 put := func(key, value string) {
877 t.Helper()
878 _, err := kv.Put(key, []byte(value))
879 expectOk(t, err)
880 }
881
882 // Put in a few names and ages.
883 put("name", "derek")
884 put("age", "22")
885 put("country", "US")
886 put("name", "ivan")
887 put("age", "33")
888 put("country", "US")
889 put("name", "rip")
890 put("age", "44")
891 put("country", "MT")
892
893 keys, err := kv.ListKeys()
894 expectOk(t, err)
895
896 kmap := make(map[string]struct{})
897 for key := range keys.Keys() {
898 if _, ok := kmap[key]; ok {
899 t.Fatalf("Already saw %q", key)
900 }
901 kmap[key] = struct{}{}
902 }
903 if len(kmap) != 3 {
904 t.Fatalf("Expected 3 total keys, got %d", len(kmap))
905 }
906 expected := map[string]struct{}{
907 "name": struct{}{},
908 "age": struct{}{},
909 "country": struct{}{},
910 }
911 if !reflect.DeepEqual(kmap, expected) {
912 t.Fatalf("Expected %+v but got %+v", expected, kmap)
913 }
914 // Make sure delete and purge do the right thing and not return the keys.
915 err = kv.Delete("name")
916 expectOk(t, err)
917 err = kv.Purge("country")
918 expectOk(t, err)
919
920 keys, err = kv.ListKeys()
921 expectOk(t, err)
922
923 kmap = make(map[string]struct{})

Callers

nothing calls this directly

Calls 14

FatalfMethod · 0.80
RunBasicJetStreamServerFunction · 0.70
jsClientFunction · 0.70
expectOkFunction · 0.70
CreateKeyValueMethod · 0.65
PutMethod · 0.65
ListKeysMethod · 0.65
KeysMethod · 0.65
DeleteMethod · 0.65
PurgeMethod · 0.65
StopMethod · 0.65

Tested by

no test coverage detected