(t *testing.T)
| 1145 | } |
| 1146 | |
| 1147 | func TestAlwaysReconnectOnMaxConnectionsExceededErr(t *testing.T) { |
| 1148 | conf := createConfFile(t, []byte(` |
| 1149 | server_name: "A" |
| 1150 | listen: 127.0.0.1:-1 |
| 1151 | max_connections: 2 |
| 1152 | `)) |
| 1153 | defer os.Remove(conf) |
| 1154 | |
| 1155 | s, _ := RunServerWithConfig(conf) |
| 1156 | defer s.Shutdown() |
| 1157 | |
| 1158 | clientOpts := func(name string) ([]nats.Option, chan error, chan struct{}, chan struct{}) { |
| 1159 | disconnectCh := make(chan error, 10) |
| 1160 | reconnectCh := make(chan struct{}, 10) |
| 1161 | closedCh := make(chan struct{}, 10) |
| 1162 | return []nats.Option{ |
| 1163 | nats.MaxReconnects(-1), |
| 1164 | nats.ReconnectWait(10 * time.Millisecond), |
| 1165 | nats.Timeout(200 * time.Millisecond), |
| 1166 | nats.DisconnectErrHandler(func(c *nats.Conn, err error) { |
| 1167 | clientID, _ := c.GetClientID() |
| 1168 | t.Logf("[%v] Disconnected: %v: %v", name, clientID, err) |
| 1169 | if err != nil { |
| 1170 | disconnectCh <- err |
| 1171 | } |
| 1172 | }), |
| 1173 | nats.ReconnectHandler(func(c *nats.Conn) { |
| 1174 | clientID, _ := c.GetClientID() |
| 1175 | t.Logf("[%v] Reconnected: %v", name, clientID) |
| 1176 | reconnectCh <- struct{}{} |
| 1177 | }), |
| 1178 | nats.ClosedHandler(func(c *nats.Conn) { |
| 1179 | clientID, _ := c.GetClientID() |
| 1180 | t.Logf("[%v] Closed: %v", name, clientID) |
| 1181 | closedCh <- struct{}{} |
| 1182 | }), |
| 1183 | nats.Name(name), |
| 1184 | }, disconnectCh, reconnectCh, closedCh |
| 1185 | } |
| 1186 | |
| 1187 | opts1, errCh1, reconnectCh1, _ := clientOpts("A") |
| 1188 | nc1, err := nats.Connect(s.ClientURL(), opts1...) |
| 1189 | if err != nil { |
| 1190 | t.Fatalf("Failed to connect first client: %v", err) |
| 1191 | } |
| 1192 | defer nc1.Close() |
| 1193 | |
| 1194 | opts2, errCh2, reconnectCh2, _ := clientOpts("B") |
| 1195 | nc2, err := nats.Connect(s.ClientURL(), opts2...) |
| 1196 | if err != nil { |
| 1197 | t.Fatalf("Failed to connect second client: %v", err) |
| 1198 | } |
| 1199 | defer nc2.Close() |
| 1200 | |
| 1201 | if s.NumClients() != 2 { |
| 1202 | t.Fatalf("Expected 2 client connections to server. Got %d", s.NumClients()) |
| 1203 | } |
| 1204 | if err := nc1.Flush(); err != nil { |
nothing calls this directly
no test coverage detected