xmpp/filter_test.go
changeset 136 f35f853a52b6
child 153 bbd4166df95d
equal deleted inserted replaced
135:a4755a42af19 136:f35f853a52b6
       
     1 package xmpp
       
     2 
       
     3 import (
       
     4 	"fmt"
       
     5 	"strconv"
       
     6 	"testing"
       
     7 )
       
     8 
       
     9 func TestCloseIn(t *testing.T) {
       
    10 	add := make(chan Filter)
       
    11 	in := make(chan Stanza)
       
    12 	out := make(chan Stanza)
       
    13 	go filterMgr(add, in, out)
       
    14 	close(in)
       
    15 	_, ok := <-out
       
    16 	if ok {
       
    17 		fmt.Errorf("out didn't close")
       
    18 	}
       
    19 }
       
    20 
       
    21 func passthru(in <-chan Stanza, out chan<- Stanza) {
       
    22 	defer close(out)
       
    23 	for stan := range in {
       
    24 		out <- stan
       
    25 	}
       
    26 }
       
    27 
       
    28 func TestFilters(t *testing.T) {
       
    29 	for n := 0; n < 10; n++ {
       
    30 		filterN(n, t)
       
    31 	}
       
    32 }
       
    33 
       
    34 func filterN(numFilts int, t *testing.T) {
       
    35 	add := make(chan Filter)
       
    36 	in := make(chan Stanza)
       
    37 	out := make(chan Stanza)
       
    38 	go filterMgr(add, in, out)
       
    39 	for i := 0; i < numFilts; i++ {
       
    40 		add <- passthru
       
    41 	}
       
    42 	go func() {
       
    43 		for i := 0; i < 100; i++ {
       
    44 			msg := Message{}
       
    45 			msg.Id = fmt.Sprintf("%d", i)
       
    46 			in <- &msg
       
    47 		}
       
    48 	}()
       
    49 	for i := 0; i < 100; i++ {
       
    50 		stan := <-out
       
    51 		msg, ok := stan.(*Message)
       
    52 		if !ok {
       
    53 			t.Errorf("N = %d: msg %d not a Message: %#v", numFilts,
       
    54 				i, stan)
       
    55 			continue
       
    56 		}
       
    57 		n, err := strconv.Atoi(msg.Header.Id)
       
    58 		if err != nil {
       
    59 			t.Errorf("N = %d: msg %d parsing ID '%s': %v", numFilts,
       
    60 				i, msg.Header.Id, err)
       
    61 			continue
       
    62 		}
       
    63 		if n != i {
       
    64 			t.Errorf("N = %d: msg %d wrong id %d", numFilts, i, n)
       
    65 		}
       
    66 	}
       
    67 }