88 lines
2.1 KiB
Go
88 lines
2.1 KiB
Go
//go:build integration
|
|
|
|
package testbus
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
)
|
|
|
|
// 1175/AC3, 1171/AC1: a JetStream bus of this test alone answers a publish and a key-value create.
|
|
func TestBusAnswersPublishAndKeyValueCreate(t *testing.T) {
|
|
conn, err := nats.Connect(URL(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
js, err := jetstream.New(conn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
|
|
if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "EVENTS", Subjects: []string{"mesh.>"}}); err != nil {
|
|
t.Fatalf("the bus did not answer a stream create: %v", err)
|
|
}
|
|
ack, err := js.Publish(ctx, "mesh.test", []byte("hello"))
|
|
if err != nil {
|
|
t.Fatalf("the bus did not answer a publish: %v", err)
|
|
}
|
|
if ack.Stream != "EVENTS" {
|
|
t.Errorf("published into %q, want EVENTS", ack.Stream)
|
|
}
|
|
|
|
kv, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: "TESTKV"})
|
|
if err != nil {
|
|
t.Fatalf("the bus did not answer a key-value create: %v", err)
|
|
}
|
|
if _, err := kv.Put(ctx, "k", []byte("v")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entry, err := kv.Get(ctx, "k")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(entry.Value()) != "v" {
|
|
t.Errorf("read back %q, want %q", entry.Value(), "v")
|
|
}
|
|
}
|
|
|
|
// 1175/AC3, 1171/AC1: the bus is gone when the test that started it ends.
|
|
func TestBusGoneWhenItsTestEnds(t *testing.T) {
|
|
var url string
|
|
t.Run("one", func(t *testing.T) {
|
|
url = URL(t)
|
|
conn, err := nats.Connect(url)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conn.Close()
|
|
})
|
|
if _, err := nats.Connect(url, nats.Timeout(time.Second)); err == nil {
|
|
t.Fatal("the subtest's bus still answers now that the subtest has ended")
|
|
}
|
|
}
|
|
|
|
// 1175/AC3: two tests never share a bus.
|
|
func TestEveryTestHasABusOfItsOwn(t *testing.T) {
|
|
seen := map[string]bool{}
|
|
for i := 0; i < 2; i++ {
|
|
t.Run("one", func(t *testing.T) {
|
|
url := URL(t)
|
|
if seen[url] {
|
|
t.Fatalf("two tests were handed %s", url)
|
|
}
|
|
seen[url] = true
|
|
})
|
|
}
|
|
// Asked twice, one test reaches one bus.
|
|
if a, b := URL(t), URL(t); a != b {
|
|
t.Errorf("one test was handed two buses: %s and %s", a, b)
|
|
}
|
|
}
|