]> git.scottworley.com Git - reliable-chat/blob - server/server_test.go
fd4a4c87a108a964b509ad095036c20cfd1346fc
[reliable-chat] / server / server_test.go
1 package main
2
3 import "testing"
4 import "runtime"
5 import "time"
6
7 func TestMessageInsertAndRetreive(t *testing.T) {
8 say := "'Ello, Mister Polly Parrot!"
9 at := time.Now()
10 var zero_time time.Time
11 store := start_store()
12 store.Add <- &Message{at, say}
13 messages_from_store := make(chan []Message, 1)
14 store.Get <- &StoreRequest{zero_time, messages_from_store}
15 messages := <-messages_from_store
16 if len(messages) != 1 {
17 t.Fail()
18 }
19 if messages[0].Time != at {
20 t.Fail()
21 }
22 if messages[0].Text != say {
23 t.Fail()
24 }
25 close(store.Get)
26 close(store.Add)
27 }
28
29 func TestFetchBlocksUntilSpeak(t *testing.T) {
30 start_fetch_wait_count := fetch_wait_count.String()
31 say := "I've got a lovely fresh cuttle fish for you"
32 at := time.Now()
33 var zero_time time.Time
34 store := start_store()
35 messages_from_store := make(chan []Message, 1)
36 store.Get <- &StoreRequest{zero_time, messages_from_store}
37 for start_fetch_wait_count == fetch_wait_count.String() {
38 runtime.Gosched()
39 }
40 store.Add <- &Message{at, say}
41 messages := <-messages_from_store
42 if len(messages) != 1 {
43 t.Fail()
44 }
45 if messages[0].Time != at {
46 t.Fail()
47 }
48 if messages[0].Text != say {
49 t.Fail()
50 }
51 close(store.Get)
52 close(store.Add)
53 }
54
55 func TestMultipleListeners(t *testing.T) {
56 say := "This is your nine o'clock alarm call!"
57 at := time.Now()
58 var zero_time time.Time
59 store := start_store()
60 const num_clients = 13
61 var messages_from_store [num_clients]chan []Message
62 for i := 0; i < num_clients; i++ {
63 messages_from_store[i] = make(chan []Message, 1)
64 store.Get <- &StoreRequest{zero_time, messages_from_store[i]}
65 }
66 store.Add <- &Message{at, say}
67 for i := 0; i < num_clients; i++ {
68 messages := <-messages_from_store[i]
69 if len(messages) != 1 {
70 t.Fail()
71 }
72 if messages[0].Time != at {
73 t.Fail()
74 }
75 if messages[0].Text != say {
76 t.Fail()
77 }
78 }
79 close(store.Get)
80 close(store.Add)
81 }