xmpp/filter_test.go
author Chris Jones <christian.jones@sri.com>
Sun, 15 Sep 2013 12:00:17 -0600
changeset 143 62166e57800e
parent 136 f35f853a52b6
child 153 bbd4166df95d
permissions -rw-r--r--
Split stream.go into layer1, layer2, layer3, and sasl.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
136
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     1
package xmpp
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     2
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     3
import (
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     4
	"fmt"
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     5
	"strconv"
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     6
	"testing"
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     7
)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     8
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     9
func TestCloseIn(t *testing.T) {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    10
	add := make(chan Filter)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    11
	in := make(chan Stanza)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    12
	out := make(chan Stanza)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    13
	go filterMgr(add, in, out)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    14
	close(in)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    15
	_, ok := <-out
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    16
	if ok {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    17
		fmt.Errorf("out didn't close")
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    18
	}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    19
}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    20
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    21
func passthru(in <-chan Stanza, out chan<- Stanza) {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    22
	defer close(out)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    23
	for stan := range in {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    24
		out <- stan
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    25
	}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    26
}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    27
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    28
func TestFilters(t *testing.T) {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    29
	for n := 0; n < 10; n++ {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    30
		filterN(n, t)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    31
	}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    32
}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    33
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    34
func filterN(numFilts int, t *testing.T) {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    35
	add := make(chan Filter)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    36
	in := make(chan Stanza)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    37
	out := make(chan Stanza)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    38
	go filterMgr(add, in, out)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    39
	for i := 0; i < numFilts; i++ {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    40
		add <- passthru
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    41
	}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    42
	go func() {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    43
		for i := 0; i < 100; i++ {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    44
			msg := Message{}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    45
			msg.Id = fmt.Sprintf("%d", i)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    46
			in <- &msg
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    47
		}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    48
	}()
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    49
	for i := 0; i < 100; i++ {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    50
		stan := <-out
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    51
		msg, ok := stan.(*Message)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    52
		if !ok {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    53
			t.Errorf("N = %d: msg %d not a Message: %#v", numFilts,
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    54
				i, stan)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    55
			continue
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    56
		}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    57
		n, err := strconv.Atoi(msg.Header.Id)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    58
		if err != nil {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    59
			t.Errorf("N = %d: msg %d parsing ID '%s': %v", numFilts,
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    60
				i, msg.Header.Id, err)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    61
			continue
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    62
		}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    63
		if n != i {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    64
			t.Errorf("N = %d: msg %d wrong id %d", numFilts, i, n)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    65
		}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    66
	}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    67
}