| 1 | // Copyright 2022-2023 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 testscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/ |
| 15 | |
| 16 | import ( |
| 17 | "contextscip-go gomod github.com/golang/go/src go1.25.0 context/" |
| 18 | "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 19 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 20 | "netscip-go gomod github.com/golang/go/src go1.25.0 net/" |
| 21 | "net/urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/" |
| 22 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 23 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 24 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 25 | "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/" |
| 26 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 27 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 28 | |
| 29 | "github.com/nats-io/nats-server/v2/serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/" |
| 30 | "github.com/nats-io/nats.goscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/" |
| 31 | "github.com/nats-io/nats.go/jetstreamscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream`/" |
| 32 | |
| 33 | natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/ "github.com/nats-io/nats-server/v2/testscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/" |
| 34 | ) |
| 35 | |
| 36 | type jsServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer# struct { |
| 37 | *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Serverscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#Server. |
| 38 | myoptsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#myopts. *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Options |
| 39 | restartscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#restart. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex# |
| 40 | } |
| 41 | |
| 42 | // Restart can be used to start again a server |
| 43 | // using the same listen address as before. |
| 44 | func (srv *jsServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#) Restartscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#Restart().() { |
| 45 | srv.restartscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#restart..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 46 | defer srv.restartscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#restart..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 47 | srv.Serverscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#Server. = natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.RunServer(srv.myoptsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#myopts.) |
| 48 | } |
| 49 | |
| 50 | // Dumb wait program to sync on callbacks, etc... Will timeout |
| 51 | func Waitscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/Wait().(ch chan bool) error { |
| 52 | return WaitTimescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/WaitTime().(ch, 5*timescip-go gomod github.com/golang/go/src go1.25.0 time/.Secondscip-go gomod github.com/golang/go/src go1.25.0 time/Second.) |
| 53 | } |
| 54 | |
| 55 | // Wait for a chan with a timeout. |
| 56 | func WaitTimescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/WaitTime().(ch chan bool, timeout timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#) error { |
| 57 | select { |
| 58 | case <-ch: |
| 59 | return nil |
| 60 | case <-timescip-go gomod github.com/golang/go/src go1.25.0 time/.Afterscip-go gomod github.com/golang/go/src go1.25.0 time/After().(timeout): |
| 61 | } |
| 62 | return errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("timeout") |
| 63 | } |
| 64 | |
| 65 | //////////////////////////////////////////////////////////////////////////////// |
| 66 | // Creating client connections |
| 67 | //////////////////////////////////////////////////////////////////////////////// |
| 68 | |
| 69 | // NewDefaultConnection |
| 70 | func NewDefaultConnectionscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/NewDefaultConnection().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) *natsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/.Connscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/Conn# { |
| 71 | return NewConnectionscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/NewConnection().(t, natsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/.DefaultPortscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/DefaultPort.) |
| 72 | } |
| 73 | |
| 74 | // NewConnection forms connection on a given port. |
| 75 | func NewConnectionscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/NewConnection().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, port int) *natsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/.Connscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/Conn# { |
| 76 | url := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("nats://127.0.0.1:%d", port) |
| 77 | nc, err := natsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/.Connectscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/Connect().(url) |
| 78 | if err != nil { |
| 79 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Failed to create default connection: %v\n", err) |
| 80 | return nil |
| 81 | } |
| 82 | return nc |
| 83 | } |
| 84 | |
| 85 | //////////////////////////////////////////////////////////////////////////////// |
| 86 | // Running nats server in separate Go routines |
| 87 | //////////////////////////////////////////////////////////////////////////////// |
| 88 | |
| 89 | // RunDefaultServer will run a server on the default port. |
| 90 | func RunDefaultServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/RunDefaultServer().() *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Server { |
| 91 | return RunServerOnPortscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/RunServerOnPort().(natsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/.DefaultPortscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/DefaultPort.) |
| 92 | } |
| 93 | |
| 94 | // RunServerOnPort will run a server on the given port. |
| 95 | func RunServerOnPortscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/RunServerOnPort().(port int) *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Server { |
| 96 | opts := natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.DefaultTestOptions |
| 97 | opts.Port = port |
| 98 | opts.Cluster.Name = "testing" |
| 99 | return RunServerWithOptionsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/RunServerWithOptions().(opts) |
| 100 | } |
| 101 | |
| 102 | // RunServerWithOptions will run a server with the given options. |
| 103 | func RunServerWithOptionsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/RunServerWithOptions().(opts serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Options) *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Server { |
| 104 | return natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.RunServer(&opts) |
| 105 | } |
| 106 | |
| 107 | // RunServerWithConfig will run a server with the given configuration file. |
| 108 | func RunServerWithConfigscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/RunServerWithConfig().(configFile string) (*serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Server, *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Options) { |
| 109 | return natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.RunServerWithConfig(configFile) |
| 110 | } |
| 111 | |
| 112 | func RunBasicJetStreamServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/RunBasicJetStreamServer().() *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Server { |
| 113 | opts := natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.DefaultTestOptions |
| 114 | opts.Port = -1 |
| 115 | opts.JetStream = true |
| 116 | return RunServerWithOptionsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/RunServerWithOptions().(opts) |
| 117 | } |
| 118 | |
| 119 | func createConfFilescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/createConfFile().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, content []byte) string { |
| 120 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 121 | conf, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.CreateTempscip-go gomod github.com/golang/go/src go1.25.0 os/CreateTemp().("", "") |
| 122 | if err != nil { |
| 123 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Error creating conf file: %v", err) |
| 124 | } |
| 125 | fName := conf.Namescip-go gomod github.com/golang/go/src go1.25.0 os/File#Name().() |
| 126 | if err := conf.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().(); err != nil { |
| 127 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Unexpected error: %v", err) |
| 128 | } |
| 129 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(fName, content, 0666); err != nil { |
| 130 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Removescip-go gomod github.com/golang/go/src go1.25.0 os/Remove().(fName); err != nil { |
| 131 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Unexpected error: %v", err) |
| 132 | } |
| 133 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Error writing conf file: %v", err) |
| 134 | } |
| 135 | return fName |
| 136 | } |
| 137 | |
| 138 | func shutdownJSServerAndRemoveStoragescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/shutdownJSServerAndRemoveStorage().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, s *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Server) { |
| 139 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 140 | var sd string |
| 141 | if config := s.JetStreamConfig(); config != nil { |
| 142 | sd = config.StoreDir |
| 143 | } |
| 144 | s.Shutdown() |
| 145 | if sd != "" { |
| 146 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(sd); err != nil { |
| 147 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Unable to remove storage %q: %v", sd, err) |
| 148 | } |
| 149 | } |
| 150 | s.WaitForShutdown() |
| 151 | } |
| 152 | |
| 153 | func setupJSClusterWithSizescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/setupJSClusterWithSize().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, clusterName string, size int) []*jsServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer# { |
| 154 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 155 | nodes := make([]*jsServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#, size) |
| 156 | opts := make([]*serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Options, 0) |
| 157 | |
| 158 | var activeListeners []netscip-go gomod github.com/golang/go/src go1.25.0 net/.Listenerscip-go gomod github.com/golang/go/src go1.25.0 net/Listener# |
| 159 | getAddr := func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) (string, string, int) { |
| 160 | l, err := netscip-go gomod github.com/golang/go/src go1.25.0 net/.Listenscip-go gomod github.com/golang/go/src go1.25.0 net/Listen().("tcp", "127.0.0.1:0") |
| 161 | if err != nil { |
| 162 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Unexpected error: %v", err) |
| 163 | } |
| 164 | |
| 165 | addr := l.Addrscip-go gomod github.com/golang/go/src go1.25.0 net/Listener#Addr().() |
| 166 | host := addr.(*netscip-go gomod github.com/golang/go/src go1.25.0 net/.TCPAddrscip-go gomod github.com/golang/go/src go1.25.0 net/TCPAddr#).IPscip-go gomod github.com/golang/go/src go1.25.0 net/TCPAddr#IP..Stringscip-go gomod github.com/golang/go/src go1.25.0 net/IP#String().() |
| 167 | port := addr.(*netscip-go gomod github.com/golang/go/src go1.25.0 net/.TCPAddrscip-go gomod github.com/golang/go/src go1.25.0 net/TCPAddr#).Portscip-go gomod github.com/golang/go/src go1.25.0 net/TCPAddr#Port. |
| 168 | timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sleepscip-go gomod github.com/golang/go/src go1.25.0 time/Sleep().(100 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond.) |
| 169 | |
| 170 | // we cannot close the listener immediately to avoid duplicate port binding |
| 171 | // the returned net.Listener has to be closed after all ports are drawn |
| 172 | activeListeners = append(activeListeners, l) |
| 173 | return addr.Stringscip-go gomod github.com/golang/go/src go1.25.0 net/Addr#String().(), host, port |
| 174 | } |
| 175 | |
| 176 | routes := []string{} |
| 177 | for i := 0; i < size; i++ { |
| 178 | o := natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.DefaultTestOptions |
| 179 | o.JetStream = true |
| 180 | o.ServerName = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("NODE_%d", i) |
| 181 | tdir, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirTempscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirTemp().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.TempDirscip-go gomod github.com/golang/go/src go1.25.0 os/TempDir().(), fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s_%s-", o.ServerName, clusterName)) |
| 182 | if err != nil { |
| 183 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 184 | } |
| 185 | o.StoreDir = tdir |
| 186 | |
| 187 | if size > 1 { |
| 188 | o.Cluster.Name = clusterName |
| 189 | _, host1, port1 := getAddr(t) |
| 190 | o.Host = host1 |
| 191 | o.Port = port1 |
| 192 | |
| 193 | addr2, host2, port2 := getAddr(t) |
| 194 | o.Cluster.Host = host2 |
| 195 | o.Cluster.Port = port2 |
| 196 | o.Tags = []string{o.ServerName} |
| 197 | routes = append(routes, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("nats://%s", addr2)) |
| 198 | } |
| 199 | opts = append(opts, &o) |
| 200 | } |
| 201 | // close all connections used to randomize ports |
| 202 | for _, l := range activeListeners { |
| 203 | l.Closescip-go gomod github.com/golang/go/src go1.25.0 net/Listener#Close().() |
| 204 | } |
| 205 | |
| 206 | if size > 1 { |
| 207 | routesStr := serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.RoutesFromStr(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(routes, ",")) |
| 208 | |
| 209 | for i, o := range opts { |
| 210 | o.Routes = routesStr |
| 211 | nodes[i] = &jsServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#{Serverscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#Server.: natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.RunServer(o), myoptsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#myopts.: o} |
| 212 | } |
| 213 | } else { |
| 214 | o := opts[0] |
| 215 | nodes[0] = &jsServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#{Serverscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#Server.: natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.RunServer(o), myoptsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#myopts.: o} |
| 216 | } |
| 217 | |
| 218 | // Wait until JS is ready. |
| 219 | srvA := nodes[0] |
| 220 | nc, err := natsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/.Connectscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/Connect().(srvA.ClientURL()) |
| 221 | if err != nil { |
| 222 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(err) |
| 223 | } |
| 224 | waitForJSReadyscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/waitForJSReady().(t, nc) |
| 225 | nc.Closescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/Conn#Close().() |
| 226 | |
| 227 | return nodes |
| 228 | } |
| 229 | |
| 230 | func waitForJSReadyscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/waitForJSReady().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, nc *natsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/.Connscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/Conn#) { |
| 231 | var err error |
| 232 | timeout := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().().Addscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Add().(10 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Secondscip-go gomod github.com/golang/go/src go1.25.0 time/Second.) |
| 233 | for timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().().Beforescip-go gomod github.com/golang/go/src go1.25.0 time/Time#Before().(timeout) { |
| 234 | // Use a smaller MaxWait here since if it fails, we don't want |
| 235 | // to wait for too long since we are going to try again. |
| 236 | js, err := nc.JetStreamscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/Conn#JetStream().(natsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/.MaxWaitscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/MaxWait#(250 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond.)) |
| 237 | if err != nil { |
| 238 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 239 | } |
| 240 | _, err = js.AccountInfoscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/JetStreamManager#AccountInfo.() |
| 241 | if err != nil { |
| 242 | continue |
| 243 | } |
| 244 | return |
| 245 | } |
| 246 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Timeout waiting for JS to be ready: %v", err) |
| 247 | } |
| 248 | |
| 249 | func withJSClusterAndStreamscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/withJSClusterAndStream().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, clusterName string, size int, stream jetstreamscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream`/.StreamConfigscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream`/StreamConfig#, tfn func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, subject string, srvs ...*jsServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#)) { |
| 250 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 251 | |
| 252 | withJSClusterscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/withJSCluster().(t, clusterName, size, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, nodes ...*jsServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#) { |
| 253 | srvA := nodes[0] |
| 254 | nc, err := natsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/.Connectscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/Connect().(srvA.ClientURL()) |
| 255 | if err != nil { |
| 256 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(err) |
| 257 | } |
| 258 | defer nc.Closescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/Conn#Close().() |
| 259 | |
| 260 | ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.25.0 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.25.0 context/WithTimeout().(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.0 context/Background().(), 10*timescip-go gomod github.com/golang/go/src go1.25.0 time/.Secondscip-go gomod github.com/golang/go/src go1.25.0 time/Second.) |
| 261 | defer cancel() |
| 262 | jsm, err := jetstreamscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream`/.Newscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream`/New().(nc) |
| 263 | if err != nil { |
| 264 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 265 | } |
| 266 | CreateStream: |
| 267 | for { |
| 268 | select { |
| 269 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.25.0 context/Context#Done().(): |
| 270 | if err != nil { |
| 271 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Unexpected error creating stream: %v", err) |
| 272 | } |
| 273 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Unable to create stream on cluster") |
| 274 | case <-timescip-go gomod github.com/golang/go/src go1.25.0 time/.Afterscip-go gomod github.com/golang/go/src go1.25.0 time/After().(500 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond.): |
| 275 | _, err = jsm.AccountInfoscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream`/JetStream#AccountInfo.(ctx) |
| 276 | if err != nil { |
| 277 | // Backoff for a bit until cluster and resources are ready. |
| 278 | timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sleepscip-go gomod github.com/golang/go/src go1.25.0 time/Sleep().(500 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond.) |
| 279 | } |
| 280 | _, err = jsm.CreateStreamscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream`/StreamManager#CreateStream.(ctx, stream) |
| 281 | if err != nil { |
| 282 | continue CreateStream |
| 283 | } |
| 284 | break CreateStream |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | tfn(t, stream.Namescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream`/StreamConfig#Name., nodes...) |
| 289 | }) |
| 290 | } |
| 291 | |
| 292 | func withJSClusterscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/withJSCluster().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, clusterName string, size int, tfn func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, srvs ...*jsServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#)) { |
| 293 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 294 | |
| 295 | nodes := setupJSClusterWithSizescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/setupJSClusterWithSize().(t, clusterName, size) |
| 296 | defer func() { |
| 297 | // Ensure that they get shutdown and remove their state. |
| 298 | for _, node := range nodes { |
| 299 | node.restartscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#restart..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 300 | shutdownJSServerAndRemoveStoragescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/shutdownJSServerAndRemoveStorage().(t, node.Serverscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#Server.) |
| 301 | node.restartscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/jsServer#restart..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 302 | } |
| 303 | }() |
| 304 | tfn(t, nodes...) |
| 305 | } |
| 306 | |
| 307 | func restartBasicJSServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/restartBasicJSServer().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, s *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Server) *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Server { |
| 308 | opts := natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.DefaultTestOptions |
| 309 | clientURL, err := urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.25.0 `net/url`/Parse().(s.ClientURL()) |
| 310 | if err != nil { |
| 311 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Unexpected error: %v", err) |
| 312 | } |
| 313 | port, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(clientURL.Portscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/URL#Port().()) |
| 314 | if err != nil { |
| 315 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Unexpected error: %v", err) |
| 316 | } |
| 317 | opts.Port = port |
| 318 | opts.JetStream = true |
| 319 | opts.StoreDir = s.JetStreamConfig().StoreDir |
| 320 | s.Shutdown() |
| 321 | s.WaitForShutdown() |
| 322 | return RunServerWithOptionsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/RunServerWithOptions().(opts) |
| 323 | } |
| 324 | |
| 325 | func checkForscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/jetstream/test`/checkFor().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, totalWait, sleepDur timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#, f func() error) { |
| 326 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 327 | timeout := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().().Addscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Add().(totalWait) |
| 328 | var err error |
| 329 | for timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().().Beforescip-go gomod github.com/golang/go/src go1.25.0 time/Time#Before().(timeout) { |
| 330 | err = f() |
| 331 | if err == nil { |
| 332 | return |
| 333 | } |
| 334 | timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sleepscip-go gomod github.com/golang/go/src go1.25.0 time/Sleep().(sleepDur) |
| 335 | } |
| 336 | if err != nil { |
| 337 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err.Error()) |
| 338 | } |
| 339 | } |