github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/event/filter/filter_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package filter 13 14 import ( 15 "testing" 16 "time" 17 ) 18 19 // Simple test to check if baseline matching/mismatching filtering works. 20 func TestFilters(t *testing.T) { 21 fm := New() 22 fm.Start() 23 24 // Register two filters to catch posted data 25 first := make(chan struct{}) 26 fm.Install(Generic{ 27 Str1: "hello", 28 Fn: func(data interface{}) { 29 first <- struct{}{} 30 }, 31 }) 32 second := make(chan struct{}) 33 fm.Install(Generic{ 34 Str1: "hello1", 35 Str2: "hello", 36 Fn: func(data interface{}) { 37 second <- struct{}{} 38 }, 39 }) 40 // Post an event that should only match the first filter 41 fm.Notify(Generic{Str1: "hello"}, true) 42 fm.Stop() 43 44 // Ensure only the mathcing filters fire 45 select { 46 case <-first: 47 case <-time.After(100 * time.Millisecond): 48 t.Error("matching filter timed out") 49 } 50 select { 51 case <-second: 52 t.Error("mismatching filter fired") 53 case <-time.After(100 * time.Millisecond): 54 } 55 }