notification_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "testing"
  21. "time"
  22. "golang.org/x/net/context"
  23. )
  24. type NotificationTestService struct{}
  25. var (
  26. unsubCallbackCalled = false
  27. )
  28. func (s *NotificationTestService) Unsubscribe(subid string) {
  29. unsubCallbackCalled = true
  30. }
  31. func (s *NotificationTestService) SomeSubscription(ctx context.Context, n, val int) (Subscription, error) {
  32. notifier, supported := ctx.Value(NotifierContextKey).(Notifier)
  33. if !supported {
  34. return nil, ErrNotificationsUnsupported
  35. }
  36. // by explicitly creating an subscription we make sure that the subscription id is send back to the client
  37. // before the first subscription.Notify is called. Otherwise the events might be send before the response
  38. // for the eth_subscribe method.
  39. subscription, err := notifier.NewSubscription(s.Unsubscribe)
  40. if err != nil {
  41. return nil, err
  42. }
  43. go func() {
  44. for i := 0; i < n; i++ {
  45. if err := subscription.Notify(val + i); err != nil {
  46. return
  47. }
  48. }
  49. }()
  50. return subscription, nil
  51. }
  52. func TestNotifications(t *testing.T) {
  53. server := NewServer()
  54. service := &NotificationTestService{}
  55. if err := server.RegisterName("eth", service); err != nil {
  56. t.Fatalf("unable to register test service %v", err)
  57. }
  58. clientConn, serverConn := net.Pipe()
  59. go server.ServeCodec(NewJSONCodec(serverConn), OptionMethodInvocation|OptionSubscriptions)
  60. out := json.NewEncoder(clientConn)
  61. in := json.NewDecoder(clientConn)
  62. n := 5
  63. val := 12345
  64. request := map[string]interface{}{
  65. "id": 1,
  66. "method": "eth_subscribe",
  67. "version": "2.0",
  68. "params": []interface{}{"someSubscription", n, val},
  69. }
  70. // create subscription
  71. if err := out.Encode(request); err != nil {
  72. t.Fatal(err)
  73. }
  74. var subid string
  75. response := JSONSuccessResponse{Result: subid}
  76. if err := in.Decode(&response); err != nil {
  77. t.Fatal(err)
  78. }
  79. var ok bool
  80. if subid, ok = response.Result.(string); !ok {
  81. t.Fatalf("expected subscription id, got %T", response.Result)
  82. }
  83. for i := 0; i < n; i++ {
  84. var notification jsonNotification
  85. if err := in.Decode(&notification); err != nil {
  86. t.Fatalf("%v", err)
  87. }
  88. if int(notification.Params.Result.(float64)) != val+i {
  89. t.Fatalf("expected %d, got %d", val+i, notification.Params.Result)
  90. }
  91. }
  92. clientConn.Close() // causes notification unsubscribe callback to be called
  93. time.Sleep(1 * time.Second)
  94. if !unsubCallbackCalled {
  95. t.Error("unsubscribe callback not called after closing connection")
  96. }
  97. }