]> git.scottworley.com Git - reliable-chat/blame - server/server_test.go
Merge skitch's changes.
[reliable-chat] / server / server_test.go
CommitLineData
cc9bd370
SW
1package main
2
3import "testing"
ca612dcb 4import "runtime"
d69a32e6 5import "strconv"
cc9bd370
SW
6import "time"
7
1c425518
SW
8func expectMessage(t *testing.T, m *Message, at time.Time, id, say string) {
9 if m.Time != at {
10 t.Fail()
11 }
12 if m.ID != id {
13 t.Fail()
14 }
15 if m.Text != say {
16 t.Fail()
17 }
18}
19
cc9bd370 20func TestMessageInsertAndRetreive(t *testing.T) {
b199796a 21 id := "1"
ca612dcb 22 say := "'Ello, Mister Polly Parrot!"
cc9bd370
SW
23 at := time.Now()
24 var zero_time time.Time
25 store := start_store()
b199796a 26 store.Add <- &Message{at, id, say}
cc9bd370 27 messages_from_store := make(chan []Message, 1)
fa5e7c1b 28 store.Get <- &StoreRequest{zero_time, messages_from_store}
cc9bd370
SW
29 messages := <-messages_from_store
30 if len(messages) != 1 {
d69a32e6 31 t.FailNow()
cc9bd370 32 }
1c425518 33 expectMessage(t, &messages[0], at, id, say)
c282d878
SW
34 close(store.Get)
35 close(store.Add)
cc9bd370 36}
ca612dcb
SW
37
38func TestFetchBlocksUntilSpeak(t *testing.T) {
39 start_fetch_wait_count := fetch_wait_count.String()
b199796a 40 id := "2"
ca612dcb
SW
41 say := "I've got a lovely fresh cuttle fish for you"
42 at := time.Now()
43 var zero_time time.Time
44 store := start_store()
45 messages_from_store := make(chan []Message, 1)
46 store.Get <- &StoreRequest{zero_time, messages_from_store}
47 for start_fetch_wait_count == fetch_wait_count.String() {
48 runtime.Gosched()
49 }
b199796a 50 store.Add <- &Message{at, id, say}
ca612dcb
SW
51 messages := <-messages_from_store
52 if len(messages) != 1 {
d69a32e6 53 t.FailNow()
ca612dcb 54 }
1c425518 55 expectMessage(t, &messages[0], at, id, say)
ca612dcb
SW
56 close(store.Get)
57 close(store.Add)
58}
d4039f63
SW
59
60func TestMultipleListeners(t *testing.T) {
b199796a 61 id := "3"
d4039f63
SW
62 say := "This is your nine o'clock alarm call!"
63 at := time.Now()
64 var zero_time time.Time
65 store := start_store()
66 const num_clients = 13
67 var messages_from_store [num_clients]chan []Message
68 for i := 0; i < num_clients; i++ {
69 messages_from_store[i] = make(chan []Message, 1)
70 store.Get <- &StoreRequest{zero_time, messages_from_store[i]}
71 }
b199796a 72 store.Add <- &Message{at, id, say}
d4039f63
SW
73 for i := 0; i < num_clients; i++ {
74 messages := <-messages_from_store[i]
75 if len(messages) != 1 {
d69a32e6 76 t.FailNow()
d4039f63 77 }
1c425518 78 expectMessage(t,& messages[0], at, id, say)
d4039f63
SW
79 }
80 close(store.Get)
81 close(store.Add)
82}
d69a32e6
SW
83
84func parseDuration(s string) time.Duration {
85 d, err := time.ParseDuration(s)
86 if err != nil {
87 panic(err)
88 }
89 return d
90}
91
92func atoi(s string) int {
93 i, err := strconv.Atoi(s)
94 if err != nil {
95 panic(err)
96 }
97 return i
98}
99
100func TestPartialRetreive(t *testing.T) {
101 start_speak_count := atoi(speak_count.String())
b199796a
SW
102 id1 := "4"
103 id2 := "5"
104 id3 := "6"
d69a32e6
SW
105 say1 := "No, no.....No, 'e's stunned!"
106 say2 := "You stunned him, just as he was wakin' up!"
107 say3 := "Norwegian Blues stun easily, major."
108 base := time.Now()
109 at1 := base.Add(parseDuration("-4m"))
110 since := base.Add(parseDuration("-3m"))
111 at2 := base.Add(parseDuration("-2m"))
112 at3 := base.Add(parseDuration("-1m"))
113 store := start_store()
b199796a
SW
114 store.Add <- &Message{at1, id1, say1}
115 store.Add <- &Message{at2, id2, say2}
116 store.Add <- &Message{at3, id3, say3}
d69a32e6
SW
117 for atoi(speak_count.String()) != start_speak_count+3 {
118 runtime.Gosched()
119 }
120 messages_from_store := make(chan []Message, 1)
121 store.Get <- &StoreRequest{since, messages_from_store}
122 messages := <-messages_from_store
123 if len(messages) != 2 {
124 t.FailNow()
125 }
1c425518
SW
126 expectMessage(t, &messages[0], at2, id2, say2)
127 expectMessage(t, &messages[1], at3, id3, say3)
d69a32e6
SW
128 close(store.Get)
129 close(store.Add)
130}
6e7f760f
SW
131
132func TestPrecisePartialRetreive(t *testing.T) {
133 start_speak_count := atoi(speak_count.String())
b199796a
SW
134 id1 := "7"
135 id2 := "8"
136 id3 := "9"
6e7f760f
SW
137 say1 := "Well, he's...he's, ah...probably pining for the fjords."
138 say2 := "PININ' for the FJORDS?!?!?!?"
139 say3 := "look, why did he fall flat on his back the moment I got 'im home?"
140 base := time.Now()
141 at1 := base.Add(parseDuration("-3m"))
142 at2 := base.Add(parseDuration("-2m"))
143 at3 := base.Add(parseDuration("-1m"))
144 since := at2
145 store := start_store()
b199796a
SW
146 store.Add <- &Message{at1, id1, say1}
147 store.Add <- &Message{at2, id2, say2}
148 store.Add <- &Message{at3, id3, say3}
6e7f760f
SW
149 for atoi(speak_count.String()) != start_speak_count+3 {
150 runtime.Gosched()
151 }
152 messages_from_store := make(chan []Message, 1)
153 store.Get <- &StoreRequest{since, messages_from_store}
154 messages := <-messages_from_store
155 if len(messages) != 1 {
156 t.FailNow()
157 }
1c425518 158 expectMessage(t, &messages[0], at3, id3, say3)
6e7f760f
SW
159 close(store.Get)
160 close(store.Add)
161}
162
163func TestTypicalFlow(t *testing.T) {
b199796a
SW
164 id1 := "10"
165 id2 := "11"
6e7f760f
SW
166 say1 := "The Norwegian Blue prefers kippin' on it's back!"
167 say2 := "Remarkable bird, innit, squire? Lovely plumage!"
168 store := start_store()
169
170 // A waiting zero-time fetch.
171 var zero_time time.Time
172 prev_fetch_wait_count := fetch_wait_count.String()
173 fetch1 := make(chan []Message, 1)
174 store.Get <- &StoreRequest{zero_time, fetch1}
175 for prev_fetch_wait_count == fetch_wait_count.String() {
176 runtime.Gosched()
177 }
178
179 // Someone speaks. This triggers delivery.
180 at1 := time.Now()
b199796a 181 store.Add <- &Message{at1, id1, say1}
6e7f760f
SW
182 messages1 := <-fetch1
183 if len(messages1) != 1 {
184 t.FailNow()
185 }
1c425518 186 expectMessage(t, &messages1[0], at1, id1, say1)
6e7f760f
SW
187
188 // Upon recipt, client blocks on fetch with since=at1
189 prev_fetch_wait_count = fetch_wait_count.String()
190 fetch2 := make(chan []Message, 1)
191 store.Get <- &StoreRequest{at1, fetch2}
192 for prev_fetch_wait_count == fetch_wait_count.String() {
193 runtime.Gosched()
194 }
195
196 // Someone speaks again. This triggers another delivery.
197 at2 := time.Now()
198 if !at2.After(at1) {
199 t.Fail()
200 }
b199796a 201 store.Add <- &Message{at2, id2, say2}
6e7f760f
SW
202 messages2 := <-fetch2
203 if len(messages2) != 1 {
204 t.FailNow()
205 }
1c425518 206 expectMessage(t, &messages2[0], at2, id2, say2)
6e7f760f
SW
207
208 close(store.Get)
209 close(store.Add)
210}