1 /* reliable-chat - multipath chat
2 * Copyright (C) 2012 Scott Worley <sworley@chkno.net>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 func expectMessage(t *testing.T, m *Message, at time.Time, id, say string) {
37 func TestMessageInsertAndRetreive(t *testing.T) {
39 say := "'Ello, Mister Polly Parrot!"
41 var zero_time time.Time
42 store := start_store()
48 store.Add <- &Message{at, id, say}
49 messages_from_store := make(chan []Message, 1)
50 store.Get <- &StoreRequest{zero_time, messages_from_store}
51 messages := <-messages_from_store
52 if len(messages) != 1 {
55 expectMessage(t, &messages[0], at, id, say)
58 func TestFetchBlocksUntilSpeak(t *testing.T) {
59 start_fetch_wait_count := fetch_wait_count.String()
61 say := "I've got a lovely fresh cuttle fish for you"
63 var zero_time time.Time
64 store := start_store()
70 messages_from_store := make(chan []Message, 1)
71 store.Get <- &StoreRequest{zero_time, messages_from_store}
72 for start_fetch_wait_count == fetch_wait_count.String() {
75 store.Add <- &Message{at, id, say}
76 messages := <-messages_from_store
77 if len(messages) != 1 {
80 expectMessage(t, &messages[0], at, id, say)
83 func TestMultipleListeners(t *testing.T) {
85 say := "This is your nine o'clock alarm call!"
87 var zero_time time.Time
88 store := start_store()
94 const num_clients = 13
95 var messages_from_store [num_clients]chan []Message
96 for i := 0; i < num_clients; i++ {
97 messages_from_store[i] = make(chan []Message, 1)
98 store.Get <- &StoreRequest{zero_time, messages_from_store[i]}
100 store.Add <- &Message{at, id, say}
101 for i := 0; i < num_clients; i++ {
102 messages := <-messages_from_store[i]
103 if len(messages) != 1 {
106 expectMessage(t, &messages[0], at, id, say)
110 func parseDuration(s string) time.Duration {
111 d, err := time.ParseDuration(s)
118 func atoi(s string) int {
119 i, err := strconv.Atoi(s)
126 func TestPartialRetreive(t *testing.T) {
127 start_speak_count := atoi(speak_count.String())
131 say1 := "No, no.....No, 'e's stunned!"
132 say2 := "You stunned him, just as he was wakin' up!"
133 say3 := "Norwegian Blues stun easily, major."
135 at1 := base.Add(parseDuration("-4m"))
136 since := base.Add(parseDuration("-3m"))
137 at2 := base.Add(parseDuration("-2m"))
138 at3 := base.Add(parseDuration("-1m"))
139 store := start_store()
145 store.Add <- &Message{at1, id1, say1}
146 store.Add <- &Message{at2, id2, say2}
147 store.Add <- &Message{at3, id3, say3}
148 for atoi(speak_count.String()) != start_speak_count+3 {
151 messages_from_store := make(chan []Message, 1)
152 store.Get <- &StoreRequest{since, messages_from_store}
153 messages := <-messages_from_store
154 if len(messages) != 2 {
157 expectMessage(t, &messages[0], at2, id2, say2)
158 expectMessage(t, &messages[1], at3, id3, say3)
161 func TestPrecisePartialRetreive(t *testing.T) {
162 start_speak_count := atoi(speak_count.String())
166 say1 := "Well, he's...he's, ah...probably pining for the fjords."
167 say2 := "PININ' for the FJORDS?!?!?!?"
168 say3 := "look, why did he fall flat on his back the moment I got 'im home?"
170 at1 := base.Add(parseDuration("-3m"))
171 at2 := base.Add(parseDuration("-2m"))
172 at3 := base.Add(parseDuration("-1m"))
174 store := start_store()
180 store.Add <- &Message{at1, id1, say1}
181 store.Add <- &Message{at2, id2, say2}
182 store.Add <- &Message{at3, id3, say3}
183 for atoi(speak_count.String()) != start_speak_count+3 {
186 messages_from_store := make(chan []Message, 1)
187 store.Get <- &StoreRequest{since, messages_from_store}
188 messages := <-messages_from_store
189 if len(messages) != 1 {
192 expectMessage(t, &messages[0], at3, id3, say3)
195 func TestTypicalFlow(t *testing.T) {
198 say1 := "The Norwegian Blue prefers kippin' on it's back!"
199 say2 := "Remarkable bird, innit, squire? Lovely plumage!"
200 store := start_store()
206 // A waiting zero-time fetch.
207 var zero_time time.Time
208 prev_fetch_wait_count := fetch_wait_count.String()
209 fetch1 := make(chan []Message, 1)
210 store.Get <- &StoreRequest{zero_time, fetch1}
211 for prev_fetch_wait_count == fetch_wait_count.String() {
215 // Someone speaks. This triggers delivery.
217 store.Add <- &Message{at1, id1, say1}
218 messages1 := <-fetch1
219 if len(messages1) != 1 {
222 expectMessage(t, &messages1[0], at1, id1, say1)
224 // Upon recipt, client blocks on fetch with since=at1
225 prev_fetch_wait_count = fetch_wait_count.String()
226 fetch2 := make(chan []Message, 1)
227 store.Get <- &StoreRequest{at1, fetch2}
228 for prev_fetch_wait_count == fetch_wait_count.String() {
232 // Someone speaks again. This triggers another delivery.
237 store.Add <- &Message{at2, id2, say2}
238 messages2 := <-fetch2
239 if len(messages2) != 1 {
242 expectMessage(t, &messages2[0], at2, id2, say2)
245 func TestExpiryDueToLimit(t *testing.T) {
246 previous_limit := *max_messages
247 defer func() { *max_messages = previous_limit }()
250 start_speak_count := atoi(speak_count.String())
251 start_drop_count := atoi(drop_due_to_limit_count.String())
255 say1 := "'E's passed on!"
256 say2 := "This parrot is no more!"
257 say3 := "He has ceased to be!"
259 at1 := base.Add(parseDuration("-3m"))
260 at2 := base.Add(parseDuration("-2m"))
261 at3 := base.Add(parseDuration("-1m"))
262 store := start_store()
268 store.Add <- &Message{at1, id1, say1}
269 store.Add <- &Message{at2, id2, say2}
270 store.Add <- &Message{at3, id3, say3}
271 for atoi(speak_count.String()) != start_speak_count+3 {
274 if atoi(drop_due_to_limit_count.String()) != start_drop_count+1 {
277 messages_from_store := make(chan []Message, 1)
278 var zero_time time.Time
279 store.Get <- &StoreRequest{zero_time, messages_from_store}
280 messages := <-messages_from_store
281 if len(messages) != 2 {
284 expectMessage(t, &messages[0], at2, id2, say2)
285 expectMessage(t, &messages[1], at3, id3, say3)