testservice_test.go 4.4 KB

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