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

Function TestKeyValueCreate

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

Source from the content-addressed store, hash-verified

1589}
1590
1591func TestKeyValueCreate(t *testing.T) {
1592 s := RunBasicJetStreamServer()
1593 defer shutdownJSServerAndRemoveStorage(t, s)
1594
1595 nc, js := jsClient(t, s)
1596 defer nc.Close()
1597
1598 kv, err := js.CreateKeyValue(&nats.KeyValueConfig{
1599 Bucket: "TEST",
1600 Description: "Test KV",
1601 MaxValueSize: 128,
1602 History: 10,
1603 TTL: 1 * time.Hour,
1604 MaxBytes: 1024,
1605 Storage: nats.FileStorage,
1606 })
1607 if err != nil {
1608 t.Fatalf("Error creating kv: %v", err)
1609 }
1610
1611 expectedStreamConfig := nats.StreamConfig{
1612 Name: "KV_TEST",
1613 Description: "Test KV",
1614 Subjects: []string{"$KV.TEST.>"},
1615 MaxMsgs: -1,
1616 MaxBytes: 1024,
1617 Discard: nats.DiscardNew,
1618 MaxAge: 1 * time.Hour,
1619 MaxMsgsPerSubject: 10,
1620 MaxMsgSize: 128,
1621 Storage: nats.FileStorage,
1622 DenyDelete: true,
1623 AllowRollup: true,
1624 AllowDirect: true,
1625 MaxConsumers: -1,
1626 Replicas: 1,
1627 Duplicates: 2 * time.Minute,
1628 }
1629
1630 si, err := js.StreamInfo("KV_TEST")
1631 if err != nil {
1632 t.Fatalf("Error getting stream info: %v", err)
1633 }
1634 // Metadata is set by the server, so we need to set it here.
1635 expectedStreamConfig.Metadata = si.Config.Metadata
1636 if !reflect.DeepEqual(si.Config, expectedStreamConfig) {
1637 t.Fatalf("Expected stream config to be %+v, got %+v", expectedStreamConfig, si.Config)
1638 }
1639
1640 _, err = kv.Create("key", []byte("1"))
1641 if err != nil {
1642 t.Fatalf("Error creating key: %v", err)
1643 }
1644
1645 _, err = kv.Create("key", []byte("1"))
1646 expected := "nats: wrong last sequence: 1: key exists"
1647 if err.Error() != expected {
1648 t.Fatalf("Expected %q, got: %v", expected, err)

Callers

nothing calls this directly

Calls 12

APIErrorMethod · 0.95
FatalfMethod · 0.80
RunBasicJetStreamServerFunction · 0.70
jsClientFunction · 0.70
CreateKeyValueMethod · 0.65
StreamInfoMethod · 0.65
CreateMethod · 0.65
ErrorMethod · 0.65
APIErrorMethod · 0.65
CloseMethod · 0.45
IsMethod · 0.45

Tested by

no test coverage detected