subscription_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2016 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. "encoding/json"
  19. "net"
  20. "sync"
  21. "testing"
  22. "time"
  23. "golang.org/x/net/context"
  24. )
  25. type NotificationTestService struct {
  26. mu sync.Mutex
  27. unsubscribed bool
  28. gotHangSubscriptionReq chan struct{}
  29. unblockHangSubscription chan struct{}
  30. }
  31. func (s *NotificationTestService) Echo(i int) int {
  32. return i
  33. }
  34. func (s *NotificationTestService) wasUnsubCallbackCalled() bool {
  35. s.mu.Lock()
  36. defer s.mu.Unlock()
  37. return s.unsubscribed
  38. }
  39. func (s *NotificationTestService) Unsubscribe(subid string) {
  40. s.mu.Lock()
  41. s.unsubscribed = true
  42. s.mu.Unlock()
  43. }
  44. func (s *NotificationTestService) SomeSubscription(ctx context.Context, n, val int) (*Subscription, error) {
  45. notifier, supported := NotifierFromContext(ctx)
  46. if !supported {
  47. return nil, ErrNotificationsUnsupported
  48. }
  49. // by explicitly creating an subscription we make sure that the subscription id is send back to the client
  50. // before the first subscription.Notify is called. Otherwise the events might be send before the response
  51. // for the eth_subscribe method.
  52. subscription := notifier.CreateSubscription()
  53. go func() {
  54. // test expects n events, if we begin sending event immediately some events
  55. // will probably be dropped since the subscription ID might not be send to
  56. // the client.
  57. time.Sleep(5 * time.Second)
  58. for i := 0; i < n; i++ {
  59. if err := notifier.Notify(subscription.ID, val+i); err != nil {
  60. return
  61. }
  62. }
  63. select {
  64. case <-notifier.Closed():
  65. s.mu.Lock()
  66. s.unsubscribed = true
  67. s.mu.Unlock()
  68. case <-subscription.Err():
  69. s.mu.Lock()
  70. s.unsubscribed = true
  71. s.mu.Unlock()
  72. }
  73. }()
  74. return subscription, nil
  75. }
  76. // HangSubscription blocks on s.unblockHangSubscription before
  77. // sending anything.
  78. func (s *NotificationTestService) HangSubscription(ctx context.Context, val int) (*Subscription, error) {
  79. notifier, supported := NotifierFromContext(ctx)
  80. if !supported {
  81. return nil, ErrNotificationsUnsupported
  82. }
  83. s.gotHangSubscriptionReq <- struct{}{}
  84. <-s.unblockHangSubscription
  85. subscription := notifier.CreateSubscription()
  86. go func() {
  87. notifier.Notify(subscription.ID, val)
  88. }()
  89. return subscription, nil
  90. }
  91. func TestNotifications(t *testing.T) {
  92. server := NewServer()
  93. service := &NotificationTestService{}
  94. if err := server.RegisterName("eth", service); err != nil {
  95. t.Fatalf("unable to register test service %v", err)
  96. }
  97. clientConn, serverConn := net.Pipe()
  98. go server.ServeCodec(NewJSONCodec(serverConn), OptionMethodInvocation|OptionSubscriptions)
  99. out := json.NewEncoder(clientConn)
  100. in := json.NewDecoder(clientConn)
  101. n := 5
  102. val := 12345
  103. request := map[string]interface{}{
  104. "id": 1,
  105. "method": "eth_subscribe",
  106. "version": "2.0",
  107. "params": []interface{}{"someSubscription", n, val},
  108. }
  109. // create subscription
  110. if err := out.Encode(request); err != nil {
  111. t.Fatal(err)
  112. }
  113. var subid string
  114. response := jsonSuccessResponse{Result: subid}
  115. if err := in.Decode(&response); err != nil {
  116. t.Fatal(err)
  117. }
  118. var ok bool
  119. if _, ok = response.Result.(string); !ok {
  120. t.Fatalf("expected subscription id, got %T", response.Result)
  121. }
  122. for i := 0; i < n; i++ {
  123. var notification jsonNotification
  124. if err := in.Decode(&notification); err != nil {
  125. t.Fatalf("%v", err)
  126. }
  127. if int(notification.Params.Result.(float64)) != val+i {
  128. t.Fatalf("expected %d, got %d", val+i, notification.Params.Result)
  129. }
  130. }
  131. clientConn.Close() // causes notification unsubscribe callback to be called
  132. time.Sleep(1 * time.Second)
  133. if !service.wasUnsubCallbackCalled() {
  134. t.Error("unsubscribe callback not called after closing connection")
  135. }
  136. }