1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 func TestBuffer(t *testing.T) {
13 afterChan := make(chan time.Time)
14 ch := make(chan *Message)
16 ch <- &Message{Kind: "err", Body: "a"}
17 ch <- &Message{Kind: "err", Body: "b"}
18 ch <- &Message{Kind: "out", Body: "1"}
19 ch <- &Message{Kind: "out", Body: "2"}
20 afterChan <- time.Time{} // value itself doesn't matter
21 ch <- &Message{Kind: "out", Body: "3"}
22 ch <- &Message{Kind: "out", Body: "4"}
27 timeAfter := func(d time.Duration) <-chan time.Time {
30 for m := range buffer(ch, timeAfter) {
34 t.Fatalf("got %v messages, want 3", len(ms))
36 if g, w := ms[0].Body, "ab"; g != w {
37 t.Errorf("message 0 body = %q, want %q", g, w)
39 if g, w := ms[1].Body, "12"; g != w {
40 t.Errorf("message 1 body = %q, want %q", g, w)
42 if g, w := ms[2].Body, "34"; g != w {
43 t.Errorf("message 2 body = %q, want %q", g, w)
47 type killRecorder chan struct{}
49 func (k killRecorder) Kill() { close(k) }
51 func TestLimiter(t *testing.T) {
52 ch := make(chan *Message)
55 for i := 0; i < msgLimit+10; i++ {
58 ch <- &Message{Kind: "end"}
61 kr := make(killRecorder)
63 for m := range limiter(ch, kr) {
65 if n > msgLimit && m.Kind != "end" {
66 t.Errorf("received non-end message after limit")
70 t.Errorf("received %v messages, want %v", n, msgLimit+1)
74 case <-time.After(100 * time.Millisecond):
75 t.Errorf("process wasn't killed after reaching limit")