]> git.scottworley.com Git - reliable-chat/blob - server/server_test.go
fb20ddc4070459b5bf9668a8b1ae2ac164c5af42
[reliable-chat] / server / server_test.go
1 package main
2
3 import "testing"
4 import "runtime"
5 import "strconv"
6 import "time"
7
8 func expectMessage(t *testing.T, m *Message, at time.Time, id, say string) {
9 if m.Time != at {
10 t.Fail()
11 }
12 if m.ID != id {
13 t.Fail()
14 }
15 if m.Text != say {
16 t.Fail()
17 }
18 }
19
20 func TestMessageInsertAndRetreive(t *testing.T) {
21 id := "1"
22 say := "'Ello, Mister Polly Parrot!"
23 at := time.Now()
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 {
31 t.FailNow()
32 }
33 expectMessage(t, &messages[0], at, id, say)
34 close(store.Get)
35 close(store.Add)
36 }
37
38 func TestFetchBlocksUntilSpeak(t *testing.T) {
39 start_fetch_wait_count := fetch_wait_count.String()
40 id := "2"
41 say := "I've got a lovely fresh cuttle fish for you"
42 at := time.Now()
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() {
48 runtime.Gosched()
49 }
50 store.Add <- &Message{at, id, say}
51 messages := <-messages_from_store
52 if len(messages) != 1 {
53 t.FailNow()
54 }
55 expectMessage(t, &messages[0], at, id, say)
56 close(store.Get)
57 close(store.Add)
58 }
59
60 func TestMultipleListeners(t *testing.T) {
61 id := "3"
62 say := "This is your nine o'clock alarm call!"
63 at := time.Now()
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]}
71 }
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 {
76 t.FailNow()
77 }
78 expectMessage(t,& messages[0], at, id, say)
79 }
80 close(store.Get)
81 close(store.Add)
82 }
83
84 func parseDuration(s string) time.Duration {
85 d, err := time.ParseDuration(s)
86 if err != nil {
87 panic(err)
88 }
89 return d
90 }
91
92 func atoi(s string) int {
93 i, err := strconv.Atoi(s)
94 if err != nil {
95 panic(err)
96 }
97 return i
98 }
99
100 func TestPartialRetreive(t *testing.T) {
101 start_speak_count := atoi(speak_count.String())
102 id1 := "4"
103 id2 := "5"
104 id3 := "6"
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."
108 base := time.Now()
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 {
118 runtime.Gosched()
119 }
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 {
124 t.FailNow()
125 }
126 expectMessage(t, &messages[0], at2, id2, say2)
127 expectMessage(t, &messages[1], at3, id3, say3)
128 close(store.Get)
129 close(store.Add)
130 }
131
132 func TestPrecisePartialRetreive(t *testing.T) {
133 start_speak_count := atoi(speak_count.String())
134 id1 := "7"
135 id2 := "8"
136 id3 := "9"
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?"
140 base := time.Now()
141 at1 := base.Add(parseDuration("-3m"))
142 at2 := base.Add(parseDuration("-2m"))
143 at3 := base.Add(parseDuration("-1m"))
144 since := at2
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 {
150 runtime.Gosched()
151 }
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 {
156 t.FailNow()
157 }
158 expectMessage(t, &messages[0], at3, id3, say3)
159 close(store.Get)
160 close(store.Add)
161 }
162
163 func TestTypicalFlow(t *testing.T) {
164 id1 := "10"
165 id2 := "11"
166 say1 := "The Norwegian Blue prefers kippin' on it's back!"
167 say2 := "Remarkable bird, innit, squire? Lovely plumage!"
168 store := start_store()
169
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() {
176 runtime.Gosched()
177 }
178
179 // Someone speaks. This triggers delivery.
180 at1 := time.Now()
181 store.Add <- &Message{at1, id1, say1}
182 messages1 := <-fetch1
183 if len(messages1) != 1 {
184 t.FailNow()
185 }
186 expectMessage(t, &messages1[0], at1, id1, say1)
187
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() {
193 runtime.Gosched()
194 }
195
196 // Someone speaks again. This triggers another delivery.
197 at2 := time.Now()
198 if !at2.After(at1) {
199 t.Fail()
200 }
201 store.Add <- &Message{at2, id2, say2}
202 messages2 := <-fetch2
203 if len(messages2) != 1 {
204 t.FailNow()
205 }
206 expectMessage(t, &messages2[0], at2, id2, say2)
207
208 close(store.Get)
209 close(store.Add)
210 }