|
4 | 4 | "net/http"
|
5 | 5 | "net/http/httptest"
|
6 | 6 | "os"
|
| 7 | + "reflect" |
7 | 8 | "sync"
|
8 | 9 | "testing"
|
9 | 10 | "time"
|
@@ -33,6 +34,13 @@ func TestNewPool(t *testing.T) {
|
33 | 34 | mockHttpServer.Close()
|
34 | 35 | time.Sleep(2*time.Second + 100*time.Millisecond)
|
35 | 36 | assert.NotEqual(t, statusAvailable, pool.status[defaultAddr]) // the status should be unavailable again
|
| 37 | + |
| 38 | + httpProbeOpt := &HttpProbeOptions{ |
| 39 | + Protocol: "http", |
| 40 | + } |
| 41 | + pool = NewPool([]string{defaultAddr}, Options{HttpProbeOptions: httpProbeOpt}) |
| 42 | + assert.False(t, httpProbeOpt == pool.httpProbeOptions) // not equal but deep equal, as copied |
| 43 | + assert.True(t, reflect.DeepEqual(httpProbeOpt, pool.httpProbeOptions)) |
36 | 44 | }
|
37 | 45 |
|
38 | 46 | func TestAddressPool_GetAvailableAddress_priority(t *testing.T) {
|
@@ -328,3 +336,62 @@ func TestPool_CheckReadiness(t *testing.T) {
|
328 | 336 | })
|
329 | 337 | }
|
330 | 338 | }
|
| 339 | + |
| 340 | +func TestPool_doCheckConnectivity(t *testing.T) { |
| 341 | + server1HttpCalled := false |
| 342 | + server1 := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 343 | + server1HttpCalled = true |
| 344 | + return |
| 345 | + })) |
| 346 | + server1Addr := server1.Listener.Addr().String() |
| 347 | + |
| 348 | + // http probe is empty,use tcp probe |
| 349 | + p := NewPool([]string{server1Addr}) |
| 350 | + assert.NoError(t, p.doCheckConnectivity(server1Addr)) |
| 351 | + assert.False(t, server1HttpCalled) |
| 352 | + |
| 353 | + // http probe is not empty |
| 354 | + p = NewPool([]string{server1Addr}, Options{HttpProbeOptions: &HttpProbeOptions{ |
| 355 | + Protocol: "http", |
| 356 | + Path: "/", |
| 357 | + }}) |
| 358 | + assert.NoError(t, p.doCheckConnectivity(server1Addr)) |
| 359 | + assert.True(t, server1HttpCalled) |
| 360 | + server1.Close() |
| 361 | + assert.Error(t, p.doCheckConnectivity(server1Addr)) |
| 362 | + |
| 363 | + // http probe got 404,tcp probe again |
| 364 | + server1HttpCalled = false |
| 365 | + mux := http.NewServeMux() |
| 366 | + mux.HandleFunc("/test", func(writer http.ResponseWriter, request *http.Request) { |
| 367 | + server1HttpCalled = true |
| 368 | + return |
| 369 | + }) |
| 370 | + server1 = httptest.NewServer(mux) |
| 371 | + server1Addr = server1.Listener.Addr().String() |
| 372 | + p = NewPool([]string{server1Addr}, Options{HttpProbeOptions: &HttpProbeOptions{ |
| 373 | + Protocol: "http", |
| 374 | + Path: "/", // wrong path, got 404 |
| 375 | + }}) |
| 376 | + assert.NoError(t, p.doCheckConnectivity(server1Addr)) |
| 377 | + assert.False(t, server1HttpCalled) |
| 378 | + p.httpProbeOptions.Path = "/test" // right path |
| 379 | + assert.NoError(t, p.doCheckConnectivity(server1Addr)) |
| 380 | + assert.True(t, server1HttpCalled) |
| 381 | + server1.Close() |
| 382 | + |
| 383 | + // https probe |
| 384 | + server1HttpCalled = false |
| 385 | + server1 = httptest.NewTLSServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 386 | + server1HttpCalled = true |
| 387 | + return |
| 388 | + })) |
| 389 | + server1Addr = server1.Listener.Addr().String() |
| 390 | + p = NewPool([]string{server1Addr}, Options{HttpProbeOptions: &HttpProbeOptions{ |
| 391 | + Protocol: "https", |
| 392 | + Path: "/", // wrong path, got 404 |
| 393 | + }}) |
| 394 | + assert.NoError(t, p.doCheckConnectivity(server1Addr)) |
| 395 | + assert.True(t, server1HttpCalled) |
| 396 | + server1.Close() |
| 397 | +} |
0 commit comments