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, version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 func expectMessage(t *testing.T, m *Message, at time.Time, id, say string) {
36 func TestMessageInsertAndRetreive(t *testing.T) {
38 say := "'Ello, Mister Polly Parrot!"
40 var zero_time time.Time
41 store := start_store()
47 store.Add <- &Message{at, id, say}
48 messages_from_store := make(chan []Message, 1)
49 store.Get <- &StoreRequest{zero_time, messages_from_store}
50 messages := <-messages_from_store
51 if len(messages) != 1 {
54 expectMessage(t, &messages[0], at, id, say)
57 func TestFetchBlocksUntilSpeak(t *testing.T) {
58 start_fetch_wait_count := fetch_wait_count.String()
60 say := "I've got a lovely fresh cuttle fish for you"
62 var zero_time time.Time
63 store := start_store()
69 messages_from_store := make(chan []Message, 1)
70 store.Get <- &StoreRequest{zero_time, messages_from_store}
71 for start_fetch_wait_count == fetch_wait_count.String() {
74 store.Add <- &Message{at, id, say}
75 messages := <-messages_from_store
76 if len(messages) != 1 {
79 expectMessage(t, &messages[0], at, id, say)
82 func TestMultipleListeners(t *testing.T) {
84 say := "This is your nine o'clock alarm call!"
86 var zero_time time.Time
87 store := start_store()
93 const num_clients = 13
94 var messages_from_store [num_clients]chan []Message
95 for i := 0; i < num_clients; i++ {
96 messages_from_store[i] = make(chan []Message, 1)
97 store.Get <- &StoreRequest{zero_time, messages_from_store[i]}
99 store.Add <- &Message{at, id, say}
100 for i := 0; i < num_clients; i++ {
101 messages := <-messages_from_store[i]
102 if len(messages) != 1 {
105 expectMessage(t, &messages[0], at, id, say)
109 func parseDuration(s string) time.Duration {
110 d, err := time.ParseDuration(s)
117 func atoi(s string) int {
118 i, err := strconv.Atoi(s)
125 func TestPartialRetreive(t *testing.T) {
126 start_speak_count := atoi(speak_count.String())
130 say1 := "No, no.....No, 'e's stunned!"
131 say2 := "You stunned him, just as he was wakin' up!"
132 say3 := "Norwegian Blues stun easily, major."
134 at1 := base.Add(parseDuration("-4m"))
135 since := base.Add(parseDuration("-3m"))
136 at2 := base.Add(parseDuration("-2m"))
137 at3 := base.Add(parseDuration("-1m"))
138 store := start_store()
144 store.Add <- &Message{at1, id1, say1}
145 store.Add <- &Message{at2, id2, say2}
146 store.Add <- &Message{at3, id3, say3}
147 for atoi(speak_count.String()) != start_speak_count+3 {
150 messages_from_store := make(chan []Message, 1)
151 store.Get <- &StoreRequest{since, messages_from_store}
152 messages := <-messages_from_store
153 if len(messages) != 2 {
156 expectMessage(t, &messages[0], at2, id2, say2)
157 expectMessage(t, &messages[1], at3, id3, say3)
160 func TestPrecisePartialRetreive(t *testing.T) {
161 start_speak_count := atoi(speak_count.String())
165 say1 := "Well, he's...he's, ah...probably pining for the fjords."
166 say2 := "PININ' for the FJORDS?!?!?!?"
167 say3 := "look, why did he fall flat on his back the moment I got 'im home?"
169 at1 := base.Add(parseDuration("-3m"))
170 at2 := base.Add(parseDuration("-2m"))
171 at3 := base.Add(parseDuration("-1m"))
173 store := start_store()
179 store.Add <- &Message{at1, id1, say1}
180 store.Add <- &Message{at2, id2, say2}
181 store.Add <- &Message{at3, id3, say3}
182 for atoi(speak_count.String()) != start_speak_count+3 {
185 messages_from_store := make(chan []Message, 1)
186 store.Get <- &StoreRequest{since, messages_from_store}
187 messages := <-messages_from_store
188 if len(messages) != 1 {
191 expectMessage(t, &messages[0], at3, id3, say3)
194 func TestTypicalFlow(t *testing.T) {
197 say1 := "The Norwegian Blue prefers kippin' on it's back!"
198 say2 := "Remarkable bird, innit, squire? Lovely plumage!"
199 store := start_store()
205 // A waiting zero-time fetch.
206 var zero_time time.Time
207 prev_fetch_wait_count := fetch_wait_count.String()
208 fetch1 := make(chan []Message, 1)
209 store.Get <- &StoreRequest{zero_time, fetch1}
210 for prev_fetch_wait_count == fetch_wait_count.String() {
214 // Someone speaks. This triggers delivery.
216 store.Add <- &Message{at1, id1, say1}
217 messages1 := <-fetch1
218 if len(messages1) != 1 {
221 expectMessage(t, &messages1[0], at1, id1, say1)
223 // Upon recipt, client blocks on fetch with since=at1
224 prev_fetch_wait_count = fetch_wait_count.String()
225 fetch2 := make(chan []Message, 1)
226 store.Get <- &StoreRequest{at1, fetch2}
227 for prev_fetch_wait_count == fetch_wait_count.String() {
231 // Someone speaks again. This triggers another delivery.
236 store.Add <- &Message{at2, id2, say2}
237 messages2 := <-fetch2
238 if len(messages2) != 1 {
241 expectMessage(t, &messages2[0], at2, id2, say2)
244 func TestExpiryDueToLimit(t *testing.T) {
245 previous_limit := *max_messages
246 defer func() { *max_messages = previous_limit }()
249 start_speak_count := atoi(speak_count.String())
250 start_drop_count := atoi(drop_due_to_limit_count.String())
254 say1 := "'E's passed on!"
255 say2 := "This parrot is no more!"
256 say3 := "He has ceased to be!"
258 at1 := base.Add(parseDuration("-3m"))
259 at2 := base.Add(parseDuration("-2m"))
260 at3 := base.Add(parseDuration("-1m"))
261 store := start_store()
267 store.Add <- &Message{at1, id1, say1}
268 store.Add <- &Message{at2, id2, say2}
269 store.Add <- &Message{at3, id3, say3}
270 for atoi(speak_count.String()) != start_speak_count+3 {
273 if atoi(drop_due_to_limit_count.String()) != start_drop_count+1 {
276 messages_from_store := make(chan []Message, 1)
277 var zero_time time.Time
278 store.Get <- &StoreRequest{zero_time, messages_from_store}
279 messages := <-messages_from_store
280 if len(messages) != 2 {
283 expectMessage(t, &messages[0], at2, id2, say2)
284 expectMessage(t, &messages[1], at3, id3, say3)