(t *testing.T)
| 1047 | } |
| 1048 | |
| 1049 | func TestKeyValueListKeys(t *testing.T) { |
| 1050 | s := RunBasicJetStreamServer() |
| 1051 | defer shutdownJSServerAndRemoveStorage(t, s) |
| 1052 | |
| 1053 | nc, js := jsClient(t, s) |
| 1054 | defer nc.Close() |
| 1055 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1056 | defer cancel() |
| 1057 | |
| 1058 | kv, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: "KVS", History: 2}) |
| 1059 | expectOk(t, err) |
| 1060 | |
| 1061 | put := func(key, value string) { |
| 1062 | t.Helper() |
| 1063 | _, err := kv.Put(ctx, key, []byte(value)) |
| 1064 | expectOk(t, err) |
| 1065 | } |
| 1066 | |
| 1067 | // Put in a few names and ages. |
| 1068 | put("name", "derek") |
| 1069 | put("age", "22") |
| 1070 | put("country", "US") |
| 1071 | put("name", "ivan") |
| 1072 | put("age", "33") |
| 1073 | put("country", "US") |
| 1074 | put("name", "rip") |
| 1075 | put("age", "44") |
| 1076 | put("country", "MT") |
| 1077 | |
| 1078 | keys, err := kv.ListKeys(ctx) |
| 1079 | expectOk(t, err) |
| 1080 | |
| 1081 | kmap := make(map[string]struct{}) |
| 1082 | for key := range keys.Keys() { |
| 1083 | if _, ok := kmap[key]; ok { |
| 1084 | t.Fatalf("Already saw %q", key) |
| 1085 | } |
| 1086 | kmap[key] = struct{}{} |
| 1087 | } |
| 1088 | if len(kmap) != 3 { |
| 1089 | t.Fatalf("Expected 3 total keys, got %d", len(kmap)) |
| 1090 | } |
| 1091 | expected := map[string]struct{}{ |
| 1092 | "name": struct{}{}, |
| 1093 | "age": struct{}{}, |
| 1094 | "country": struct{}{}, |
| 1095 | } |
| 1096 | if !reflect.DeepEqual(kmap, expected) { |
| 1097 | t.Fatalf("Expected %+v but got %+v", expected, kmap) |
| 1098 | } |
| 1099 | // Make sure delete and purge do the right thing and not return the keys. |
| 1100 | err = kv.Delete(ctx, "name") |
| 1101 | expectOk(t, err) |
| 1102 | err = kv.Purge(ctx, "country") |
| 1103 | expectOk(t, err) |
| 1104 | |
| 1105 | keys, err = kv.ListKeys(ctx) |
| 1106 | expectOk(t, err) |
nothing calls this directly
no test coverage detected