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