| 1 |
package proton_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/tls" |
| 6 |
"net" |
| 7 |
"net/http" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/henrybear327/go-proton-api" |
| 12 |
"github.com/henrybear327/go-proton-api/server" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
) |
| 15 |
|
| 16 |
func TestStatus(t *testing.T) { |
| 17 |
s := server.New() |
| 18 |
defer s.Close() |
| 19 |
|
| 20 |
ctl := proton.NewNetCtl() |
| 21 |
|
| 22 |
m := proton.New( |
| 23 |
proton.WithHostURL(s.GetHostURL()), |
| 24 |
proton.WithTransport(ctl.NewRoundTripper(&tls.Config{InsecureSkipVerify: true})), |
| 25 |
) |
| 26 |
defer m.Close() |
| 27 |
|
| 28 |
var ( |
| 29 |
called int |
| 30 |
status proton.Status |
| 31 |
) |
| 32 |
|
| 33 |
m.AddStatusObserver(func(val proton.Status) { |
| 34 |
called++ |
| 35 |
status = val |
| 36 |
}) |
| 37 |
|
| 38 |
// This should succeed. |
| 39 |
require.NoError(t, m.Ping(context.Background())) |
| 40 |
|
| 41 |
// Status should not have been called yet. |
| 42 |
require.Zero(t, called) |
| 43 |
|
| 44 |
// Now we simulate a network failure. |
| 45 |
ctl.Disable() |
| 46 |
|
| 47 |
// This should fail. |
| 48 |
require.Error(t, m.Ping(context.Background())) |
| 49 |
|
| 50 |
// Status should have been called once and status should indicate network is down. |
| 51 |
require.Equal(t, 1, called) |
| 52 |
require.Equal(t, proton.StatusDown, status) |
| 53 |
|
| 54 |
// Now we simulate a network restoration. |
| 55 |
ctl.Enable() |
| 56 |
|
| 57 |
// This should succeed. |
| 58 |
require.NoError(t, m.Ping(context.Background())) |
| 59 |
|
| 60 |
// Status should have been called twice and status should indicate network is up. |
| 61 |
require.Equal(t, 2, called) |
| 62 |
require.Equal(t, proton.StatusUp, status) |
| 63 |
} |
| 64 |
|
| 65 |
func TestStatus_NoDial(t *testing.T) { |
| 66 |
s := server.New() |
| 67 |
defer s.Close() |
| 68 |
|
| 69 |
ctl := proton.NewNetCtl() |
| 70 |
|
| 71 |
m := proton.New( |
| 72 |
proton.WithHostURL(s.GetHostURL()), |
| 73 |
proton.WithTransport(ctl.NewRoundTripper(&tls.Config{InsecureSkipVerify: true})), |
| 74 |
) |
| 75 |
defer m.Close() |
| 76 |
|
| 77 |
var ( |
| 78 |
called int |
| 79 |
status proton.Status |
| 80 |
) |
| 81 |
|
| 82 |
m.AddStatusObserver(func(val proton.Status) { |
| 83 |
called++ |
| 84 |
status = val |
| 85 |
}) |
| 86 |
|
| 87 |
// Disable dialing. |
| 88 |
ctl.SetCanDial(false) |
| 89 |
|
| 90 |
// This should fail. |
| 91 |
require.Error(t, m.Ping(context.Background())) |
| 92 |
|
| 93 |
// Status should have been called once and status should indicate network is down. |
| 94 |
require.Equal(t, 1, called) |
| 95 |
require.Equal(t, proton.StatusDown, status) |
| 96 |
} |
| 97 |
|
| 98 |
func TestStatus_NoRead(t *testing.T) { |
| 99 |
s := server.New() |
| 100 |
defer s.Close() |
| 101 |
|
| 102 |
ctl := proton.NewNetCtl() |
| 103 |
|
| 104 |
m := proton.New( |
| 105 |
proton.WithHostURL(s.GetHostURL()), |
| 106 |
proton.WithTransport(ctl.NewRoundTripper(&tls.Config{InsecureSkipVerify: true})), |
| 107 |
) |
| 108 |
defer m.Close() |
| 109 |
|
| 110 |
var ( |
| 111 |
called int |
| 112 |
status proton.Status |
| 113 |
) |
| 114 |
|
| 115 |
m.AddStatusObserver(func(val proton.Status) { |
| 116 |
called++ |
| 117 |
status = val |
| 118 |
}) |
| 119 |
|
| 120 |
// Disable reading. |
| 121 |
ctl.SetCanRead(false) |
| 122 |
|
| 123 |
// This should fail. |
| 124 |
require.Error(t, m.Ping(context.Background())) |
| 125 |
|
| 126 |
// Status should have been called once and status should indicate network is down. |
| 127 |
require.Equal(t, 1, called) |
| 128 |
require.Equal(t, proton.StatusDown, status) |
| 129 |
} |
| 130 |
|
| 131 |
func TestStatus_NoWrite(t *testing.T) { |
| 132 |
s := server.New() |
| 133 |
defer s.Close() |
| 134 |
|
| 135 |
ctl := proton.NewNetCtl() |
| 136 |
|
| 137 |
m := proton.New( |
| 138 |
proton.WithHostURL(s.GetHostURL()), |
| 139 |
proton.WithTransport(ctl.NewRoundTripper(&tls.Config{InsecureSkipVerify: true})), |
| 140 |
) |
| 141 |
defer m.Close() |
| 142 |
|
| 143 |
var ( |
| 144 |
called int |
| 145 |
status proton.Status |
| 146 |
) |
| 147 |
|
| 148 |
m.AddStatusObserver(func(val proton.Status) { |
| 149 |
called++ |
| 150 |
status = val |
| 151 |
}) |
| 152 |
|
| 153 |
// Disable writing. |
| 154 |
ctl.SetCanWrite(false) |
| 155 |
|
| 156 |
// This should fail. |
| 157 |
require.Error(t, m.Ping(context.Background())) |
| 158 |
|
| 159 |
// Status should have been called once and status should indicate network is down. |
| 160 |
require.Equal(t, 1, called) |
| 161 |
require.Equal(t, proton.StatusDown, status) |
| 162 |
} |
| 163 |
|
| 164 |
func TestStatus_NoReadExistingConn(t *testing.T) { |
| 165 |
s := server.New() |
| 166 |
defer s.Close() |
| 167 |
|
| 168 |
_, _, err := s.CreateUser("user", []byte("pass")) |
| 169 |
require.NoError(t, err) |
| 170 |
|
| 171 |
ctl := proton.NewNetCtl() |
| 172 |
|
| 173 |
var dialed int |
| 174 |
|
| 175 |
ctl.OnDial(func(net.Conn) { |
| 176 |
dialed++ |
| 177 |
}) |
| 178 |
|
| 179 |
m := proton.New( |
| 180 |
proton.WithHostURL(s.GetHostURL()), |
| 181 |
proton.WithTransport(ctl.NewRoundTripper(&tls.Config{InsecureSkipVerify: true})), |
| 182 |
) |
| 183 |
defer m.Close() |
| 184 |
|
| 185 |
// This should succeed. |
| 186 |
c, _, err := m.NewClientWithLogin(context.Background(), "user", []byte("pass")) |
| 187 |
require.NoError(t, err) |
| 188 |
defer c.Close() |
| 189 |
|
| 190 |
// We should have dialed once. |
| 191 |
require.Equal(t, 1, dialed) |
| 192 |
|
| 193 |
// Disable reading on the existing connection. |
| 194 |
ctl.SetCanRead(false) |
| 195 |
|
| 196 |
// This should fail because we won't be able to read the response. |
| 197 |
require.Error(t, getErr(c.GetUser(context.Background()))) |
| 198 |
} |
| 199 |
|
| 200 |
func TestStatus_NoWriteExistingConn(t *testing.T) { |
| 201 |
s := server.New() |
| 202 |
defer s.Close() |
| 203 |
|
| 204 |
_, _, err := s.CreateUser("user", []byte("pass")) |
| 205 |
require.NoError(t, err) |
| 206 |
|
| 207 |
ctl := proton.NewNetCtl() |
| 208 |
|
| 209 |
var dialed int |
| 210 |
|
| 211 |
ctl.OnDial(func(net.Conn) { |
| 212 |
dialed++ |
| 213 |
}) |
| 214 |
|
| 215 |
m := proton.New( |
| 216 |
proton.WithHostURL(s.GetHostURL()), |
| 217 |
proton.WithTransport(ctl.NewRoundTripper(&tls.Config{InsecureSkipVerify: true})), |
| 218 |
proton.WithRetryCount(0), |
| 219 |
) |
| 220 |
defer m.Close() |
| 221 |
|
| 222 |
// This should succeed. |
| 223 |
c, _, err := m.NewClientWithLogin(context.Background(), "user", []byte("pass")) |
| 224 |
require.NoError(t, err) |
| 225 |
defer c.Close() |
| 226 |
|
| 227 |
// We should have dialed once. |
| 228 |
require.Equal(t, 1, dialed) |
| 229 |
|
| 230 |
// Disable reading on the existing connection. |
| 231 |
ctl.SetCanWrite(false) |
| 232 |
|
| 233 |
// This should fail because we won't be able to write the request. |
| 234 |
require.Error(t, c.LabelMessages(context.Background(), []string{"messageID"}, proton.TrashLabel)) |
| 235 |
|
| 236 |
// We should still have dialed twice; the connection could not be reused because the write failed. |
| 237 |
require.Equal(t, 2, dialed) |
| 238 |
} |
| 239 |
|
| 240 |
func TestStatus_ContextCancel(t *testing.T) { |
| 241 |
s := server.New() |
| 242 |
defer s.Close() |
| 243 |
|
| 244 |
m := proton.New(proton.WithHostURL(s.GetHostURL())) |
| 245 |
defer m.Close() |
| 246 |
|
| 247 |
var called int |
| 248 |
|
| 249 |
m.AddStatusObserver(func(proton.Status) { |
| 250 |
called++ |
| 251 |
}) |
| 252 |
|
| 253 |
// Create a context that will be canceled. |
| 254 |
ctx, cancel := context.WithCancel(context.Background()) |
| 255 |
cancel() |
| 256 |
|
| 257 |
// This should fail because the context is canceled. |
| 258 |
require.Error(t, m.Ping(ctx)) |
| 259 |
|
| 260 |
// Status should not have been called; this was not a network error. |
| 261 |
require.Zero(t, called) |
| 262 |
} |
| 263 |
|
| 264 |
func TestStatus_ContextTimeout(t *testing.T) { |
| 265 |
s := server.New() |
| 266 |
defer s.Close() |
| 267 |
|
| 268 |
m := proton.New(proton.WithHostURL(s.GetHostURL())) |
| 269 |
defer m.Close() |
| 270 |
|
| 271 |
var called int |
| 272 |
|
| 273 |
m.AddStatusObserver(func(proton.Status) { |
| 274 |
called++ |
| 275 |
}) |
| 276 |
|
| 277 |
// Create a context that will time out. |
| 278 |
ctx, cancel := context.WithTimeout(context.Background(), 0) |
| 279 |
cancel() |
| 280 |
|
| 281 |
// This should fail because the context is canceled. |
| 282 |
require.Error(t, m.Ping(ctx)) |
| 283 |
|
| 284 |
// Status should have been called; this was a network error (took too long). |
| 285 |
require.NotZero(t, called) |
| 286 |
} |
| 287 |
|
| 288 |
func TestStatus_ServerDrop(t *testing.T) { |
| 289 |
l, err := net.Listen("tcp", "127.0.0.1:0") |
| 290 |
require.NoError(t, err) |
| 291 |
|
| 292 |
// Create a listener that will drop connections. |
| 293 |
dropListener := proton.NewListener(l, proton.NewDropConn) |
| 294 |
defer dropListener.Close() |
| 295 |
|
| 296 |
s := server.New(server.WithListener(dropListener), server.WithTLS(true)) |
| 297 |
defer s.Close() |
| 298 |
|
| 299 |
userID, _, err := s.CreateUser("user", []byte("pass")) |
| 300 |
require.NoError(t, err) |
| 301 |
|
| 302 |
m := proton.New(proton.WithHostURL(s.GetHostURL()), proton.WithTransport(proton.InsecureTransport())) |
| 303 |
defer m.Close() |
| 304 |
|
| 305 |
var status []proton.Status |
| 306 |
|
| 307 |
// Track the status as it changes. |
| 308 |
m.AddStatusObserver(func(s proton.Status) { |
| 309 |
status = append(status, s) |
| 310 |
}) |
| 311 |
|
| 312 |
// Login the new user. |
| 313 |
c, auth, err := m.NewClientWithLogin(context.Background(), "user", []byte("pass")) |
| 314 |
require.NoError(t, err) |
| 315 |
require.Equal(t, userID, auth.UserID) |
| 316 |
|
| 317 |
// This should succeed. |
| 318 |
user, err := c.GetUser(context.Background()) |
| 319 |
require.NoError(t, err) |
| 320 |
require.Equal(t, userID, user.ID) |
| 321 |
|
| 322 |
// Status should be empty. |
| 323 |
require.Empty(t, status) |
| 324 |
|
| 325 |
// Drop all existing connections and prevent new connections from writing (simulating server kicking off client). |
| 326 |
dropListener.DropAll() |
| 327 |
dropListener.SetCanWrite(false) |
| 328 |
|
| 329 |
// This should fail because the connection will be dropped. |
| 330 |
require.ErrorIs(t, getErr(c.GetUser(context.Background())), new(proton.NetError)) |
| 331 |
|
| 332 |
// Status should be down. |
| 333 |
require.Equal(t, []proton.Status{proton.StatusDown}, status) |
| 334 |
|
| 335 |
// Allow new connections to write. |
| 336 |
dropListener.SetCanWrite(true) |
| 337 |
|
| 338 |
// This should succeed. |
| 339 |
require.NoError(t, getErr(c.GetUser(context.Background()))) |
| 340 |
|
| 341 |
// Status should be up. |
| 342 |
require.Equal(t, []proton.Status{proton.StatusDown, proton.StatusUp}, status) |
| 343 |
} |
| 344 |
|
| 345 |
func TestStatus_ServerHang(t *testing.T) { |
| 346 |
l, err := net.Listen("tcp", "127.0.0.1:0") |
| 347 |
require.NoError(t, err) |
| 348 |
|
| 349 |
// Create a listener that will hang on reads/writes. |
| 350 |
hangListener := proton.NewListener(l, proton.NewHangConn) |
| 351 |
defer hangListener.Close() |
| 352 |
|
| 353 |
s := server.New(server.WithListener(hangListener), server.WithTLS(false)) |
| 354 |
defer s.Close() |
| 355 |
|
| 356 |
userID, _, err := s.CreateUser("user", []byte("pass")) |
| 357 |
require.NoError(t, err) |
| 358 |
|
| 359 |
m := proton.New( |
| 360 |
proton.WithHostURL(s.GetHostURL()), |
| 361 |
proton.WithTransport(&http.Transport{ResponseHeaderTimeout: time.Second}), |
| 362 |
) |
| 363 |
defer m.Close() |
| 364 |
|
| 365 |
var status []proton.Status |
| 366 |
|
| 367 |
// Track the status as it changes. |
| 368 |
m.AddStatusObserver(func(s proton.Status) { |
| 369 |
status = append(status, s) |
| 370 |
}) |
| 371 |
|
| 372 |
// Login the new user. |
| 373 |
c, auth, err := m.NewClientWithLogin(context.Background(), "user", []byte("pass")) |
| 374 |
require.NoError(t, err) |
| 375 |
require.Equal(t, userID, auth.UserID) |
| 376 |
|
| 377 |
// This should succeed. |
| 378 |
user, err := c.GetUser(context.Background()) |
| 379 |
require.NoError(t, err) |
| 380 |
require.Equal(t, userID, user.ID) |
| 381 |
|
| 382 |
// Status should be empty. |
| 383 |
require.Empty(t, status) |
| 384 |
|
| 385 |
// Drop all existing connections and hang on writing to new connections. |
| 386 |
hangListener.DropAll() |
| 387 |
hangListener.SetCanWrite(false) |
| 388 |
|
| 389 |
// This should fail because the connection will hang. |
| 390 |
require.ErrorIs(t, getErr(c.GetUser(context.Background())), new(proton.NetError)) |
| 391 |
|
| 392 |
// Status should be down. |
| 393 |
require.Equal(t, []proton.Status{proton.StatusDown}, status) |
| 394 |
|
| 395 |
// Allow new connections to write. |
| 396 |
hangListener.SetCanWrite(true) |
| 397 |
|
| 398 |
// This should succeed. |
| 399 |
require.NoError(t, getErr(c.GetUser(context.Background()))) |
| 400 |
|
| 401 |
// Status should be up. |
| 402 |
require.Equal(t, []proton.Status{proton.StatusDown, proton.StatusUp}, status) |
| 403 |
} |
| 404 |
|
| 405 |
func getErr[T any](_ T, err error) error { |
| 406 |
return err |
| 407 |
} |