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

Function TestKeyValueKeys

jetstream/test/kv_test.go:972–1047  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

970}
971
972func TestKeyValueKeys(t *testing.T) {
973 s := RunBasicJetStreamServer()
974 defer shutdownJSServerAndRemoveStorage(t, s)
975
976 nc, js := jsClient(t, s)
977 defer nc.Close()
978 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
979 defer cancel()
980
981 kv, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: "KVS", History: 2})
982 expectOk(t, err)
983
984 put := func(key, value string) {
985 t.Helper()
986 _, err := kv.Put(ctx, key, []byte(value))
987 expectOk(t, err)
988 }
989
990 _, err = kv.Keys(ctx)
991 expectErr(t, err, jetstream.ErrNoKeysFound)
992
993 // Put in a few names and ages.
994 put("name", "derek")
995 put("age", "22")
996 put("country", "US")
997 put("name", "ivan")
998 put("age", "33")
999 put("country", "US")
1000 put("name", "rip")
1001 put("age", "44")
1002 put("country", "MT")
1003
1004 keys, err := kv.Keys(ctx)
1005 expectOk(t, err)
1006
1007 kmap := make(map[string]struct{})
1008 for _, key := range keys {
1009 if _, ok := kmap[key]; ok {
1010 t.Fatalf("Already saw %q", key)
1011 }
1012 kmap[key] = struct{}{}
1013 }
1014 if len(kmap) != 3 {
1015 t.Fatalf("Expected 3 total keys, got %d", len(kmap))
1016 }
1017 expected := map[string]struct{}{
1018 "name": struct{}{},
1019 "age": struct{}{},
1020 "country": struct{}{},
1021 }
1022 if !reflect.DeepEqual(kmap, expected) {
1023 t.Fatalf("Expected %+v but got %+v", expected, kmap)
1024 }
1025 // Make sure delete and purge do the right thing and not return the keys.
1026 err = kv.Delete(ctx, "name")
1027 expectOk(t, err)
1028 err = kv.Purge(ctx, "country")
1029 expectOk(t, err)

Callers

nothing calls this directly

Calls 12

FatalfMethod · 0.80
RunBasicJetStreamServerFunction · 0.70
jsClientFunction · 0.70
expectOkFunction · 0.70
expectErrFunction · 0.70
CreateKeyValueMethod · 0.65
PutMethod · 0.65
KeysMethod · 0.65
DeleteMethod · 0.65
PurgeMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected