8 func TestMessageInsertAndRetreive(t *testing.T) {
10 say := "'Ello, Mister Polly Parrot!"
12 var zero_time time.Time
13 store := start_store()
14 store.Add <- &Message{at, id, say}
15 messages_from_store := make(chan []Message, 1)
16 store.Get <- &StoreRequest{zero_time, messages_from_store}
17 messages := <-messages_from_store
18 if len(messages) != 1 {
21 if messages[0].Time != at {
24 if messages[0].ID != id {
27 if messages[0].Text != say {
34 func TestFetchBlocksUntilSpeak(t *testing.T) {
35 start_fetch_wait_count := fetch_wait_count.String()
37 say := "I've got a lovely fresh cuttle fish for you"
39 var zero_time time.Time
40 store := start_store()
41 messages_from_store := make(chan []Message, 1)
42 store.Get <- &StoreRequest{zero_time, messages_from_store}
43 for start_fetch_wait_count == fetch_wait_count.String() {
46 store.Add <- &Message{at, id, say}
47 messages := <-messages_from_store
48 if len(messages) != 1 {
51 if messages[0].Time != at {
54 if messages[0].ID != id {
57 if messages[0].Text != say {
64 func TestMultipleListeners(t *testing.T) {
66 say := "This is your nine o'clock alarm call!"
68 var zero_time time.Time
69 store := start_store()
70 const num_clients = 13
71 var messages_from_store [num_clients]chan []Message
72 for i := 0; i < num_clients; i++ {
73 messages_from_store[i] = make(chan []Message, 1)
74 store.Get <- &StoreRequest{zero_time, messages_from_store[i]}
76 store.Add <- &Message{at, id, say}
77 for i := 0; i < num_clients; i++ {
78 messages := <-messages_from_store[i]
79 if len(messages) != 1 {
82 if messages[0].Time != at {
85 if messages[0].ID != id {
88 if messages[0].Text != say {
96 func parseDuration(s string) time.Duration {
97 d, err := time.ParseDuration(s)
104 func atoi(s string) int {
105 i, err := strconv.Atoi(s)
112 func TestPartialRetreive(t *testing.T) {
113 start_speak_count := atoi(speak_count.String())
117 say1 := "No, no.....No, 'e's stunned!"
118 say2 := "You stunned him, just as he was wakin' up!"
119 say3 := "Norwegian Blues stun easily, major."
121 at1 := base.Add(parseDuration("-4m"))
122 since := base.Add(parseDuration("-3m"))
123 at2 := base.Add(parseDuration("-2m"))
124 at3 := base.Add(parseDuration("-1m"))
125 store := start_store()
126 store.Add <- &Message{at1, id1, say1}
127 store.Add <- &Message{at2, id2, say2}
128 store.Add <- &Message{at3, id3, say3}
129 for atoi(speak_count.String()) != start_speak_count+3 {
132 messages_from_store := make(chan []Message, 1)
133 store.Get <- &StoreRequest{since, messages_from_store}
134 messages := <-messages_from_store
135 if len(messages) != 2 {
138 if messages[0].Time != at2 {
141 if messages[0].ID != id2 {
144 if messages[0].Text != say2 {
147 if messages[1].Time != at3 {
150 if messages[1].ID != id3 {
153 if messages[1].Text != 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()
174 store.Add <- &Message{at1, id1, say1}
175 store.Add <- &Message{at2, id2, say2}
176 store.Add <- &Message{at3, id3, say3}
177 for atoi(speak_count.String()) != start_speak_count+3 {
180 messages_from_store := make(chan []Message, 1)
181 store.Get <- &StoreRequest{since, messages_from_store}
182 messages := <-messages_from_store
183 if len(messages) != 1 {
186 if messages[0].Time != at3 {
189 if messages[0].ID != id3 {
192 if messages[0].Text != say3 {
199 func TestTypicalFlow(t *testing.T) {
202 say1 := "The Norwegian Blue prefers kippin' on it's back!"
203 say2 := "Remarkable bird, innit, squire? Lovely plumage!"
204 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 if messages1[0].Time != at1 {
225 if messages1[0].ID != id1 {
228 if messages1[0].Text != say1 {
232 // Upon recipt, client blocks on fetch with since=at1
233 prev_fetch_wait_count = fetch_wait_count.String()
234 fetch2 := make(chan []Message, 1)
235 store.Get <- &StoreRequest{at1, fetch2}
236 for prev_fetch_wait_count == fetch_wait_count.String() {
240 // Someone speaks again. This triggers another delivery.
245 store.Add <- &Message{at2, id2, say2}
246 messages2 := <-fetch2
247 if len(messages2) != 1 {
250 if messages2[0].Time != at2 {
253 if messages2[0].ID != id2 {
256 if messages2[0].Text != say2 {