12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364 |
- /*
- *
- * Copyright 2014 gRPC authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
- package grpc
- import (
- "context"
- "errors"
- "fmt"
- "math"
- "net"
- "strings"
- "sync/atomic"
- "testing"
- "time"
- "golang.org/x/net/http2"
- "google.golang.org/grpc/connectivity"
- "google.golang.org/grpc/credentials"
- "google.golang.org/grpc/internal/backoff"
- "google.golang.org/grpc/internal/envconfig"
- "google.golang.org/grpc/internal/transport"
- "google.golang.org/grpc/keepalive"
- "google.golang.org/grpc/naming"
- "google.golang.org/grpc/resolver"
- "google.golang.org/grpc/resolver/manual"
- _ "google.golang.org/grpc/resolver/passthrough"
- "google.golang.org/grpc/testdata"
- )
- func assertState(wantState connectivity.State, cc *ClientConn) (connectivity.State, bool) {
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
- defer cancel()
- var state connectivity.State
- for state = cc.GetState(); state != wantState && cc.WaitForStateChange(ctx, state); state = cc.GetState() {
- }
- return state, state == wantState
- }
- func (s) TestDialWithTimeout(t *testing.T) {
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- defer lis.Close()
- lisAddr := resolver.Address{Addr: lis.Addr().String()}
- lisDone := make(chan struct{})
- dialDone := make(chan struct{})
- // 1st listener accepts the connection and then does nothing
- go func() {
- defer close(lisDone)
- conn, err := lis.Accept()
- if err != nil {
- t.Errorf("Error while accepting. Err: %v", err)
- return
- }
- framer := http2.NewFramer(conn, conn)
- if err := framer.WriteSettings(http2.Setting{}); err != nil {
- t.Errorf("Error while writing settings. Err: %v", err)
- return
- }
- <-dialDone // Close conn only after dial returns.
- }()
- r, cleanup := manual.GenerateAndRegisterManualResolver()
- defer cleanup()
- r.InitialState(resolver.State{Addresses: []resolver.Address{lisAddr}})
- client, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithTimeout(5*time.Second))
- close(dialDone)
- if err != nil {
- t.Fatalf("Dial failed. Err: %v", err)
- }
- defer client.Close()
- timeout := time.After(1 * time.Second)
- select {
- case <-timeout:
- t.Fatal("timed out waiting for server to finish")
- case <-lisDone:
- }
- }
- func (s) TestDialWithMultipleBackendsNotSendingServerPreface(t *testing.T) {
- lis1, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- defer lis1.Close()
- lis1Addr := resolver.Address{Addr: lis1.Addr().String()}
- lis1Done := make(chan struct{})
- // 1st listener accepts the connection and immediately closes it.
- go func() {
- defer close(lis1Done)
- conn, err := lis1.Accept()
- if err != nil {
- t.Errorf("Error while accepting. Err: %v", err)
- return
- }
- conn.Close()
- }()
- lis2, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- defer lis2.Close()
- lis2Done := make(chan struct{})
- lis2Addr := resolver.Address{Addr: lis2.Addr().String()}
- // 2nd listener should get a connection attempt since the first one failed.
- go func() {
- defer close(lis2Done)
- _, err := lis2.Accept() // Closing the client will clean up this conn.
- if err != nil {
- t.Errorf("Error while accepting. Err: %v", err)
- return
- }
- }()
- r, cleanup := manual.GenerateAndRegisterManualResolver()
- defer cleanup()
- r.InitialState(resolver.State{Addresses: []resolver.Address{lis1Addr, lis2Addr}})
- client, err := Dial(r.Scheme()+":///test.server", WithInsecure())
- if err != nil {
- t.Fatalf("Dial failed. Err: %v", err)
- }
- defer client.Close()
- timeout := time.After(5 * time.Second)
- select {
- case <-timeout:
- t.Fatal("timed out waiting for server 1 to finish")
- case <-lis1Done:
- }
- select {
- case <-timeout:
- t.Fatal("timed out waiting for server 2 to finish")
- case <-lis2Done:
- }
- }
- var allReqHSSettings = []envconfig.RequireHandshakeSetting{
- envconfig.RequireHandshakeOff,
- envconfig.RequireHandshakeOn,
- }
- func (s) TestDialWaitsForServerSettings(t *testing.T) {
- // Restore current setting after test.
- old := envconfig.RequireHandshake
- defer func() { envconfig.RequireHandshake = old }()
- // Test with all environment variable settings, which should not impact the
- // test case since WithWaitForHandshake has higher priority.
- for _, setting := range allReqHSSettings {
- envconfig.RequireHandshake = setting
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- defer lis.Close()
- done := make(chan struct{})
- sent := make(chan struct{})
- dialDone := make(chan struct{})
- go func() { // Launch the server.
- defer func() {
- close(done)
- }()
- conn, err := lis.Accept()
- if err != nil {
- t.Errorf("Error while accepting. Err: %v", err)
- return
- }
- defer conn.Close()
- // Sleep for a little bit to make sure that Dial on client
- // side blocks until settings are received.
- time.Sleep(100 * time.Millisecond)
- framer := http2.NewFramer(conn, conn)
- close(sent)
- if err := framer.WriteSettings(http2.Setting{}); err != nil {
- t.Errorf("Error while writing settings. Err: %v", err)
- return
- }
- <-dialDone // Close conn only after dial returns.
- }()
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- client, err := DialContext(ctx, lis.Addr().String(), WithInsecure(), WithWaitForHandshake(), WithBlock())
- close(dialDone)
- if err != nil {
- t.Fatalf("Error while dialing. Err: %v", err)
- }
- defer client.Close()
- select {
- case <-sent:
- default:
- t.Fatalf("Dial returned before server settings were sent")
- }
- <-done
- }
- }
- func (s) TestDialWaitsForServerSettingsViaEnv(t *testing.T) {
- // Set default behavior and restore current setting after test.
- old := envconfig.RequireHandshake
- envconfig.RequireHandshake = envconfig.RequireHandshakeOn
- defer func() { envconfig.RequireHandshake = old }()
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- defer lis.Close()
- done := make(chan struct{})
- sent := make(chan struct{})
- dialDone := make(chan struct{})
- go func() { // Launch the server.
- defer func() {
- close(done)
- }()
- conn, err := lis.Accept()
- if err != nil {
- t.Errorf("Error while accepting. Err: %v", err)
- return
- }
- defer conn.Close()
- // Sleep for a little bit to make sure that Dial on client
- // side blocks until settings are received.
- time.Sleep(100 * time.Millisecond)
- framer := http2.NewFramer(conn, conn)
- close(sent)
- if err := framer.WriteSettings(http2.Setting{}); err != nil {
- t.Errorf("Error while writing settings. Err: %v", err)
- return
- }
- <-dialDone // Close conn only after dial returns.
- }()
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- client, err := DialContext(ctx, lis.Addr().String(), WithInsecure(), WithBlock())
- close(dialDone)
- if err != nil {
- t.Fatalf("Error while dialing. Err: %v", err)
- }
- defer client.Close()
- select {
- case <-sent:
- default:
- t.Fatalf("Dial returned before server settings were sent")
- }
- <-done
- }
- func (s) TestDialWaitsForServerSettingsAndFails(t *testing.T) {
- // Restore current setting after test.
- old := envconfig.RequireHandshake
- defer func() { envconfig.RequireHandshake = old }()
- for _, setting := range allReqHSSettings {
- envconfig.RequireHandshake = setting
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- done := make(chan struct{})
- numConns := 0
- go func() { // Launch the server.
- defer func() {
- close(done)
- }()
- for {
- conn, err := lis.Accept()
- if err != nil {
- break
- }
- numConns++
- defer conn.Close()
- }
- }()
- ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
- defer cancel()
- client, err := DialContext(ctx,
- lis.Addr().String(),
- WithInsecure(),
- WithWaitForHandshake(),
- WithBlock(),
- withBackoff(noBackoff{}),
- withMinConnectDeadline(func() time.Duration { return time.Second / 4 }))
- lis.Close()
- if err == nil {
- client.Close()
- t.Fatalf("Unexpected success (err=nil) while dialing")
- }
- if err != context.DeadlineExceeded {
- t.Fatalf("DialContext(_) = %v; want context.DeadlineExceeded", err)
- }
- if numConns < 2 {
- t.Fatalf("dial attempts: %v; want > 1", numConns)
- }
- <-done
- }
- }
- func (s) TestDialWaitsForServerSettingsViaEnvAndFails(t *testing.T) {
- // Set default behavior and restore current setting after test.
- old := envconfig.RequireHandshake
- envconfig.RequireHandshake = envconfig.RequireHandshakeOn
- defer func() { envconfig.RequireHandshake = old }()
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- done := make(chan struct{})
- numConns := 0
- go func() { // Launch the server.
- defer func() {
- close(done)
- }()
- for {
- conn, err := lis.Accept()
- if err != nil {
- break
- }
- numConns++
- defer conn.Close()
- }
- }()
- ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
- defer cancel()
- client, err := DialContext(ctx,
- lis.Addr().String(),
- WithInsecure(),
- WithBlock(),
- withBackoff(noBackoff{}),
- withMinConnectDeadline(func() time.Duration { return time.Second / 4 }))
- lis.Close()
- if err == nil {
- client.Close()
- t.Fatalf("Unexpected success (err=nil) while dialing")
- }
- if err != context.DeadlineExceeded {
- t.Fatalf("DialContext(_) = %v; want context.DeadlineExceeded", err)
- }
- if numConns < 2 {
- t.Fatalf("dial attempts: %v; want > 1", numConns)
- }
- <-done
- }
- func (s) TestDialDoesNotWaitForServerSettings(t *testing.T) {
- // Restore current setting after test.
- old := envconfig.RequireHandshake
- defer func() { envconfig.RequireHandshake = old }()
- envconfig.RequireHandshake = envconfig.RequireHandshakeOff
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- defer lis.Close()
- done := make(chan struct{})
- dialDone := make(chan struct{})
- go func() { // Launch the server.
- defer func() {
- close(done)
- }()
- conn, err := lis.Accept()
- if err != nil {
- t.Errorf("Error while accepting. Err: %v", err)
- return
- }
- defer conn.Close()
- <-dialDone // Close conn only after dial returns.
- }()
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- client, err := DialContext(ctx, lis.Addr().String(), WithInsecure(), WithBlock())
- if err != nil {
- t.Fatalf("DialContext returned err =%v; want nil", err)
- }
- defer client.Close()
- if state := client.GetState(); state != connectivity.Ready {
- t.Fatalf("client.GetState() = %v; want connectivity.Ready", state)
- }
- close(dialDone)
- }
- // 1. Client connects to a server that doesn't send preface.
- // 2. After minConnectTimeout(500 ms here), client disconnects and retries.
- // 3. The new server sends its preface.
- // 4. Client doesn't kill the connection this time.
- func (s) TestCloseConnectionWhenServerPrefaceNotReceived(t *testing.T) {
- // Restore current setting after test.
- old := envconfig.RequireHandshake
- defer func() { envconfig.RequireHandshake = old }()
- envconfig.RequireHandshake = envconfig.RequireHandshakeOn
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- var (
- conn2 net.Conn
- over uint32
- )
- defer func() {
- lis.Close()
- // conn2 shouldn't be closed until the client has
- // observed a successful test.
- if conn2 != nil {
- conn2.Close()
- }
- }()
- done := make(chan struct{})
- accepted := make(chan struct{})
- go func() { // Launch the server.
- defer close(done)
- conn1, err := lis.Accept()
- if err != nil {
- t.Errorf("Error while accepting. Err: %v", err)
- return
- }
- defer conn1.Close()
- // Don't send server settings and the client should close the connection and try again.
- conn2, err = lis.Accept() // Accept a reconnection request from client.
- if err != nil {
- t.Errorf("Error while accepting. Err: %v", err)
- return
- }
- close(accepted)
- framer := http2.NewFramer(conn2, conn2)
- if err = framer.WriteSettings(http2.Setting{}); err != nil {
- t.Errorf("Error while writing settings. Err: %v", err)
- return
- }
- b := make([]byte, 8)
- for {
- _, err = conn2.Read(b)
- if err == nil {
- continue
- }
- if atomic.LoadUint32(&over) == 1 {
- // The connection stayed alive for the timer.
- // Success.
- return
- }
- t.Errorf("Unexpected error while reading. Err: %v, want timeout error", err)
- break
- }
- }()
- client, err := Dial(lis.Addr().String(), WithInsecure(), withMinConnectDeadline(func() time.Duration { return time.Millisecond * 500 }))
- if err != nil {
- t.Fatalf("Error while dialing. Err: %v", err)
- }
- // wait for connection to be accepted on the server.
- timer := time.NewTimer(time.Second * 10)
- select {
- case <-accepted:
- case <-timer.C:
- t.Fatalf("Client didn't make another connection request in time.")
- }
- // Make sure the connection stays alive for sometime.
- time.Sleep(time.Second)
- atomic.StoreUint32(&over, 1)
- client.Close()
- <-done
- }
- func (s) TestBackoffWhenNoServerPrefaceReceived(t *testing.T) {
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- defer lis.Close()
- done := make(chan struct{})
- go func() { // Launch the server.
- defer func() {
- close(done)
- }()
- conn, err := lis.Accept() // Accept the connection only to close it immediately.
- if err != nil {
- t.Errorf("Error while accepting. Err: %v", err)
- return
- }
- prevAt := time.Now()
- conn.Close()
- var prevDuration time.Duration
- // Make sure the retry attempts are backed off properly.
- for i := 0; i < 3; i++ {
- conn, err := lis.Accept()
- if err != nil {
- t.Errorf("Error while accepting. Err: %v", err)
- return
- }
- meow := time.Now()
- conn.Close()
- dr := meow.Sub(prevAt)
- if dr <= prevDuration {
- t.Errorf("Client backoff did not increase with retries. Previous duration: %v, current duration: %v", prevDuration, dr)
- return
- }
- prevDuration = dr
- prevAt = meow
- }
- }()
- client, err := Dial(lis.Addr().String(), WithInsecure())
- if err != nil {
- t.Fatalf("Error while dialing. Err: %v", err)
- }
- defer client.Close()
- <-done
- }
- func (s) TestConnectivityStates(t *testing.T) {
- servers, resolver, cleanup := startServers(t, 2, math.MaxUint32)
- defer cleanup()
- cc, err := Dial("passthrough:///foo.bar.com", WithBalancer(RoundRobin(resolver)), WithInsecure())
- if err != nil {
- t.Fatalf("Dial(\"foo.bar.com\", WithBalancer(_)) = _, %v, want _ <nil>", err)
- }
- defer cc.Close()
- wantState := connectivity.Ready
- if state, ok := assertState(wantState, cc); !ok {
- t.Fatalf("asserState(%s) = %s, false, want %s, true", wantState, state, wantState)
- }
- // Send an update to delete the server connection (tearDown addrConn).
- update := []*naming.Update{
- {
- Op: naming.Delete,
- Addr: "localhost:" + servers[0].port,
- },
- }
- resolver.w.inject(update)
- wantState = connectivity.TransientFailure
- if state, ok := assertState(wantState, cc); !ok {
- t.Fatalf("asserState(%s) = %s, false, want %s, true", wantState, state, wantState)
- }
- update[0] = &naming.Update{
- Op: naming.Add,
- Addr: "localhost:" + servers[1].port,
- }
- resolver.w.inject(update)
- wantState = connectivity.Ready
- if state, ok := assertState(wantState, cc); !ok {
- t.Fatalf("asserState(%s) = %s, false, want %s, true", wantState, state, wantState)
- }
- }
- func (s) TestWithTimeout(t *testing.T) {
- conn, err := Dial("passthrough:///Non-Existent.Server:80", WithTimeout(time.Millisecond), WithBlock(), WithInsecure())
- if err == nil {
- conn.Close()
- }
- if err != context.DeadlineExceeded {
- t.Fatalf("Dial(_, _) = %v, %v, want %v", conn, err, context.DeadlineExceeded)
- }
- }
- func (s) TestWithTransportCredentialsTLS(t *testing.T) {
- ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
- defer cancel()
- creds, err := credentials.NewClientTLSFromFile(testdata.Path("ca.pem"), "x.test.youtube.com")
- if err != nil {
- t.Fatalf("Failed to create credentials %v", err)
- }
- conn, err := DialContext(ctx, "passthrough:///Non-Existent.Server:80", WithTransportCredentials(creds), WithBlock())
- if err == nil {
- conn.Close()
- }
- if err != context.DeadlineExceeded {
- t.Fatalf("Dial(_, _) = %v, %v, want %v", conn, err, context.DeadlineExceeded)
- }
- }
- func (s) TestDefaultAuthority(t *testing.T) {
- target := "Non-Existent.Server:8080"
- conn, err := Dial(target, WithInsecure())
- if err != nil {
- t.Fatalf("Dial(_, _) = _, %v, want _, <nil>", err)
- }
- defer conn.Close()
- if conn.authority != target {
- t.Fatalf("%v.authority = %v, want %v", conn, conn.authority, target)
- }
- }
- func (s) TestTLSServerNameOverwrite(t *testing.T) {
- overwriteServerName := "over.write.server.name"
- creds, err := credentials.NewClientTLSFromFile(testdata.Path("ca.pem"), overwriteServerName)
- if err != nil {
- t.Fatalf("Failed to create credentials %v", err)
- }
- conn, err := Dial("passthrough:///Non-Existent.Server:80", WithTransportCredentials(creds))
- if err != nil {
- t.Fatalf("Dial(_, _) = _, %v, want _, <nil>", err)
- }
- defer conn.Close()
- if conn.authority != overwriteServerName {
- t.Fatalf("%v.authority = %v, want %v", conn, conn.authority, overwriteServerName)
- }
- }
- func (s) TestWithAuthority(t *testing.T) {
- overwriteServerName := "over.write.server.name"
- conn, err := Dial("passthrough:///Non-Existent.Server:80", WithInsecure(), WithAuthority(overwriteServerName))
- if err != nil {
- t.Fatalf("Dial(_, _) = _, %v, want _, <nil>", err)
- }
- defer conn.Close()
- if conn.authority != overwriteServerName {
- t.Fatalf("%v.authority = %v, want %v", conn, conn.authority, overwriteServerName)
- }
- }
- func (s) TestWithAuthorityAndTLS(t *testing.T) {
- overwriteServerName := "over.write.server.name"
- creds, err := credentials.NewClientTLSFromFile(testdata.Path("ca.pem"), overwriteServerName)
- if err != nil {
- t.Fatalf("Failed to create credentials %v", err)
- }
- conn, err := Dial("passthrough:///Non-Existent.Server:80", WithTransportCredentials(creds), WithAuthority("no.effect.authority"))
- if err != nil {
- t.Fatalf("Dial(_, _) = _, %v, want _, <nil>", err)
- }
- defer conn.Close()
- if conn.authority != overwriteServerName {
- t.Fatalf("%v.authority = %v, want %v", conn, conn.authority, overwriteServerName)
- }
- }
- // When creating a transport configured with n addresses, only calculate the
- // backoff once per "round" of attempts instead of once per address (n times
- // per "round" of attempts).
- func (s) TestDial_OneBackoffPerRetryGroup(t *testing.T) {
- var attempts uint32
- getMinConnectTimeout := func() time.Duration {
- if atomic.AddUint32(&attempts, 1) == 1 {
- // Once all addresses are exhausted, hang around and wait for the
- // client.Close to happen rather than re-starting a new round of
- // attempts.
- return time.Hour
- }
- t.Error("only one attempt backoff calculation, but got more")
- return 0
- }
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
- defer cancel()
- lis1, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- defer lis1.Close()
- lis2, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- defer lis2.Close()
- server1Done := make(chan struct{})
- server2Done := make(chan struct{})
- // Launch server 1.
- go func() {
- conn, err := lis1.Accept()
- if err != nil {
- t.Error(err)
- return
- }
- conn.Close()
- close(server1Done)
- }()
- // Launch server 2.
- go func() {
- conn, err := lis2.Accept()
- if err != nil {
- t.Error(err)
- return
- }
- conn.Close()
- close(server2Done)
- }()
- rb := manual.NewBuilderWithScheme("whatever")
- rb.InitialState(resolver.State{Addresses: []resolver.Address{
- {Addr: lis1.Addr().String()},
- {Addr: lis2.Addr().String()},
- }})
- client, err := DialContext(ctx, "this-gets-overwritten",
- WithInsecure(),
- WithBalancerName(stateRecordingBalancerName),
- withResolverBuilder(rb),
- withMinConnectDeadline(getMinConnectTimeout))
- if err != nil {
- t.Fatal(err)
- }
- defer client.Close()
- timeout := time.After(15 * time.Second)
- select {
- case <-timeout:
- t.Fatal("timed out waiting for test to finish")
- case <-server1Done:
- }
- select {
- case <-timeout:
- t.Fatal("timed out waiting for test to finish")
- case <-server2Done:
- }
- }
- func (s) TestDialContextCancel(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- cancel()
- if _, err := DialContext(ctx, "Non-Existent.Server:80", WithBlock(), WithInsecure()); err != context.Canceled {
- t.Fatalf("DialContext(%v, _) = _, %v, want _, %v", ctx, err, context.Canceled)
- }
- }
- type failFastError struct{}
- func (failFastError) Error() string { return "failfast" }
- func (failFastError) Temporary() bool { return false }
- func (s) TestDialContextFailFast(t *testing.T) {
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- failErr := failFastError{}
- dialer := func(string, time.Duration) (net.Conn, error) {
- return nil, failErr
- }
- _, err := DialContext(ctx, "Non-Existent.Server:80", WithBlock(), WithInsecure(), WithDialer(dialer), FailOnNonTempDialError(true))
- if terr, ok := err.(transport.ConnectionError); !ok || terr.Origin() != failErr {
- t.Fatalf("DialContext() = _, %v, want _, %v", err, failErr)
- }
- }
- // blockingBalancer mimics the behavior of balancers whose initialization takes a long time.
- // In this test, reading from blockingBalancer.Notify() blocks forever.
- type blockingBalancer struct {
- ch chan []Address
- }
- func newBlockingBalancer() Balancer {
- return &blockingBalancer{ch: make(chan []Address)}
- }
- func (b *blockingBalancer) Start(target string, config BalancerConfig) error {
- return nil
- }
- func (b *blockingBalancer) Up(addr Address) func(error) {
- return nil
- }
- func (b *blockingBalancer) Get(ctx context.Context, opts BalancerGetOptions) (addr Address, put func(), err error) {
- return Address{}, nil, nil
- }
- func (b *blockingBalancer) Notify() <-chan []Address {
- return b.ch
- }
- func (b *blockingBalancer) Close() error {
- close(b.ch)
- return nil
- }
- func (s) TestDialWithBlockingBalancer(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- dialDone := make(chan struct{})
- go func() {
- DialContext(ctx, "Non-Existent.Server:80", WithBlock(), WithInsecure(), WithBalancer(newBlockingBalancer()))
- close(dialDone)
- }()
- cancel()
- <-dialDone
- }
- // securePerRPCCredentials always requires transport security.
- type securePerRPCCredentials struct{}
- func (c securePerRPCCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
- return nil, nil
- }
- func (c securePerRPCCredentials) RequireTransportSecurity() bool {
- return true
- }
- func (s) TestCredentialsMisuse(t *testing.T) {
- tlsCreds, err := credentials.NewClientTLSFromFile(testdata.Path("ca.pem"), "x.test.youtube.com")
- if err != nil {
- t.Fatalf("Failed to create authenticator %v", err)
- }
- // Two conflicting credential configurations
- if _, err := Dial("passthrough:///Non-Existent.Server:80", WithTransportCredentials(tlsCreds), WithBlock(), WithInsecure()); err != errCredentialsConflict {
- t.Fatalf("Dial(_, _) = _, %v, want _, %v", err, errCredentialsConflict)
- }
- // security info on insecure connection
- if _, err := Dial("passthrough:///Non-Existent.Server:80", WithPerRPCCredentials(securePerRPCCredentials{}), WithBlock(), WithInsecure()); err != errTransportCredentialsMissing {
- t.Fatalf("Dial(_, _) = _, %v, want _, %v", err, errTransportCredentialsMissing)
- }
- }
- func (s) TestWithBackoffConfigDefault(t *testing.T) {
- testBackoffConfigSet(t, &DefaultBackoffConfig)
- }
- func (s) TestWithBackoffConfig(t *testing.T) {
- b := BackoffConfig{MaxDelay: DefaultBackoffConfig.MaxDelay / 2}
- expected := b
- testBackoffConfigSet(t, &expected, WithBackoffConfig(b))
- }
- func (s) TestWithBackoffMaxDelay(t *testing.T) {
- md := DefaultBackoffConfig.MaxDelay / 2
- expected := BackoffConfig{MaxDelay: md}
- testBackoffConfigSet(t, &expected, WithBackoffMaxDelay(md))
- }
- func testBackoffConfigSet(t *testing.T, expected *BackoffConfig, opts ...DialOption) {
- opts = append(opts, WithInsecure())
- conn, err := Dial("passthrough:///foo:80", opts...)
- if err != nil {
- t.Fatalf("unexpected error dialing connection: %v", err)
- }
- defer conn.Close()
- if conn.dopts.bs == nil {
- t.Fatalf("backoff config not set")
- }
- actual, ok := conn.dopts.bs.(backoff.Exponential)
- if !ok {
- t.Fatalf("unexpected type of backoff config: %#v", conn.dopts.bs)
- }
- expectedValue := backoff.Exponential{
- MaxDelay: expected.MaxDelay,
- }
- if actual != expectedValue {
- t.Fatalf("unexpected backoff config on connection: %v, want %v", actual, expected)
- }
- }
- // emptyBalancer returns an empty set of servers.
- type emptyBalancer struct {
- ch chan []Address
- }
- func newEmptyBalancer() Balancer {
- return &emptyBalancer{ch: make(chan []Address, 1)}
- }
- func (b *emptyBalancer) Start(_ string, _ BalancerConfig) error {
- b.ch <- nil
- return nil
- }
- func (b *emptyBalancer) Up(_ Address) func(error) {
- return nil
- }
- func (b *emptyBalancer) Get(_ context.Context, _ BalancerGetOptions) (Address, func(), error) {
- return Address{}, nil, nil
- }
- func (b *emptyBalancer) Notify() <-chan []Address {
- return b.ch
- }
- func (b *emptyBalancer) Close() error {
- close(b.ch)
- return nil
- }
- func (s) TestNonblockingDialWithEmptyBalancer(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- dialDone := make(chan error)
- go func() {
- dialDone <- func() error {
- conn, err := DialContext(ctx, "Non-Existent.Server:80", WithInsecure(), WithBalancer(newEmptyBalancer()))
- if err != nil {
- return err
- }
- return conn.Close()
- }()
- }()
- if err := <-dialDone; err != nil {
- t.Fatalf("unexpected error dialing connection: %s", err)
- }
- }
- func (s) TestResolverServiceConfigBeforeAddressNotPanic(t *testing.T) {
- r, rcleanup := manual.GenerateAndRegisterManualResolver()
- defer rcleanup()
- cc, err := Dial(r.Scheme()+":///test.server", WithInsecure())
- if err != nil {
- t.Fatalf("failed to dial: %v", err)
- }
- defer cc.Close()
- // SwitchBalancer before NewAddress. There was no balancer created, this
- // makes sure we don't call close on nil balancerWrapper.
- r.UpdateState(resolver.State{ServiceConfig: `{"loadBalancingPolicy": "round_robin"}`}) // This should not panic.
- time.Sleep(time.Second) // Sleep to make sure the service config is handled by ClientConn.
- }
- func (s) TestResolverServiceConfigWhileClosingNotPanic(t *testing.T) {
- for i := 0; i < 10; i++ { // Run this multiple times to make sure it doesn't panic.
- r, rcleanup := manual.GenerateAndRegisterManualResolver()
- defer rcleanup()
- cc, err := Dial(r.Scheme()+":///test.server", WithInsecure())
- if err != nil {
- t.Fatalf("failed to dial: %v", err)
- }
- // Send a new service config while closing the ClientConn.
- go cc.Close()
- go r.UpdateState(resolver.State{ServiceConfig: `{"loadBalancingPolicy": "round_robin"}`}) // This should not panic.
- }
- }
- func (s) TestResolverEmptyUpdateNotPanic(t *testing.T) {
- r, rcleanup := manual.GenerateAndRegisterManualResolver()
- defer rcleanup()
- cc, err := Dial(r.Scheme()+":///test.server", WithInsecure())
- if err != nil {
- t.Fatalf("failed to dial: %v", err)
- }
- defer cc.Close()
- // This make sure we don't create addrConn with empty address list.
- r.UpdateState(resolver.State{}) // This should not panic.
- time.Sleep(time.Second) // Sleep to make sure the service config is handled by ClientConn.
- }
- func (s) TestClientUpdatesParamsAfterGoAway(t *testing.T) {
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Failed to listen. Err: %v", err)
- }
- defer lis.Close()
- connected := make(chan struct{})
- go func() {
- conn, err := lis.Accept()
- if err != nil {
- t.Errorf("error accepting connection: %v", err)
- return
- }
- defer conn.Close()
- f := http2.NewFramer(conn, conn)
- // Start a goroutine to read from the conn to prevent the client from
- // blocking after it writes its preface.
- go func() {
- for {
- if _, err := f.ReadFrame(); err != nil {
- return
- }
- }
- }()
- if err := f.WriteSettings(http2.Setting{}); err != nil {
- t.Errorf("error writing settings: %v", err)
- return
- }
- <-connected
- if err := f.WriteGoAway(0, http2.ErrCodeEnhanceYourCalm, []byte("too_many_pings")); err != nil {
- t.Errorf("error writing GOAWAY: %v", err)
- return
- }
- }()
- addr := lis.Addr().String()
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- cc, err := DialContext(ctx, addr, WithBlock(), WithInsecure(), WithKeepaliveParams(keepalive.ClientParameters{
- Time: 10 * time.Second,
- Timeout: 100 * time.Millisecond,
- PermitWithoutStream: true,
- }))
- if err != nil {
- t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", addr, err)
- }
- defer cc.Close()
- close(connected)
- for {
- time.Sleep(10 * time.Millisecond)
- cc.mu.RLock()
- v := cc.mkp.Time
- if v == 20*time.Second {
- // Success
- cc.mu.RUnlock()
- return
- }
- if ctx.Err() != nil {
- // Timeout
- t.Fatalf("cc.dopts.copts.Keepalive.Time = %v , want 20s", v)
- }
- cc.mu.RUnlock()
- }
- }
- func (s) TestDisableServiceConfigOption(t *testing.T) {
- r, cleanup := manual.GenerateAndRegisterManualResolver()
- defer cleanup()
- addr := r.Scheme() + ":///non.existent"
- cc, err := Dial(addr, WithInsecure(), WithDisableServiceConfig())
- if err != nil {
- t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", addr, err)
- }
- defer cc.Close()
- r.UpdateState(resolver.State{ServiceConfig: `{
- "methodConfig": [
- {
- "name": [
- {
- "service": "foo",
- "method": "Bar"
- }
- ],
- "waitForReady": true
- }
- ]
- }`})
- time.Sleep(1 * time.Second)
- m := cc.GetMethodConfig("/foo/Bar")
- if m.WaitForReady != nil {
- t.Fatalf("want: method (\"/foo/bar/\") config to be empty, got: %v", m)
- }
- }
- func (s) TestGetClientConnTarget(t *testing.T) {
- addr := "nonexist:///non.existent"
- cc, err := Dial(addr, WithInsecure())
- if err != nil {
- t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", addr, err)
- }
- defer cc.Close()
- if cc.Target() != addr {
- t.Fatalf("Target() = %s, want %s", cc.Target(), addr)
- }
- }
- type backoffForever struct{}
- func (b backoffForever) Backoff(int) time.Duration { return time.Duration(math.MaxInt64) }
- func (s) TestResetConnectBackoff(t *testing.T) {
- dials := make(chan struct{})
- defer func() { // If we fail, let the http2client break out of dialing.
- select {
- case <-dials:
- default:
- }
- }()
- dialer := func(string, time.Duration) (net.Conn, error) {
- dials <- struct{}{}
- return nil, errors.New("failed to fake dial")
- }
- cc, err := Dial("any", WithInsecure(), WithDialer(dialer), withBackoff(backoffForever{}))
- if err != nil {
- t.Fatalf("Dial() = _, %v; want _, nil", err)
- }
- defer cc.Close()
- select {
- case <-dials:
- case <-time.NewTimer(10 * time.Second).C:
- t.Fatal("Failed to call dial within 10s")
- }
- select {
- case <-dials:
- t.Fatal("Dial called unexpectedly before resetting backoff")
- case <-time.NewTimer(100 * time.Millisecond).C:
- }
- cc.ResetConnectBackoff()
- select {
- case <-dials:
- case <-time.NewTimer(10 * time.Second).C:
- t.Fatal("Failed to call dial within 10s after resetting backoff")
- }
- }
- func (s) TestBackoffCancel(t *testing.T) {
- dialStrCh := make(chan string)
- cc, err := Dial("any", WithInsecure(), WithDialer(func(t string, _ time.Duration) (net.Conn, error) {
- dialStrCh <- t
- return nil, fmt.Errorf("test dialer, always error")
- }))
- if err != nil {
- t.Fatalf("Failed to create ClientConn: %v", err)
- }
- <-dialStrCh
- cc.Close()
- // Should not leak. May need -count 5000 to exercise.
- }
- // UpdateAddresses should cause the next reconnect to begin from the top of the
- // list if the connection is not READY.
- func (s) TestUpdateAddresses_RetryFromFirstAddr(t *testing.T) {
- lis1, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- defer lis1.Close()
- lis2, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- defer lis2.Close()
- lis3, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- defer lis3.Close()
- closeServer2 := make(chan struct{})
- server1ContactedFirstTime := make(chan struct{})
- server1ContactedSecondTime := make(chan struct{})
- server2ContactedFirstTime := make(chan struct{})
- server2ContactedSecondTime := make(chan struct{})
- server3Contacted := make(chan struct{})
- // Launch server 1.
- go func() {
- // First, let's allow the initial connection to go READY. We need to do
- // this because tryUpdateAddrs only works after there's some non-nil
- // address on the ac, and curAddress is only set after READY.
- conn1, err := lis1.Accept()
- if err != nil {
- t.Error(err)
- return
- }
- go keepReading(conn1)
- framer := http2.NewFramer(conn1, conn1)
- if err := framer.WriteSettings(http2.Setting{}); err != nil {
- t.Errorf("Error while writing settings frame. %v", err)
- return
- }
- // nextStateNotifier() is updated after balancerBuilder.Build(), which is
- // called by grpc.Dial. It's safe to do it here because lis1.Accept blocks
- // until balancer is built to process the addresses.
- stateNotifications := testBalancerBuilder.nextStateNotifier()
- // Wait for the transport to become ready.
- for s := range stateNotifications {
- if s == connectivity.Ready {
- break
- }
- }
- // Once it's ready, curAddress has been set. So let's close this
- // connection prompting the first reconnect cycle.
- conn1.Close()
- // Accept and immediately close, causing it to go to server2.
- conn2, err := lis1.Accept()
- if err != nil {
- t.Error(err)
- return
- }
- close(server1ContactedFirstTime)
- conn2.Close()
- // Hopefully it picks this server after tryUpdateAddrs.
- lis1.Accept()
- close(server1ContactedSecondTime)
- }()
- // Launch server 2.
- go func() {
- // Accept and then hang waiting for the test call tryUpdateAddrs and
- // then signal to this server to close. After this server closes, it
- // should start from the top instead of trying server2 or continuing
- // to server3.
- conn, err := lis2.Accept()
- if err != nil {
- t.Error(err)
- return
- }
- close(server2ContactedFirstTime)
- <-closeServer2
- conn.Close()
- // After tryUpdateAddrs, it should NOT try server2.
- lis2.Accept()
- close(server2ContactedSecondTime)
- }()
- // Launch server 3.
- go func() {
- // After tryUpdateAddrs, it should NOT try server3. (or any other time)
- lis3.Accept()
- close(server3Contacted)
- }()
- addrsList := []resolver.Address{
- {Addr: lis1.Addr().String()},
- {Addr: lis2.Addr().String()},
- {Addr: lis3.Addr().String()},
- }
- rb := manual.NewBuilderWithScheme("whatever")
- rb.InitialState(resolver.State{Addresses: addrsList})
- client, err := Dial("this-gets-overwritten",
- WithInsecure(),
- WithWaitForHandshake(),
- withResolverBuilder(rb),
- withBackoff(noBackoff{}),
- WithBalancerName(stateRecordingBalancerName),
- withMinConnectDeadline(func() time.Duration { return time.Hour }))
- if err != nil {
- t.Fatal(err)
- }
- defer client.Close()
- timeout := time.After(5 * time.Second)
- // Wait for server1 to be contacted (which will immediately fail), then
- // server2 (which will hang waiting for our signal).
- select {
- case <-server1ContactedFirstTime:
- case <-timeout:
- t.Fatal("timed out waiting for server1 to be contacted")
- }
- select {
- case <-server2ContactedFirstTime:
- case <-timeout:
- t.Fatal("timed out waiting for server2 to be contacted")
- }
- // Grab the addrConn and call tryUpdateAddrs.
- var ac *addrConn
- client.mu.Lock()
- for clientAC := range client.conns {
- ac = clientAC
- break
- }
- client.mu.Unlock()
- ac.acbw.UpdateAddresses(addrsList)
- // We've called tryUpdateAddrs - now let's make server2 close the
- // connection and check that it goes back to server1 instead of continuing
- // to server3 or trying server2 again.
- close(closeServer2)
- select {
- case <-server1ContactedSecondTime:
- case <-server2ContactedSecondTime:
- t.Fatal("server2 was contacted a second time, but it after tryUpdateAddrs it should have re-started the list and tried server1")
- case <-server3Contacted:
- t.Fatal("server3 was contacted, but after tryUpdateAddrs it should have re-started the list and tried server1")
- case <-timeout:
- t.Fatal("timed out waiting for any server to be contacted after tryUpdateAddrs")
- }
- }
- func (s) TestDefaultServiceConfig(t *testing.T) {
- r, cleanup := manual.GenerateAndRegisterManualResolver()
- defer cleanup()
- addr := r.Scheme() + ":///non.existent"
- js := `{
- "methodConfig": [
- {
- "name": [
- {
- "service": "foo",
- "method": "bar"
- }
- ],
- "waitForReady": true
- }
- ]
- }`
- testInvalidDefaultServiceConfig(t)
- testDefaultServiceConfigWhenResolverServiceConfigDisabled(t, r, addr, js)
- testDefaultServiceConfigWhenResolverDoesNotReturnServiceConfig(t, r, addr, js)
- testDefaultServiceConfigWhenResolverReturnInvalidServiceConfig(t, r, addr, js)
- }
- func verifyWaitForReadyEqualsTrue(cc *ClientConn) bool {
- var i int
- for i = 0; i < 10; i++ {
- mc := cc.GetMethodConfig("/foo/bar")
- if mc.WaitForReady != nil && *mc.WaitForReady == true {
- break
- }
- time.Sleep(100 * time.Millisecond)
- }
- return i != 10
- }
- func testInvalidDefaultServiceConfig(t *testing.T) {
- _, err := Dial("fake.com", WithInsecure(), WithDefaultServiceConfig(""))
- if !strings.Contains(err.Error(), invalidDefaultServiceConfigErrPrefix) {
- t.Fatalf("Dial got err: %v, want err contains: %v", err, invalidDefaultServiceConfigErrPrefix)
- }
- }
- func testDefaultServiceConfigWhenResolverServiceConfigDisabled(t *testing.T, r resolver.Resolver, addr string, js string) {
- cc, err := Dial(addr, WithInsecure(), WithDisableServiceConfig(), WithDefaultServiceConfig(js))
- if err != nil {
- t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", addr, err)
- }
- defer cc.Close()
- // Resolver service config gets ignored since resolver service config is disabled.
- r.(*manual.Resolver).UpdateState(resolver.State{
- Addresses: []resolver.Address{{Addr: addr}},
- ServiceConfig: "{}",
- })
- if !verifyWaitForReadyEqualsTrue(cc) {
- t.Fatal("default service config failed to be applied after 1s")
- }
- }
- func testDefaultServiceConfigWhenResolverDoesNotReturnServiceConfig(t *testing.T, r resolver.Resolver, addr string, js string) {
- cc, err := Dial(addr, WithInsecure(), WithDefaultServiceConfig(js))
- if err != nil {
- t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", addr, err)
- }
- defer cc.Close()
- r.(*manual.Resolver).UpdateState(resolver.State{
- Addresses: []resolver.Address{{Addr: addr}},
- })
- if !verifyWaitForReadyEqualsTrue(cc) {
- t.Fatal("default service config failed to be applied after 1s")
- }
- }
- func testDefaultServiceConfigWhenResolverReturnInvalidServiceConfig(t *testing.T, r resolver.Resolver, addr string, js string) {
- cc, err := Dial(addr, WithInsecure(), WithDefaultServiceConfig(js))
- if err != nil {
- t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", addr, err)
- }
- defer cc.Close()
- r.(*manual.Resolver).UpdateState(resolver.State{
- Addresses: []resolver.Address{{Addr: addr}},
- ServiceConfig: "{something wrong,}",
- })
- if !verifyWaitForReadyEqualsTrue(cc) {
- t.Fatal("default service config failed to be applied after 1s")
- }
- }
|