8 func TestMessageInsertAndRetreive(t *testing.T) {
9 say := "'Ello, Mister Polly Parrot!"
11 var zero_time time.Time
12 store := start_store()
13 store.Add <- &Message{at, say}
14 messages_from_store := make(chan []Message, 1)
15 store.Get <- &StoreRequest{zero_time, messages_from_store}
16 messages := <-messages_from_store
17 if len(messages) != 1 {
20 if messages[0].Time != at {
23 if messages[0].Text != say {
30 func TestFetchBlocksUntilSpeak(t *testing.T) {
31 start_fetch_wait_count := fetch_wait_count.String()
32 say := "I've got a lovely fresh cuttle fish for you"
34 var zero_time time.Time
35 store := start_store()
36 messages_from_store := make(chan []Message, 1)
37 store.Get <- &StoreRequest{zero_time, messages_from_store}
38 for start_fetch_wait_count == fetch_wait_count.String() {
41 store.Add <- &Message{at, say}
42 messages := <-messages_from_store
43 if len(messages) != 1 {
46 if messages[0].Time != at {
49 if messages[0].Text != say {
56 func TestMultipleListeners(t *testing.T) {
57 say := "This is your nine o'clock alarm call!"
59 var zero_time time.Time
60 store := start_store()
61 const num_clients = 13
62 var messages_from_store [num_clients]chan []Message
63 for i := 0; i < num_clients; i++ {
64 messages_from_store[i] = make(chan []Message, 1)
65 store.Get <- &StoreRequest{zero_time, messages_from_store[i]}
67 store.Add <- &Message{at, say}
68 for i := 0; i < num_clients; i++ {
69 messages := <-messages_from_store[i]
70 if len(messages) != 1 {
73 if messages[0].Time != at {
76 if messages[0].Text != say {
84 func parseDuration(s string) time.Duration {
85 d, err := time.ParseDuration(s)
92 func atoi(s string) int {
93 i, err := strconv.Atoi(s)
100 func TestPartialRetreive(t *testing.T) {
101 start_speak_count := atoi(speak_count.String())
102 say1 := "No, no.....No, 'e's stunned!"
103 say2 := "You stunned him, just as he was wakin' up!"
104 say3 := "Norwegian Blues stun easily, major."
106 at1 := base.Add(parseDuration("-4m"))
107 since := base.Add(parseDuration("-3m"))
108 at2 := base.Add(parseDuration("-2m"))
109 at3 := base.Add(parseDuration("-1m"))
110 store := start_store()
111 store.Add <- &Message{at1, say1}
112 store.Add <- &Message{at2, say2}
113 store.Add <- &Message{at3, say3}
114 for atoi(speak_count.String()) != start_speak_count+3 {
117 messages_from_store := make(chan []Message, 1)
118 store.Get <- &StoreRequest{since, messages_from_store}
119 messages := <-messages_from_store
120 if len(messages) != 2 {
123 if messages[0].Time != at2 {
126 if messages[0].Text != say2 {
129 if messages[1].Time != at3 {
132 if messages[1].Text != say3 {
139 func TestPrecisePartialRetreive(t *testing.T) {
140 start_speak_count := atoi(speak_count.String())
141 say1 := "Well, he's...he's, ah...probably pining for the fjords."
142 say2 := "PININ' for the FJORDS?!?!?!?"
143 say3 := "look, why did he fall flat on his back the moment I got 'im home?"
145 at1 := base.Add(parseDuration("-3m"))
146 at2 := base.Add(parseDuration("-2m"))
147 at3 := base.Add(parseDuration("-1m"))
149 store := start_store()
150 store.Add <- &Message{at1, say1}
151 store.Add <- &Message{at2, say2}
152 store.Add <- &Message{at3, say3}
153 for atoi(speak_count.String()) != start_speak_count+3 {
156 messages_from_store := make(chan []Message, 1)
157 store.Get <- &StoreRequest{since, messages_from_store}
158 messages := <-messages_from_store
159 if len(messages) != 1 {
162 if messages[0].Time != at3 {
165 if messages[0].Text != say3 {
172 func TestTypicalFlow(t *testing.T) {
173 say1 := "The Norwegian Blue prefers kippin' on it's back!"
174 say2 := "Remarkable bird, innit, squire? Lovely plumage!"
175 store := start_store()
177 // A waiting zero-time fetch.
178 var zero_time time.Time
179 prev_fetch_wait_count := fetch_wait_count.String()
180 fetch1 := make(chan []Message, 1)
181 store.Get <- &StoreRequest{zero_time, fetch1}
182 for prev_fetch_wait_count == fetch_wait_count.String() {
186 // Someone speaks. This triggers delivery.
188 store.Add <- &Message{at1, say1}
189 messages1 := <-fetch1
190 if len(messages1) != 1 {
193 if messages1[0].Time != at1 {
196 if messages1[0].Text != say1 {
200 // Upon recipt, client blocks on fetch with since=at1
201 prev_fetch_wait_count = fetch_wait_count.String()
202 fetch2 := make(chan []Message, 1)
203 store.Get <- &StoreRequest{at1, fetch2}
204 for prev_fetch_wait_count == fetch_wait_count.String() {
208 // Someone speaks again. This triggers another delivery.
213 store.Add <- &Message{at2, say2}
214 messages2 := <-fetch2
215 if len(messages2) != 1 {
218 if messages2[0].Time != at2 {
221 if messages2[0].Text != say2 {