xmpp/filter_test.go
author Chris Jones <chris@cjones.org>
Thu, 17 Oct 2013 22:41:08 -0600
changeset 171 2f6ae54a2bd1
parent 153 bbd4166df95d
permissions -rw-r--r--
Doc fix.
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)
153
bbd4166df95d Simplified the API: There's only one constructor, and it does everything necessary to initiate the stream. StartSession() and Roster.Update() have both been eliminated.
Chris Jones <christian.jones@sri.com>
parents: 136
diff changeset
    37
	defer close(in)
136
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    38
	out := make(chan Stanza)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    39
	go filterMgr(add, in, out)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    40
	for i := 0; i < numFilts; i++ {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    41
		add <- passthru
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    42
	}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    43
	go func() {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    44
		for i := 0; i < 100; i++ {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    45
			msg := Message{}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    46
			msg.Id = fmt.Sprintf("%d", i)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    47
			in <- &msg
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
	}()
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    50
	for i := 0; i < 100; i++ {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    51
		stan := <-out
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    52
		msg, ok := stan.(*Message)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    53
		if !ok {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    54
			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
    55
				i, stan)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    56
			continue
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    57
		}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    58
		n, err := strconv.Atoi(msg.Header.Id)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    59
		if err != nil {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    60
			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
    61
				i, msg.Header.Id, err)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    62
			continue
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    63
		}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    64
		if n != i {
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    65
			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
    66
		}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    67
	}
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    68
}