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