forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_test.go
85 lines (68 loc) · 1.83 KB
/
sub_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
// +build !oss
package pubsub
import (
"testing"
"github.com/drone/drone/core"
)
func nop(*core.Message) {}
func TestSubscription_publish(t *testing.T) {
s := &subscriber{
handler: make(chan *core.Message, 5),
quit: make(chan struct{}),
}
e := new(core.Message)
s.publish(e)
if got, want := len(s.handler), 1; got != want {
t.Errorf("Want buffered channel size %d, got %d", want, got)
}
if got, want := <-s.handler, e; got != want {
t.Errorf("Want event received from channel")
}
if got, want := len(s.handler), 0; got != want {
t.Errorf("Want buffered channel size %d, got %d", want, got)
}
}
func TestSubscription_buffer(t *testing.T) {
s := &subscriber{
handler: make(chan *core.Message, 1),
quit: make(chan struct{}),
}
// the buffer size is 1 to simulate what happens
// if the subscriber cannot keep up with processing
// and the buffer fills up. In this case, events
// should be ignored until pending events are
// processed.
e := new(core.Message)
s.publish(e)
s.publish(e)
s.publish(e)
s.publish(e)
s.publish(e)
if got, want := len(s.handler), 1; got != want {
t.Errorf("Want buffered channel size %d, got %d", want, got)
}
}
func TestSubscription_stop(t *testing.T) {
s := &subscriber{
handler: make(chan *core.Message, 1),
quit: make(chan struct{}),
}
if got, want := s.done, false; got != want {
t.Errorf("Want subscription open")
}
s.close()
if got, want := s.done, true; got != want {
t.Errorf("Want subscription closed")
}
// if the subscription is closed we should
// ignore any new events being published.
e := new(core.Message)
s.publish(e)
s.publish(e)
s.publish(e)
s.publish(e)
s.publish(e)
}