github.com/ylsGit/go-ethereum@v1.6.5/event/filter/filter_test.go (about)

     1  // Copyright 2014 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  
    17  package filter
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  // Simple test to check if baseline matching/mismatching filtering works.
    25  func TestFilters(t *testing.T) {
    26  	fm := New()
    27  	fm.Start()
    28  
    29  	// Register two filters to catch posted data
    30  	first := make(chan struct{})
    31  	fm.Install(Generic{
    32  		Str1: "hello",
    33  		Fn: func(data interface{}) {
    34  			first <- struct{}{}
    35  		},
    36  	})
    37  	second := make(chan struct{})
    38  	fm.Install(Generic{
    39  		Str1: "hello1",
    40  		Str2: "hello",
    41  		Fn: func(data interface{}) {
    42  			second <- struct{}{}
    43  		},
    44  	})
    45  	// Post an event that should only match the first filter
    46  	fm.Notify(Generic{Str1: "hello"}, true)
    47  	fm.Stop()
    48  
    49  	// Ensure only the mathcing filters fire
    50  	select {
    51  	case <-first:
    52  	case <-time.After(100 * time.Millisecond):
    53  		t.Error("matching filter timed out")
    54  	}
    55  	select {
    56  	case <-second:
    57  		t.Error("mismatching filter fired")
    58  	case <-time.After(100 * time.Millisecond):
    59  	}
    60  }