xmpp/filter_test.go
author Chris Jones <christian.jones@sri.com>
Wed, 11 Sep 2013 17:17:50 -0600
changeset 140 f94b958f8df0
parent 136 f35f853a52b6
child 153 bbd4166df95d
permissions -rw-r--r--
Don't close the wrong end of a channel. But if we get an error, we can close the underlying socket.

package xmpp

import (
	"fmt"
	"strconv"
	"testing"
)

func TestCloseIn(t *testing.T) {
	add := make(chan Filter)
	in := make(chan Stanza)
	out := make(chan Stanza)
	go filterMgr(add, in, out)
	close(in)
	_, ok := <-out
	if ok {
		fmt.Errorf("out didn't close")
	}
}

func passthru(in <-chan Stanza, out chan<- Stanza) {
	defer close(out)
	for stan := range in {
		out <- stan
	}
}

func TestFilters(t *testing.T) {
	for n := 0; n < 10; n++ {
		filterN(n, t)
	}
}

func filterN(numFilts int, t *testing.T) {
	add := make(chan Filter)
	in := make(chan Stanza)
	out := make(chan Stanza)
	go filterMgr(add, in, out)
	for i := 0; i < numFilts; i++ {
		add <- passthru
	}
	go func() {
		for i := 0; i < 100; i++ {
			msg := Message{}
			msg.Id = fmt.Sprintf("%d", i)
			in <- &msg
		}
	}()
	for i := 0; i < 100; i++ {
		stan := <-out
		msg, ok := stan.(*Message)
		if !ok {
			t.Errorf("N = %d: msg %d not a Message: %#v", numFilts,
				i, stan)
			continue
		}
		n, err := strconv.Atoi(msg.Header.Id)
		if err != nil {
			t.Errorf("N = %d: msg %d parsing ID '%s': %v", numFilts,
				i, msg.Header.Id, err)
			continue
		}
		if n != i {
			t.Errorf("N = %d: msg %d wrong id %d", numFilts, i, n)
		}
	}
}