8 func expectMessage(t *testing.T, m *Message, at time.Time, id, say string) {
20 func TestMessageInsertAndRetreive(t *testing.T) {
22 say := "'Ello, Mister Polly Parrot!"
24 var zero_time time.Time
25 store := start_store()
26 store.Add <- &Message{at, id, say}
27 messages_from_store := make(chan []Message, 1)
28 store.Get <- &StoreRequest{zero_time, messages_from_store}
29 messages := <-messages_from_store
30 if len(messages) != 1 {
33 expectMessage(t, &messages[0], at, id, say)
38 func TestFetchBlocksUntilSpeak(t *testing.T) {
39 start_fetch_wait_count := fetch_wait_count.String()
41 say := "I've got a lovely fresh cuttle fish for you"
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() {
50 store.Add <- &Message{at, id, say}
51 messages := <-messages_from_store
52 if len(messages) != 1 {
55 expectMessage(t, &messages[0], at, id, say)
60 func TestMultipleListeners(t *testing.T) {
62 say := "This is your nine o'clock alarm call!"
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]}
72 store.Add <- &Message{at, id, say}
73 for i := 0; i < num_clients; i++ {
74 messages := <-messages_from_store[i]
75 if len(messages) != 1 {
78 expectMessage(t,& messages[0], at, id, 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())
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."
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()
114 store.Add <- &Message{at1, id1, say1}
115 store.Add <- &Message{at2, id2, say2}
116 store.Add <- &Message{at3, id3, say3}
117 for atoi(speak_count.String()) != start_speak_count+3 {
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 {
126 expectMessage(t, &messages[0], at2, id2, say2)
127 expectMessage(t, &messages[1], at3, id3, say3)
132 func TestPrecisePartialRetreive(t *testing.T) {
133 start_speak_count := atoi(speak_count.String())
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?"
141 at1 := base.Add(parseDuration("-3m"))
142 at2 := base.Add(parseDuration("-2m"))
143 at3 := base.Add(parseDuration("-1m"))
145 store := start_store()
146 store.Add <- &Message{at1, id1, say1}
147 store.Add <- &Message{at2, id2, say2}
148 store.Add <- &Message{at3, id3, say3}
149 for atoi(speak_count.String()) != start_speak_count+3 {
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 {
158 expectMessage(t, &messages[0], at3, id3, say3)
163 func TestTypicalFlow(t *testing.T) {
166 say1 := "The Norwegian Blue prefers kippin' on it's back!"
167 say2 := "Remarkable bird, innit, squire? Lovely plumage!"
168 store := start_store()
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() {
179 // Someone speaks. This triggers delivery.
181 store.Add <- &Message{at1, id1, say1}
182 messages1 := <-fetch1
183 if len(messages1) != 1 {
186 expectMessage(t, &messages1[0], at1, id1, say1)
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() {
196 // Someone speaks again. This triggers another delivery.
201 store.Add <- &Message{at2, id2, say2}
202 messages2 := <-fetch2
203 if len(messages2) != 1 {
206 expectMessage(t, &messages2[0], at2, id2, say2)