github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/event/filter/filter_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:39</date>
    10  //</624342639830896640>
    11  
    12  
    13  package filter
    14  
    15  import (
    16  	"testing"
    17  	"time"
    18  )
    19  
    20  //简单测试,检查基线匹配/不匹配过滤是否有效。
    21  func TestFilters(t *testing.T) {
    22  	fm := New()
    23  	fm.Start()
    24  
    25  //注册两个筛选器以捕获已发布的数据
    26  	first := make(chan struct{})
    27  	fm.Install(Generic{
    28  		Str1: "hello",
    29  		Fn: func(data interface{}) {
    30  			first <- struct{}{}
    31  		},
    32  	})
    33  	second := make(chan struct{})
    34  	fm.Install(Generic{
    35  		Str1: "hello1",
    36  		Str2: "hello",
    37  		Fn: func(data interface{}) {
    38  			second <- struct{}{}
    39  		},
    40  	})
    41  //发布只应与第一个筛选器匹配的事件
    42  	fm.Notify(Generic{Str1: "hello"}, true)
    43  	fm.Stop()
    44  
    45  //确保只有Mathing过滤器启动
    46  	select {
    47  	case <-first:
    48  	case <-time.After(100 * time.Millisecond):
    49  		t.Error("matching filter timed out")
    50  	}
    51  	select {
    52  	case <-second:
    53  		t.Error("mismatching filter fired")
    54  	case <-time.After(100 * time.Millisecond):
    55  	}
    56  }
    57