go.uber.org/yarpc@v1.72.1/transport/http/peer_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package http_test
    22  
    23  import (
    24  	"context"
    25  	"runtime"
    26  	"sync"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/opentracing/opentracing-go"
    31  	"go.uber.org/yarpc/api/backoff"
    32  	"go.uber.org/yarpc/api/peer"
    33  	"go.uber.org/yarpc/api/transport"
    34  	"go.uber.org/yarpc/internal/integrationtest"
    35  	"go.uber.org/yarpc/internal/testtime"
    36  	"go.uber.org/yarpc/peer/hostport"
    37  	"go.uber.org/yarpc/transport/http"
    38  )
    39  
    40  func newTransport() peer.Transport {
    41  	return http.NewTransport(
    42  		http.Tracer(opentracing.NoopTracer{}),
    43  		http.DisableKeepAlives(),
    44  		http.ConnTimeout(testtime.Millisecond),
    45  		http.ConnBackoff(backoff.None),
    46  		http.InnocenceWindow(10*time.Second),
    47  		http.NoJitter(),
    48  	)
    49  }
    50  
    51  var spec = integrationtest.TransportSpec{
    52  	Identify: hostport.Identify,
    53  	NewServerTransport: func(t *testing.T, addr string) peer.Transport {
    54  		return newTransport()
    55  	},
    56  	NewClientTransport: func(t *testing.T) peer.Transport {
    57  		return newTransport()
    58  	},
    59  	NewUnaryOutbound: func(x peer.Transport, pc peer.Chooser) transport.UnaryOutbound {
    60  		return x.(*http.Transport).NewOutbound(pc)
    61  	},
    62  	NewInbound: func(x peer.Transport, addr string) transport.Inbound {
    63  		return x.(*http.Transport).NewInbound(addr)
    64  	},
    65  	Addr: func(x peer.Transport, ib transport.Inbound) string {
    66  		return ib.(*http.Inbound).Addr().String()
    67  	},
    68  }
    69  
    70  func TestHTTPWithRoundRobin(t *testing.T) {
    71  	ctx := context.Background()
    72  	ctx, cancel := context.WithTimeout(ctx, testtime.Second)
    73  	defer cancel()
    74  
    75  	permanent, permanentAddr := spec.NewServer(t, "127.0.0.1:0")
    76  	defer permanent.Stop()
    77  
    78  	temporary, temporaryAddr := spec.NewServer(t, "127.0.0.1:0")
    79  	defer temporary.Stop()
    80  
    81  	// Construct a client with a bank of peers. We will keep one running all
    82  	// the time. We'll shut one down temporarily.
    83  	// The round robin peer list should only choose peers that have
    84  	// successfully connected.
    85  	client, c := spec.NewClient(t, []string{
    86  		permanentAddr,
    87  		temporaryAddr,
    88  	})
    89  	defer client.Stop()
    90  
    91  	integrationtest.Blast(ctx, t, c)
    92  
    93  	// Shut down one task in the peer list.
    94  	temporary.Stop()
    95  	// One of these requests may fail since one of the peers has gone down but
    96  	// the HTTP transport will not know until a request is attempted.
    97  	integrationtest.Call(ctx, c)
    98  	integrationtest.Call(ctx, c)
    99  	// All subsequent should succeed since the peer should be removed on
   100  	// connection fail.
   101  	integrationtest.Blast(ctx, t, c)
   102  
   103  	// Restore the server on the temporary port.
   104  	restored, _ := spec.NewServer(t, temporaryAddr)
   105  	defer restored.Stop()
   106  	integrationtest.Blast(ctx, t, c)
   107  }
   108  
   109  func TestHTTPOnSuspect(t *testing.T) {
   110  	server, serverAddr := spec.NewServer(t, "127.0.0.1:0")
   111  
   112  	client, c := spec.NewClient(t, []string{serverAddr})
   113  	defer client.Stop()
   114  
   115  	// Exercise OnSuspect
   116  	ctx := context.Background()
   117  	ctx, cancel := context.WithTimeout(ctx, 50*testtime.Millisecond)
   118  	defer cancel()
   119  	_ = integrationtest.Timeout(ctx, c)
   120  
   121  	// Exercise the innocence window
   122  	ctx = context.Background()
   123  	ctx, cancel = context.WithTimeout(ctx, 50*testtime.Millisecond)
   124  	defer cancel()
   125  	_ = integrationtest.Timeout(ctx, c)
   126  
   127  	// Validate that the peer remains available
   128  	ctx = context.Background()
   129  	ctx, cancel = context.WithTimeout(ctx, 50*testtime.Millisecond)
   130  	defer cancel()
   131  	integrationtest.Blast(ctx, t, c)
   132  
   133  	// Induce the peer management loop to exit through its shutdown path.
   134  	var wg sync.WaitGroup
   135  	wg.Add(1)
   136  	go func() {
   137  		defer wg.Done()
   138  		server.Stop()
   139  	}()
   140  	ctx = context.Background()
   141  	ctx, cancel = context.WithTimeout(ctx, 50*testtime.Millisecond)
   142  	defer cancel()
   143  	for {
   144  		err := integrationtest.Call(ctx, c)
   145  		if err != nil {
   146  			// Yielding, it transpires, is necessary to get coverage on leaving
   147  			// OnSuspect early due to the innocense window.  Even with this, it
   148  			// gets coverage about as often as it wins a coin toss.
   149  			runtime.Gosched()
   150  			break
   151  		}
   152  	}
   153  
   154  	// wait group to make sure the server is well stopped when we reach this step
   155  	// goroutine leak with the test otherwise
   156  	wg.Wait()
   157  }
   158  
   159  func TestIntegration(t *testing.T) {
   160  	t.Skip("Skipping due to test flakiness")
   161  	spec.Test(t)
   162  }