-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubject_test.go
126 lines (96 loc) · 2.69 KB
/
subject_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package reactive
import (
"reflect"
"testing"
"time"
)
func TestNewSubject(t *testing.T) {
subject := NewSubject().(*subject)
if subject == nil {
t.Error("NewSubject returned nil")
}
if subject.Subscriptions == nil {
t.Error("NewSubject did not create empty subscriptions map")
}
if len(subject.Subscriptions) != 0 {
t.Error("NewSubject did not create empty subscriptions map")
}
}
func TestSubject_Close(t *testing.T) {
sub := NewSubject()
sub.Subscribe(func() {})
sub.Close()
casted := sub.(*subject)
if !reflect.DeepEqual(casted.Subscriptions, make(map[Subscription]interface{})) {
t.Error("Subscriptions does not equal empty list")
}
}
func TestSubject_Subscribe(t *testing.T) {
subject := NewSubject().(*subject)
subscription, _ := subject.Subscribe(func() {})
if _, exists := subject.Subscriptions[subscription]; !exists {
t.Error("Subscription is not in subscription map")
}
if sub, err := subject.Subscribe(3); err == nil || sub != EmptySubscription() {
t.Error("Subscribe accepted non-function parameters")
}
}
func TestSubject_Next(t *testing.T) {
subject := NewSubject().(*subject)
didRun := false
subject.Subscribe(func(run bool) {
didRun = run
})
subject.Next(true)
if !didRun {
t.Error("Subscription handler wasnt called!")
}
}
func TestSubject_Pipe(t *testing.T) {
subject := NewSubject().(*subject)
if subject != subject.Pipe() {
t.Error("Empty pipe is different from original")
}
if subject == subject.Pipe(Take(1)) {
t.Error("Take pipe resulted in original subject")
}
if subject != subject.Pipe(nil) {
t.Error("Nil pipe is different from original")
}
}
func TestSubject_Unsubscribe(t *testing.T) {
subject := NewSubject().(*subject)
subscription, _ := subject.Subscribe(func() {})
if _, exists := subject.Subscriptions[subscription]; !exists {
t.Error("Subscription is not in subscription map")
}
subject.Unsubscribe(subscription)
if _, exists := subject.Subscriptions[subscription]; exists {
t.Error("Subscription is still in subscription map")
}
}
func TestSubject_UnsubscribeUnknown(t *testing.T) {
subject := NewSubject().(*subject)
subscription := NewSubscription()
if subject.Unsubscribe(subscription) == nil {
t.Error("Unsubscription did not recognize invalid subscription")
}
}
func TestSubject_AsChannel(t *testing.T) {
subject := NewSubject().(*subject)
channel := make(chan interface{})
go func() {
data := <-subject.AsChannel()
channel <- data
}()
// Wait for handler to register
<-time.After(time.Duration(400) * time.Millisecond)
subject.Next(true)
select {
case <-channel:
return
case <-time.After(time.Duration(500) * time.Millisecond):
t.Error("Subscription handler wasnt called!")
break
}
}