github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/event/feed_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2016 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package event
    26  
    27  import (
    28  	"fmt"
    29  	"reflect"
    30  	"sync"
    31  	"testing"
    32  	"time"
    33  )
    34  
    35  func TestFeedPanics(t *testing.T) {
    36  	{
    37  		var f Feed
    38  		f.Send(int(2))
    39  		want := feedTypeError{op: "Send", got: reflect.TypeOf(uint64(0)), want: reflect.TypeOf(int(0))}
    40  		if err := checkPanic(want, func() { f.Send(uint64(2)) }); err != nil {
    41  			t.Error(err)
    42  		}
    43  	}
    44  	{
    45  		var f Feed
    46  		ch := make(chan int)
    47  		f.Subscribe(ch)
    48  		want := feedTypeError{op: "Send", got: reflect.TypeOf(uint64(0)), want: reflect.TypeOf(int(0))}
    49  		if err := checkPanic(want, func() { f.Send(uint64(2)) }); err != nil {
    50  			t.Error(err)
    51  		}
    52  	}
    53  	{
    54  		var f Feed
    55  		f.Send(int(2))
    56  		want := feedTypeError{op: "Subscribe", got: reflect.TypeOf(make(chan uint64)), want: reflect.TypeOf(make(chan<- int))}
    57  		if err := checkPanic(want, func() { f.Subscribe(make(chan uint64)) }); err != nil {
    58  			t.Error(err)
    59  		}
    60  	}
    61  	{
    62  		var f Feed
    63  		if err := checkPanic(errBadChannel, func() { f.Subscribe(make(<-chan int)) }); err != nil {
    64  			t.Error(err)
    65  		}
    66  	}
    67  	{
    68  		var f Feed
    69  		if err := checkPanic(errBadChannel, func() { f.Subscribe(int(0)) }); err != nil {
    70  			t.Error(err)
    71  		}
    72  	}
    73  }
    74  
    75  func checkPanic(want error, fn func()) (err error) {
    76  	defer func() {
    77  		panic := recover()
    78  		if panic == nil {
    79  			err = fmt.Errorf("didn't panic")
    80  		} else if !reflect.DeepEqual(panic, want) {
    81  			err = fmt.Errorf("panicked with wrong error: got %q, want %q", panic, want)
    82  		}
    83  	}()
    84  	fn()
    85  	return nil
    86  }
    87  
    88  func TestFeed(t *testing.T) {
    89  	var feed Feed
    90  	var done, subscribed sync.WaitGroup
    91  	subscriber := func(i int) {
    92  		defer done.Done()
    93  
    94  		subchan := make(chan int)
    95  		sub := feed.Subscribe(subchan)
    96  		timeout := time.NewTimer(2 * time.Second)
    97  		subscribed.Done()
    98  
    99  		select {
   100  		case v := <-subchan:
   101  			if v != 1 {
   102  				t.Errorf("%d: received value %d, want 1", i, v)
   103  			}
   104  		case <-timeout.C:
   105  			t.Errorf("%d: receive timeout", i)
   106  		}
   107  
   108  		sub.Unsubscribe()
   109  		select {
   110  		case _, ok := <-sub.Err():
   111  			if ok {
   112  				t.Errorf("%d: error channel not closed after unsubscribe", i)
   113  			}
   114  		case <-timeout.C:
   115  			t.Errorf("%d: unsubscribe timeout", i)
   116  		}
   117  	}
   118  
   119  	const n = 1000
   120  	done.Add(n)
   121  	subscribed.Add(n)
   122  	for i := 0; i < n; i++ {
   123  		go subscriber(i)
   124  	}
   125  	subscribed.Wait()
   126  	if nsent := feed.Send(1); nsent != n {
   127  		t.Errorf("first send delivered %d times, want %d", nsent, n)
   128  	}
   129  	if nsent := feed.Send(2); nsent != 0 {
   130  		t.Errorf("second send delivered %d times, want 0", nsent)
   131  	}
   132  	done.Wait()
   133  }
   134  
   135  func TestFeedSubscribeSameChannel(t *testing.T) {
   136  	var (
   137  		feed Feed
   138  		done sync.WaitGroup
   139  		ch   = make(chan int)
   140  		sub1 = feed.Subscribe(ch)
   141  		sub2 = feed.Subscribe(ch)
   142  		_    = feed.Subscribe(ch)
   143  	)
   144  	expectSends := func(value, n int) {
   145  		if nsent := feed.Send(value); nsent != n {
   146  			t.Errorf("send delivered %d times, want %d", nsent, n)
   147  		}
   148  		done.Done()
   149  	}
   150  	expectRecv := func(wantValue, n int) {
   151  		for i := 0; i < n; i++ {
   152  			if v := <-ch; v != wantValue {
   153  				t.Errorf("received %d, want %d", v, wantValue)
   154  			}
   155  		}
   156  	}
   157  
   158  	done.Add(1)
   159  	go expectSends(1, 3)
   160  	expectRecv(1, 3)
   161  	done.Wait()
   162  
   163  	sub1.Unsubscribe()
   164  
   165  	done.Add(1)
   166  	go expectSends(2, 2)
   167  	expectRecv(2, 2)
   168  	done.Wait()
   169  
   170  	sub2.Unsubscribe()
   171  
   172  	done.Add(1)
   173  	go expectSends(3, 1)
   174  	expectRecv(3, 1)
   175  	done.Wait()
   176  }
   177  
   178  func TestFeedSubscribeBlockedPost(t *testing.T) {
   179  	var (
   180  		feed   Feed
   181  		nsends = 2000
   182  		ch1    = make(chan int)
   183  		ch2    = make(chan int)
   184  		wg     sync.WaitGroup
   185  	)
   186  	defer wg.Wait()
   187  
   188  	feed.Subscribe(ch1)
   189  	wg.Add(nsends)
   190  	for i := 0; i < nsends; i++ {
   191  		go func() {
   192  			feed.Send(99)
   193  			wg.Done()
   194  		}()
   195  	}
   196  
   197  	sub2 := feed.Subscribe(ch2)
   198  	defer sub2.Unsubscribe()
   199  
   200  //当ch1收到n次时,我们就完成了。
   201  //CH2上的接收数取决于调度。
   202  	for i := 0; i < nsends; {
   203  		select {
   204  		case <-ch1:
   205  			i++
   206  		case <-ch2:
   207  		}
   208  	}
   209  }
   210  
   211  func TestFeedUnsubscribeBlockedPost(t *testing.T) {
   212  	var (
   213  		feed   Feed
   214  		nsends = 200
   215  		chans  = make([]chan int, 2000)
   216  		subs   = make([]Subscription, len(chans))
   217  		bchan  = make(chan int)
   218  		bsub   = feed.Subscribe(bchan)
   219  		wg     sync.WaitGroup
   220  	)
   221  	for i := range chans {
   222  		chans[i] = make(chan int, nsends)
   223  	}
   224  
   225  //排队发送一些邮件。当bchan不被读取时,这些都不能取得进展。
   226  	wg.Add(nsends)
   227  	for i := 0; i < nsends; i++ {
   228  		go func() {
   229  			feed.Send(99)
   230  			wg.Done()
   231  		}()
   232  	}
   233  //订阅其他频道。
   234  	for i, ch := range chans {
   235  		subs[i] = feed.Subscribe(ch)
   236  	}
   237  //再次取消订阅。
   238  	for _, sub := range subs {
   239  		sub.Unsubscribe()
   240  	}
   241  //取消阻止发送。
   242  	bsub.Unsubscribe()
   243  	wg.Wait()
   244  }
   245  
   246  //检查在发送期间取消订阅频道是否有效
   247  //频道已发送。
   248  func TestFeedUnsubscribeSentChan(t *testing.T) {
   249  	var (
   250  		feed Feed
   251  		ch1  = make(chan int)
   252  		ch2  = make(chan int)
   253  		sub1 = feed.Subscribe(ch1)
   254  		sub2 = feed.Subscribe(ch2)
   255  		wg   sync.WaitGroup
   256  	)
   257  	defer sub2.Unsubscribe()
   258  
   259  	wg.Add(1)
   260  	go func() {
   261  		feed.Send(0)
   262  		wg.Done()
   263  	}()
   264  
   265  //等待ch1上的值。
   266  	<-ch1
   267  //取消订阅ch1,将其从发送案例中删除。
   268  	sub1.Unsubscribe()
   269  
   270  //接收CH2,完成发送。
   271  	<-ch2
   272  	wg.Wait()
   273  
   274  //再发一次。这应该只发送到ch2,因此等待组将取消阻止
   275  //一旦收到CH2上的值。
   276  	wg.Add(1)
   277  	go func() {
   278  		feed.Send(0)
   279  		wg.Done()
   280  	}()
   281  	<-ch2
   282  	wg.Wait()
   283  }
   284  
   285  func TestFeedUnsubscribeFromInbox(t *testing.T) {
   286  	var (
   287  		feed Feed
   288  		ch1  = make(chan int)
   289  		ch2  = make(chan int)
   290  		sub1 = feed.Subscribe(ch1)
   291  		sub2 = feed.Subscribe(ch1)
   292  		sub3 = feed.Subscribe(ch2)
   293  	)
   294  	if len(feed.inbox) != 3 {
   295  		t.Errorf("inbox length != 3 after subscribe")
   296  	}
   297  	if len(feed.sendCases) != 1 {
   298  		t.Errorf("sendCases is non-empty after unsubscribe")
   299  	}
   300  
   301  	sub1.Unsubscribe()
   302  	sub2.Unsubscribe()
   303  	sub3.Unsubscribe()
   304  	if len(feed.inbox) != 0 {
   305  		t.Errorf("inbox is non-empty after unsubscribe")
   306  	}
   307  	if len(feed.sendCases) != 1 {
   308  		t.Errorf("sendCases is non-empty after unsubscribe")
   309  	}
   310  }
   311  
   312  func BenchmarkFeedSend1000(b *testing.B) {
   313  	var (
   314  		done  sync.WaitGroup
   315  		feed  Feed
   316  		nsubs = 1000
   317  	)
   318  	subscriber := func(ch <-chan int) {
   319  		for i := 0; i < b.N; i++ {
   320  			<-ch
   321  		}
   322  		done.Done()
   323  	}
   324  	done.Add(nsubs)
   325  	for i := 0; i < nsubs; i++ {
   326  		ch := make(chan int, 200)
   327  		feed.Subscribe(ch)
   328  		go subscriber(ch)
   329  	}
   330  
   331  //实际基准。
   332  	b.ResetTimer()
   333  	for i := 0; i < b.N; i++ {
   334  		if feed.Send(i) != nsubs {
   335  			panic("wrong number of sends")
   336  		}
   337  	}
   338  
   339  	b.StopTimer()
   340  	done.Wait()
   341  }