github.com/ethereum/go-ethereum@v1.16.1/rpc/testservice_test.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rpc
    18  
    19  import (
    20  	"context"
    21  	"encoding/binary"
    22  	"errors"
    23  	"strings"
    24  	"sync"
    25  	"time"
    26  )
    27  
    28  func newTestServer() *Server {
    29  	server := NewServer()
    30  	server.idgen = sequentialIDGenerator()
    31  	if err := server.RegisterName("test", new(testService)); err != nil {
    32  		panic(err)
    33  	}
    34  	if err := server.RegisterName("nftest", new(notificationTestService)); err != nil {
    35  		panic(err)
    36  	}
    37  	return server
    38  }
    39  
    40  func sequentialIDGenerator() func() ID {
    41  	var (
    42  		mu      sync.Mutex
    43  		counter uint64
    44  	)
    45  	return func() ID {
    46  		mu.Lock()
    47  		defer mu.Unlock()
    48  		counter++
    49  		id := make([]byte, 8)
    50  		binary.BigEndian.PutUint64(id, counter)
    51  		return encodeID(id)
    52  	}
    53  }
    54  
    55  type testService struct{}
    56  
    57  type echoArgs struct {
    58  	S string
    59  }
    60  
    61  type echoResult struct {
    62  	String string
    63  	Int    int
    64  	Args   *echoArgs
    65  }
    66  
    67  type testError struct{}
    68  
    69  func (testError) Error() string          { return "testError" }
    70  func (testError) ErrorCode() int         { return 444 }
    71  func (testError) ErrorData() interface{} { return "testError data" }
    72  
    73  type MarshalErrObj struct{}
    74  
    75  func (o *MarshalErrObj) MarshalText() ([]byte, error) {
    76  	return nil, errors.New("marshal error")
    77  }
    78  
    79  func (s *testService) NoArgsRets() {}
    80  
    81  func (s *testService) Null() any {
    82  	return nil
    83  }
    84  
    85  func (s *testService) Echo(str string, i int, args *echoArgs) echoResult {
    86  	return echoResult{str, i, args}
    87  }
    88  
    89  func (s *testService) EchoWithCtx(ctx context.Context, str string, i int, args *echoArgs) echoResult {
    90  	return echoResult{str, i, args}
    91  }
    92  
    93  func (s *testService) Repeat(msg string, i int) string {
    94  	return strings.Repeat(msg, i)
    95  }
    96  
    97  func (s *testService) PeerInfo(ctx context.Context) PeerInfo {
    98  	return PeerInfoFromContext(ctx)
    99  }
   100  
   101  func (s *testService) Sleep(ctx context.Context, duration time.Duration) {
   102  	time.Sleep(duration)
   103  }
   104  
   105  func (s *testService) Block(ctx context.Context) error {
   106  	<-ctx.Done()
   107  	return errors.New("context canceled in testservice_block")
   108  }
   109  
   110  func (s *testService) Rets() (string, error) {
   111  	return "", nil
   112  }
   113  
   114  //lint:ignore ST1008 returns error first on purpose.
   115  func (s *testService) InvalidRets1() (error, string) {
   116  	return nil, ""
   117  }
   118  
   119  func (s *testService) InvalidRets2() (string, string) {
   120  	return "", ""
   121  }
   122  
   123  func (s *testService) InvalidRets3() (string, string, error) {
   124  	return "", "", nil
   125  }
   126  
   127  func (s *testService) ReturnError() error {
   128  	return testError{}
   129  }
   130  
   131  func (s *testService) MarshalError() *MarshalErrObj {
   132  	return &MarshalErrObj{}
   133  }
   134  
   135  func (s *testService) Panic() string {
   136  	panic("service panic")
   137  }
   138  
   139  func (s *testService) CallMeBack(ctx context.Context, method string, args []interface{}) (interface{}, error) {
   140  	c, ok := ClientFromContext(ctx)
   141  	if !ok {
   142  		return nil, errors.New("no client")
   143  	}
   144  	var result interface{}
   145  	err := c.Call(&result, method, args...)
   146  	return result, err
   147  }
   148  
   149  func (s *testService) CallMeBackLater(ctx context.Context, method string, args []interface{}) error {
   150  	c, ok := ClientFromContext(ctx)
   151  	if !ok {
   152  		return errors.New("no client")
   153  	}
   154  	go func() {
   155  		<-ctx.Done()
   156  		var result interface{}
   157  		c.Call(&result, method, args...)
   158  	}()
   159  	return nil
   160  }
   161  
   162  func (s *testService) Subscription(ctx context.Context) (*Subscription, error) {
   163  	return nil, nil
   164  }
   165  
   166  type notificationTestService struct {
   167  	unsubscribed            chan string
   168  	gotHangSubscriptionReq  chan struct{}
   169  	unblockHangSubscription chan struct{}
   170  }
   171  
   172  func (s *notificationTestService) Echo(i int) int {
   173  	return i
   174  }
   175  
   176  func (s *notificationTestService) Unsubscribe(subid string) {
   177  	if s.unsubscribed != nil {
   178  		s.unsubscribed <- subid
   179  	}
   180  }
   181  
   182  func (s *notificationTestService) SomeSubscription(ctx context.Context, n, val int) (*Subscription, error) {
   183  	notifier, supported := NotifierFromContext(ctx)
   184  	if !supported {
   185  		return nil, ErrNotificationsUnsupported
   186  	}
   187  
   188  	// By explicitly creating an subscription we make sure that the subscription id is send
   189  	// back to the client before the first subscription.Notify is called. Otherwise the
   190  	// events might be send before the response for the *_subscribe method.
   191  	subscription := notifier.CreateSubscription()
   192  	go func() {
   193  		for i := 0; i < n; i++ {
   194  			if err := notifier.Notify(subscription.ID, val+i); err != nil {
   195  				return
   196  			}
   197  		}
   198  		<-subscription.Err()
   199  		if s.unsubscribed != nil {
   200  			s.unsubscribed <- string(subscription.ID)
   201  		}
   202  	}()
   203  	return subscription, nil
   204  }
   205  
   206  // HangSubscription blocks on s.unblockHangSubscription before sending anything.
   207  func (s *notificationTestService) HangSubscription(ctx context.Context, val int) (*Subscription, error) {
   208  	notifier, supported := NotifierFromContext(ctx)
   209  	if !supported {
   210  		return nil, ErrNotificationsUnsupported
   211  	}
   212  	s.gotHangSubscriptionReq <- struct{}{}
   213  	<-s.unblockHangSubscription
   214  	subscription := notifier.CreateSubscription()
   215  
   216  	go func() {
   217  		notifier.Notify(subscription.ID, val)
   218  	}()
   219  	return subscription, nil
   220  }
   221  
   222  // largeRespService generates arbitrary-size JSON responses.
   223  type largeRespService struct {
   224  	length int
   225  }
   226  
   227  func (x largeRespService) LargeResp() string {
   228  	return strings.Repeat("x", x.length)
   229  }