//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") } // Design 47 H2 ยง800, H-A-368: the setup bound is half of the level's bound, 30 s when the level has none, and a starved // setup fails with the one line a build module reads. t.Run("the setup bound is half the level's and a starved setup says so", func(t *testing.T) { for _, c := range []struct { level string want time.Duration fails bool }{{"", 30 * time.Second, false}, {"10m", 5 * time.Minute, false}, {"ten", 0, true}, {"-1s", 0, true}} { t.Setenv(levelBoundWord, c.level) f := &failed{TB: t} var got time.Duration catch(f, func() { got = SetupBound(f) }) if c.fails != (f.words != "") || (!c.fails && got != c.want) { t.Errorf("level %q: bound %s, failed %q", c.level, got, f.words) } } f := &failed{TB: t} catch(f, func() { Starved(f, "the stream did not answer within 5s") }) if f.words != "mesh: setup starved: the stream did not answer within 5s" { t.Errorf("a starved setup said %q", f.words) } }) } // 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) } }