diff options
author | bas-vk <bas-vk@users.noreply.github.com> | 2016-07-25 16:07:05 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-25 16:07:05 +0800 |
commit | 771655e3fee585ce4bc47dfaa279557c6c1c2421 (patch) | |
tree | 9071e157a54c40f06f0e5895643c82ca9a0b037a /rpc/notification_test.go | |
parent | 60cd5bf9397bd8331bce3bb1884524d43c31dbb5 (diff) | |
parent | 91b769042857f542b2792b23ec407e1c9bd4fe8d (diff) | |
download | dexon-771655e3fee585ce4bc47dfaa279557c6c1c2421.tar dexon-771655e3fee585ce4bc47dfaa279557c6c1c2421.tar.gz dexon-771655e3fee585ce4bc47dfaa279557c6c1c2421.tar.bz2 dexon-771655e3fee585ce4bc47dfaa279557c6c1c2421.tar.lz dexon-771655e3fee585ce4bc47dfaa279557c6c1c2421.tar.xz dexon-771655e3fee585ce4bc47dfaa279557c6c1c2421.tar.zst dexon-771655e3fee585ce4bc47dfaa279557c6c1c2421.zip |
Merge pull request #2808 from fjl/rpc-client-3
rpc: add new client, use it everywhere
Diffstat (limited to 'rpc/notification_test.go')
-rw-r--r-- | rpc/notification_test.go | 45 |
1 files changed, 38 insertions, 7 deletions
diff --git a/rpc/notification_test.go b/rpc/notification_test.go index 1bcede177..280503222 100644 --- a/rpc/notification_test.go +++ b/rpc/notification_test.go @@ -19,20 +19,31 @@ package rpc import ( "encoding/json" "net" + "sync" "testing" "time" "golang.org/x/net/context" ) -type NotificationTestService struct{} +type NotificationTestService struct { + mu sync.Mutex + unsubscribed bool -var ( - unsubCallbackCalled = false -) + gotHangSubscriptionReq chan struct{} + unblockHangSubscription chan struct{} +} + +func (s *NotificationTestService) wasUnsubCallbackCalled() bool { + s.mu.Lock() + defer s.mu.Unlock() + return s.unsubscribed +} func (s *NotificationTestService) Unsubscribe(subid string) { - unsubCallbackCalled = true + s.mu.Lock() + s.unsubscribed = true + s.mu.Unlock() } func (s *NotificationTestService) SomeSubscription(ctx context.Context, n, val int) (Subscription, error) { @@ -60,6 +71,26 @@ func (s *NotificationTestService) SomeSubscription(ctx context.Context, n, val i return subscription, nil } +// HangSubscription blocks on s.unblockHangSubscription before +// sending anything. +func (s *NotificationTestService) HangSubscription(ctx context.Context, val int) (Subscription, error) { + notifier, supported := NotifierFromContext(ctx) + if !supported { + return nil, ErrNotificationsUnsupported + } + + s.gotHangSubscriptionReq <- struct{}{} + <-s.unblockHangSubscription + subscription, err := notifier.NewSubscription(s.Unsubscribe) + if err != nil { + return nil, err + } + go func() { + subscription.Notify(val) + }() + return subscription, nil +} + func TestNotifications(t *testing.T) { server := NewServer() service := &NotificationTestService{} @@ -90,7 +121,7 @@ func TestNotifications(t *testing.T) { } var subid string - response := JSONSuccessResponse{Result: subid} + response := jsonSuccessResponse{Result: subid} if err := in.Decode(&response); err != nil { t.Fatal(err) } @@ -114,7 +145,7 @@ func TestNotifications(t *testing.T) { clientConn.Close() // causes notification unsubscribe callback to be called time.Sleep(1 * time.Second) - if !unsubCallbackCalled { + if !service.wasUnsubCallbackCalled() { t.Error("unsubscribe callback not called after closing connection") } } |