github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/event/filter/filter_test.go (about)

     1  package filter
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  // Simple test to check if baseline matching/mismatching filtering works.
     9  func TestFilters(t *testing.T) {
    10  	fm := New()
    11  	fm.Start()
    12  
    13  	// Register two filters to catch posted data
    14  	first := make(chan struct{})
    15  	fm.Install(Generic{
    16  		Str1: "hello",
    17  		Fn: func(data interface{}) {
    18  			first <- struct{}{}
    19  		},
    20  	})
    21  	second := make(chan struct{})
    22  	fm.Install(Generic{
    23  		Str1: "hello1",
    24  		Str2: "hello",
    25  		Fn: func(data interface{}) {
    26  			second <- struct{}{}
    27  		},
    28  	})
    29  	// Post an event that should only match the first filter
    30  	fm.Notify(Generic{Str1: "hello"}, true)
    31  	fm.Stop()
    32  
    33  	// Ensure only the mathcing filters fire
    34  	select {
    35  	case <-first:
    36  	case <-time.After(100 * time.Millisecond):
    37  		t.Error("matching filter timed out")
    38  	}
    39  	select {
    40  	case <-second:
    41  		t.Error("mismatching filter fired")
    42  	case <-time.After(100 * time.Millisecond):
    43  	}
    44  }