github.com/klaytn/klaytn@v1.12.1/event/subscription_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from event/subscription_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package event 22 23 import ( 24 "context" 25 "errors" 26 "testing" 27 "time" 28 ) 29 30 var errInts = errors.New("error in subscribeInts") 31 32 func subscribeInts(max, fail int, c chan<- int) Subscription { 33 return NewSubscription(func(quit <-chan struct{}) error { 34 for i := 0; i < max; i++ { 35 if i >= fail { 36 return errInts 37 } 38 select { 39 case c <- i: 40 case <-quit: 41 return nil 42 } 43 } 44 return nil 45 }) 46 } 47 48 func TestNewSubscriptionError(t *testing.T) { 49 t.Parallel() 50 51 channel := make(chan int) 52 sub := subscribeInts(10, 2, channel) 53 loop: 54 for want := 0; want < 10; want++ { 55 select { 56 case got := <-channel: 57 if got != want { 58 t.Fatalf("wrong int %d, want %d", got, want) 59 } 60 case err := <-sub.Err(): 61 if err != errInts { 62 t.Fatalf("wrong error: got %q, want %q", err, errInts) 63 } 64 if want != 2 { 65 t.Fatalf("got errInts at int %d, should be received at 2", want) 66 } 67 break loop 68 } 69 } 70 sub.Unsubscribe() 71 72 err, ok := <-sub.Err() 73 if err != nil { 74 t.Fatal("got non-nil error after Unsubscribe") 75 } 76 if ok { 77 t.Fatal("channel still open after Unsubscribe") 78 } 79 } 80 81 func TestResubscribe(t *testing.T) { 82 t.Parallel() 83 84 var i int 85 nfails := 6 86 sub := Resubscribe(100*time.Millisecond, func(ctx context.Context) (Subscription, error) { 87 // fmt.Printf("call #%d @ %v\n", i, time.Now()) 88 i++ 89 if i == 2 { 90 // Delay the second failure a bit to reset the resubscribe interval. 91 time.Sleep(200 * time.Millisecond) 92 } 93 if i < nfails { 94 return nil, errors.New("oops") 95 } 96 sub := NewSubscription(func(unsubscribed <-chan struct{}) error { return nil }) 97 return sub, nil 98 }) 99 100 <-sub.Err() 101 if i != nfails { 102 t.Fatalf("resubscribe function called %d times, want %d times", i, nfails) 103 } 104 } 105 106 func TestResubscribeAbort(t *testing.T) { 107 t.Parallel() 108 109 done := make(chan error) 110 sub := Resubscribe(0, func(ctx context.Context) (Subscription, error) { 111 select { 112 case <-ctx.Done(): 113 done <- nil 114 case <-time.After(2 * time.Second): 115 done <- errors.New("context given to resubscribe function not canceled within 2s") 116 } 117 return nil, nil 118 }) 119 120 sub.Unsubscribe() 121 if err := <-done; err != nil { 122 t.Fatal(err) 123 } 124 }