go.uber.org/yarpc@v1.72.1/yarpctest/stress_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 yarpctest
    22  
    23  import (
    24  	"context"
    25  	"fmt"
    26  	"sync"
    27  	"testing"
    28  	"time"
    29  
    30  	"go.uber.org/yarpc/api/peer"
    31  	"go.uber.org/yarpc/api/transport"
    32  )
    33  
    34  func TestStress(t *testing.T) {
    35  	test := ListStressTest{
    36  		Workers:  1,
    37  		Duration: time.Second,
    38  		Timeout:  10 * time.Millisecond,
    39  		New: func(t peer.Transport) peer.ChooserList {
    40  			return newMRUList(t)
    41  		},
    42  	}
    43  	report := test.Run(t)
    44  	report.Log(t)
    45  	test.Log(t)
    46  }
    47  
    48  var _ peer.ChooserList = (*mruList)(nil)
    49  
    50  type mruList struct {
    51  	transport peer.Transport
    52  	peer      peer.Peer
    53  	mu        sync.Mutex
    54  }
    55  
    56  func newMRUList(t peer.Transport) *mruList {
    57  	return &mruList{transport: t}
    58  }
    59  
    60  func (l *mruList) Start() error {
    61  	return nil
    62  }
    63  
    64  func (l *mruList) Stop() error {
    65  	return nil
    66  }
    67  
    68  func (l *mruList) IsRunning() bool {
    69  	return true
    70  }
    71  
    72  func (l *mruList) Choose(context.Context, *transport.Request) (peer peer.Peer, onFinish func(error), err error) {
    73  	l.mu.Lock()
    74  	defer l.mu.Unlock()
    75  
    76  	if l.peer == nil {
    77  		return nil, nil, fmt.Errorf("no peer available")
    78  	}
    79  
    80  	return l.peer, func(error) {}, nil
    81  }
    82  
    83  func (l *mruList) Update(updates peer.ListUpdates) error {
    84  	l.mu.Lock()
    85  	defer l.mu.Unlock()
    86  
    87  	for _, pid := range updates.Additions {
    88  		l.update(pid)
    89  	}
    90  
    91  	if (len(updates.Additions)+len(updates.Removals))%2 == 0 {
    92  		return fmt.Errorf("can you handle this")
    93  	}
    94  	if len(updates.Additions) == 0 {
    95  		return fmt.Errorf("parting is such sweet sorrow")
    96  	}
    97  	return nil
    98  }
    99  
   100  func (l *mruList) update(id peer.Identifier) {
   101  	if peer, err := l.transport.RetainPeer(id, nopSub); err == nil {
   102  		if l.peer != nil {
   103  			_ = l.transport.ReleasePeer(id, nopSub)
   104  		}
   105  		l.peer = peer
   106  	}
   107  }
   108  
   109  type _nopSub struct{}
   110  
   111  func (_nopSub) NotifyStatusChanged(peer.Identifier) {}
   112  
   113  var nopSub = _nopSub{}