Debian Codegraph @

micro/test/testservice_helper_test.go

raw
1 // Copyright 2026 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package micro_test
15
16 import (
17 "fmt"
18 "os"
19 "strings"
20 "sync"
21 "testing"
22
23 "github.com/nats-io/nats.go"
24 testservice "github.com/synadia-io/orbit.go/ntf-client"
25 )
26
27 // testerProbe caches a one-time reachability check of the tester service so
28 // that when it is down, every test fails fast with one actionable message
29 // instead of its own raw dial error.
30 var testerProbe struct {
31 once sync.Once
32 err error
33 }
34
35 // testerURL is set by TestMain: TESTER_NATS_URL when set, otherwise the
36 // in-process tester it started.
37 var testerURL string
38
39 // newTester returns a tester Client connected to the tester service. Close is
40 // registered with t.Cleanup.
41 func newTester(t testing.TB) *testservice.Client {
42 t.Helper()
43 testerProbe.once.Do(func() {
44 nc, err := nats.Connect(testerURL)
45 if err != nil {
46 testerProbe.err = fmt.Errorf("cannot reach the tester at %s (unset TESTER_NATS_URL to use the in-process one, or start it with 'make tester-up-host'): %w", testerURL, err)
47 return
48 }
49 fmt.Fprintf(os.Stderr, "tester at %s: nats-server %s\n", testerURL, nc.ConnectedServerVersion())
50 nc.Close()
51 })
52 if testerProbe.err != nil {
53 t.Fatal(testerProbe.err)
54 }
55 c := testservice.New(t, testerURL)
56 t.Cleanup(func() { c.Close(t) })
57 return c
58 }
59
60 // withServer creates a non-JetStream server and dials it. Cleanup via t.Cleanup.
61 func withServer(t *testing.T, fn func(*testing.T, *nats.Conn), opts ...testservice.CreateOption) {
62 t.Helper()
63 withServerInstance(t, func(t *testing.T, nc *nats.Conn, _ *testservice.Instance) {
64 fn(t, nc)
65 }, opts...)
66 }
67
68 // withServerInstance is withServer plus the *testservice.Instance.
69 func withServerInstance(t *testing.T, fn func(*testing.T, *nats.Conn, *testservice.Instance), opts ...testservice.CreateOption) {
70 t.Helper()
71 c := newTester(t)
72 inst := c.CreateServer(t, false, opts...)
73 t.Cleanup(func() { inst.Destroy(t) })
74
75 nc := dialInstance(t, inst)
76 fn(t, nc, inst)
77 }
78
79 // dialInstance returns a connection that lists every server URL in inst, so
80 // reconnect survives any single node going down. nats.MaxReconnects(-1) is
81 // always set; additional connect options (e.g. credentials) may be passed.
82 // Accepts testing.TB so benchmarks can share the helper.
83 func dialInstance(t testing.TB, inst *testservice.Instance, opts ...nats.Option) *nats.Conn {
84 t.Helper()
85 urls := make([]string, len(inst.Servers))
86 for i, s := range inst.Servers {
87 urls[i] = s.URL
88 }
89 connectOpts := append([]nats.Option{nats.MaxReconnects(-1)}, opts...)
90 nc, err := nats.Connect(strings.Join(urls, ","), connectOpts...)
91 if err != nil {
92 t.Fatalf("nats.Connect: %v", err)
93 }
94 t.Cleanup(nc.Close)
95 return nc
96 }