| 1 | // Copyright 2015-2019 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/test`/ |
| 15 | |
| 16 | import ( |
| 17 | "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 19 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 20 | "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 21 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 22 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 23 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 24 | |
| 25 | "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`/" |
| 26 | "github.com/nats-io/nats.goscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go`/" |
| 27 | |
| 28 | 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`/" |
| 29 | ) |
| 30 | |
| 31 | const TEST_PORTscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/TEST_PORT. = 8368 |
| 32 | |
| 33 | // So that we can pass tests and benchmarks... |
| 34 | type tLoggerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/tLogger# interface { |
| 35 | Fatalfscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/tLogger#Fatalf.(format string, args ...any) |
| 36 | Errorfscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/tLogger#Errorf.(format string, args ...any) |
| 37 | } |
| 38 | |
| 39 | // TestLogger |
| 40 | type TestLoggerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/TestLogger# tLoggerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/tLogger# |
| 41 | |
| 42 | // Dumb wait program to sync on callbacks, etc... Will timeout |
| 43 | func Waitscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/Wait().(ch chan bool) error { |
| 44 | return WaitTimescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/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.) |
| 45 | } |
| 46 | |
| 47 | // Wait for a chan with a timeout. |
| 48 | func WaitTimescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/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 { |
| 49 | select { |
| 50 | case <-ch: |
| 51 | return nil |
| 52 | 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): |
| 53 | } |
| 54 | 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") |
| 55 | } |
| 56 | |
| 57 | func WaitOnChannelscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/WaitOnChannel().[T comparable](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#, ch <-chan T, expected T) { |
| 58 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 59 | select { |
| 60 | case s := <-ch: |
| 61 | if s != expected { |
| 62 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Expected result: %v; got: %v", expected, s) |
| 63 | } |
| 64 | 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().(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.): |
| 65 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Timeout waiting for result %v", expected) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func stackFatalfscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/stackFatalf().(t tLoggerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/tLogger#, f string, args ...any) { |
| 70 | lines := make([]string, 0, 32) |
| 71 | msg := 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().(f, args...) |
| 72 | lines = append(lines, msg) |
| 73 | |
| 74 | // Generate the Stack of callers: Skip us and verify* frames. |
| 75 | for i := 1; true; i++ { |
| 76 | _, file, line, ok := runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.Callerscip-go gomod github.com/golang/go/src go1.25.0 runtime/Caller().(i) |
| 77 | if !ok { |
| 78 | break |
| 79 | } |
| 80 | msg := 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().("%d - %s:%d", i, file, line) |
| 81 | lines = append(lines, msg) |
| 82 | } |
| 83 | t.Fatalfscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/tLogger#Fatalf.("%s", 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().(lines, "\n")) |
| 84 | } |
| 85 | |
| 86 | //////////////////////////////////////////////////////////////////////////////// |
| 87 | // Creating client connections |
| 88 | //////////////////////////////////////////////////////////////////////////////// |
| 89 | |
| 90 | // NewDefaultConnection |
| 91 | func NewDefaultConnectionscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/NewDefaultConnection().(t tLoggerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/tLogger#) *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# { |
| 92 | return NewConnectionscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/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.) |
| 93 | } |
| 94 | |
| 95 | // NewConnection forms connection on a given port. |
| 96 | func NewConnectionscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/NewConnection().(t tLoggerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/tLogger#, 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# { |
| 97 | 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) |
| 98 | 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) |
| 99 | if err != nil { |
| 100 | t.Fatalfscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/tLogger#Fatalf.("Failed to create default connection: %v\n", err) |
| 101 | return nil |
| 102 | } |
| 103 | return nc |
| 104 | } |
| 105 | |
| 106 | //////////////////////////////////////////////////////////////////////////////// |
| 107 | // Running nats server in separate Go routines |
| 108 | //////////////////////////////////////////////////////////////////////////////// |
| 109 | |
| 110 | // RunDefaultServer will run a server on the default port. |
| 111 | func RunDefaultServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/RunDefaultServer().() *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Server { |
| 112 | return RunServerOnPortscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/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.) |
| 113 | } |
| 114 | |
| 115 | // RunServerOnPort will run a server on the given port. |
| 116 | func RunServerOnPortscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/RunServerOnPort().(port int) *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Server { |
| 117 | opts := natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.DefaultTestOptions |
| 118 | opts.Port = port |
| 119 | opts.Cluster.Name = "testing" |
| 120 | return RunServerWithOptionsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/RunServerWithOptions().(&opts) |
| 121 | } |
| 122 | |
| 123 | // RunServerWithOptions will run a server with the given options. |
| 124 | func RunServerWithOptionsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/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 { |
| 125 | return natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.RunServer(opts) |
| 126 | } |
| 127 | |
| 128 | // RunServerWithConfig will run a server with the given configuration file. |
| 129 | func RunServerWithConfigscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/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) { |
| 130 | return natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.RunServerWithConfig(configFile) |
| 131 | } |
| 132 | |
| 133 | func RunBasicJetStreamServerscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/RunBasicJetStreamServer().() *serverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/server`/.Server { |
| 134 | opts := natsserverscip-go gomod github.com/golang/go/src go1.25.0 `github.com/nats-io/nats-server/v2/test`/.DefaultTestOptions |
| 135 | opts.Port = -1 |
| 136 | opts.JetStream = true |
| 137 | return RunServerWithOptionsscip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/test`/RunServerWithOptions().(&opts) |
| 138 | } |
| 139 | |
| 140 | func createConfFilescip-go gomod github.com/nats-io/nats.go . `github.com/nats-io/nats.go/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 { |
| 141 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 142 | 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().("", "") |
| 143 | if err != nil { |
| 144 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Error creating conf file: %v", err) |
| 145 | } |
| 146 | fName := conf.Namescip-go gomod github.com/golang/go/src go1.25.0 os/File#Name().() |
| 147 | conf.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().() |
| 148 | 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 { |
| 149 | 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) |
| 150 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Error writing conf file: %v", err) |
| 151 | } |
| 152 | return fName |
| 153 | } |