github.com/huayun321/go-ethereum@v1.19.19/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  func (s *testService) NoArgsRets() {}
    74  
    75  func (s *testService) Echo(str string, i int, args *echoArgs) echoResult {
    76  	return echoResult{str, i, args}
    77  }
    78  
    79  func (s *testService) EchoWithCtx(ctx context.Context, str string, i int, args *echoArgs) echoResult {
    80  	return echoResult{str, i, args}
    81  }
    82  
    83  func (s *testService) Sleep(ctx context.Context, duration time.Duration) {
    84  	time.Sleep(duration)
    85  }
    86  
    87  func (s *testService) Block(ctx context.Context) error {
    88  	<-ctx.Done()
    89  	return errors.New("context canceled in testservice_block")
    90  }
    91  
    92  func (s *testService) Rets() (string, error) {
    93  	return "", nil
    94  }
    95  
    96  //lint:ignore ST1008 returns error first on purpose.
    97  func (s *testService) InvalidRets1() (error, string) {
    98  	return nil, ""
    99  }
   100  
   101  func (s *testService) InvalidRets2() (string, string) {
   102  	return "", ""
   103  }
   104  
   105  func (s *testService) InvalidRets3() (string, string, error) {
   106  	return "", "", nil
   107  }
   108  
   109  func (s *testService) ReturnError() error {
   110  	return testError{}
   111  }
   112  
   113  func (s *testService) CallMeBack(ctx context.Context, method string, args []interface{}) (interface{}, error) {
   114  	c, ok := ClientFromContext(ctx)
   115  	if !ok {
   116  		return nil, errors.New("no client")
   117  	}
   118  	var result interface{}
   119  	err := c.Call(&result, method, args...)
   120  	return result, err
   121  }
   122  
   123  func (s *testService) CallMeBackLater(ctx context.Context, method string, args []interface{}) error {
   124  	c, ok := ClientFromContext(ctx)
   125  	if !ok {
   126  		return errors.New("no client")
   127  	}
   128  	go func() {
   129  		<-ctx.Done()
   130  		var result interface{}
   131  		c.Call(&result, method, args...)
   132  	}()
   133  	return nil
   134  }
   135  
   136  func (s *testService) Subscription(ctx context.Context) (*Subscription, error) {
   137  	return nil, nil
   138  }
   139  
   140  type notificationTestService struct {
   141  	unsubscribed            chan string
   142  	gotHangSubscriptionReq  chan struct{}
   143  	unblockHangSubscription chan struct{}
   144  }
   145  
   146  func (s *notificationTestService) Echo(i int) int {
   147  	return i
   148  }
   149  
   150  func (s *notificationTestService) Unsubscribe(subid string) {
   151  	if s.unsubscribed != nil {
   152  		s.unsubscribed <- subid
   153  	}
   154  }
   155  
   156  func (s *notificationTestService) SomeSubscription(ctx context.Context, n, val int) (*Subscription, error) {
   157  	notifier, supported := NotifierFromContext(ctx)
   158  	if !supported {
   159  		return nil, ErrNotificationsUnsupported
   160  	}
   161  
   162  	// By explicitly creating an subscription we make sure that the subscription id is send
   163  	// back to the client before the first subscription.Notify is called. Otherwise the
   164  	// events might be send before the response for the *_subscribe method.
   165  	subscription := notifier.CreateSubscription()
   166  	go func() {
   167  		for i := 0; i < n; i++ {
   168  			if err := notifier.Notify(subscription.ID, val+i); err != nil {
   169  				return
   170  			}
   171  		}
   172  		select {
   173  		case <-notifier.Closed():
   174  		case <-subscription.Err():
   175  		}
   176  		if s.unsubscribed != nil {
   177  			s.unsubscribed <- string(subscription.ID)
   178  		}
   179  	}()
   180  	return subscription, nil
   181  }
   182  
   183  // HangSubscription blocks on s.unblockHangSubscription before sending anything.
   184  func (s *notificationTestService) HangSubscription(ctx context.Context, val int) (*Subscription, error) {
   185  	notifier, supported := NotifierFromContext(ctx)
   186  	if !supported {
   187  		return nil, ErrNotificationsUnsupported
   188  	}
   189  	s.gotHangSubscriptionReq <- struct{}{}
   190  	<-s.unblockHangSubscription
   191  	subscription := notifier.CreateSubscription()
   192  
   193  	go func() {
   194  		notifier.Notify(subscription.ID, val)
   195  	}()
   196  	return subscription, nil
   197  }
   198  
   199  // largeRespService generates arbitrary-size JSON responses.
   200  type largeRespService struct {
   201  	length int
   202  }
   203  
   204  func (x largeRespService) LargeResp() string {
   205  	return strings.Repeat("x", x.length)
   206  }