testservice_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. package rpc
  17. import (
  18. "context"
  19. "encoding/binary"
  20. "errors"
  21. "strings"
  22. "sync"
  23. "time"
  24. )
  25. func newTestServer() *Server {
  26. server := NewServer()
  27. server.idgen = sequentialIDGenerator()
  28. if err := server.RegisterName("test", new(testService)); err != nil {
  29. panic(err)
  30. }
  31. if err := server.RegisterName("nftest", new(notificationTestService)); err != nil {
  32. panic(err)
  33. }
  34. return server
  35. }
  36. func sequentialIDGenerator() func() ID {
  37. var (
  38. mu sync.Mutex
  39. counter uint64
  40. )
  41. return func() ID {
  42. mu.Lock()
  43. defer mu.Unlock()
  44. counter++
  45. id := make([]byte, 8)
  46. binary.BigEndian.PutUint64(id, counter)
  47. return encodeID(id)
  48. }
  49. }
  50. type testService struct{}
  51. type echoArgs struct {
  52. S string
  53. }
  54. type echoResult struct {
  55. String string
  56. Int int
  57. Args *echoArgs
  58. }
  59. type testError struct{}
  60. func (testError) Error() string { return "testError" }
  61. func (testError) ErrorCode() int { return 444 }
  62. func (testError) ErrorData() interface{} { return "testError data" }
  63. func (s *testService) NoArgsRets() {}
  64. func (s *testService) Echo(str string, i int, args *echoArgs) echoResult {
  65. return echoResult{str, i, args}
  66. }
  67. func (s *testService) EchoWithCtx(ctx context.Context, str string, i int, args *echoArgs) echoResult {
  68. return echoResult{str, i, args}
  69. }
  70. func (s *testService) Sleep(ctx context.Context, duration time.Duration) {
  71. time.Sleep(duration)
  72. }
  73. func (s *testService) Block(ctx context.Context) error {
  74. <-ctx.Done()
  75. return errors.New("context canceled in testservice_block")
  76. }
  77. func (s *testService) Rets() (string, error) {
  78. return "", nil
  79. }
  80. //lint:ignore ST1008 returns error first on purpose.
  81. func (s *testService) InvalidRets1() (error, string) {
  82. return nil, ""
  83. }
  84. func (s *testService) InvalidRets2() (string, string) {
  85. return "", ""
  86. }
  87. func (s *testService) InvalidRets3() (string, string, error) {
  88. return "", "", nil
  89. }
  90. func (s *testService) ReturnError() error {
  91. return testError{}
  92. }
  93. func (s *testService) CallMeBack(ctx context.Context, method string, args []interface{}) (interface{}, error) {
  94. c, ok := ClientFromContext(ctx)
  95. if !ok {
  96. return nil, errors.New("no client")
  97. }
  98. var result interface{}
  99. err := c.Call(&result, method, args...)
  100. return result, err
  101. }
  102. func (s *testService) CallMeBackLater(ctx context.Context, method string, args []interface{}) error {
  103. c, ok := ClientFromContext(ctx)
  104. if !ok {
  105. return errors.New("no client")
  106. }
  107. go func() {
  108. <-ctx.Done()
  109. var result interface{}
  110. c.Call(&result, method, args...)
  111. }()
  112. return nil
  113. }
  114. func (s *testService) Subscription(ctx context.Context) (*Subscription, error) {
  115. return nil, nil
  116. }
  117. type notificationTestService struct {
  118. unsubscribed chan string
  119. gotHangSubscriptionReq chan struct{}
  120. unblockHangSubscription chan struct{}
  121. }
  122. func (s *notificationTestService) Echo(i int) int {
  123. return i
  124. }
  125. func (s *notificationTestService) Unsubscribe(subid string) {
  126. if s.unsubscribed != nil {
  127. s.unsubscribed <- subid
  128. }
  129. }
  130. func (s *notificationTestService) SomeSubscription(ctx context.Context, n, val int) (*Subscription, error) {
  131. notifier, supported := NotifierFromContext(ctx)
  132. if !supported {
  133. return nil, ErrNotificationsUnsupported
  134. }
  135. // By explicitly creating an subscription we make sure that the subscription id is send
  136. // back to the client before the first subscription.Notify is called. Otherwise the
  137. // events might be send before the response for the *_subscribe method.
  138. subscription := notifier.CreateSubscription()
  139. go func() {
  140. for i := 0; i < n; i++ {
  141. if err := notifier.Notify(subscription.ID, val+i); err != nil {
  142. return
  143. }
  144. }
  145. select {
  146. case <-notifier.Closed():
  147. case <-subscription.Err():
  148. }
  149. if s.unsubscribed != nil {
  150. s.unsubscribed <- string(subscription.ID)
  151. }
  152. }()
  153. return subscription, nil
  154. }
  155. // HangSubscription blocks on s.unblockHangSubscription before sending anything.
  156. func (s *notificationTestService) HangSubscription(ctx context.Context, val int) (*Subscription, error) {
  157. notifier, supported := NotifierFromContext(ctx)
  158. if !supported {
  159. return nil, ErrNotificationsUnsupported
  160. }
  161. s.gotHangSubscriptionReq <- struct{}{}
  162. <-s.unblockHangSubscription
  163. subscription := notifier.CreateSubscription()
  164. go func() {
  165. notifier.Notify(subscription.ID, val)
  166. }()
  167. return subscription, nil
  168. }
  169. // largeRespService generates arbitrary-size JSON responses.
  170. type largeRespService struct {
  171. length int
  172. }
  173. func (x largeRespService) LargeResp() string {
  174. return strings.Repeat("x", x.length)
  175. }