]> git.scottworley.com Git - reliable-chat/blob - server/server_test.go
Test partial fetch
[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 TestMessageInsertAndRetreive(t *testing.T) {
9 say := "'Ello, Mister Polly Parrot!"
10 at := time.Now()
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 {
18 t.FailNow()
19 }
20 if messages[0].Time != at {
21 t.Fail()
22 }
23 if messages[0].Text != say {
24 t.Fail()
25 }
26 close(store.Get)
27 close(store.Add)
28 }
29
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"
33 at := time.Now()
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() {
39 runtime.Gosched()
40 }
41 store.Add <- &Message{at, say}
42 messages := <-messages_from_store
43 if len(messages) != 1 {
44 t.FailNow()
45 }
46 if messages[0].Time != at {
47 t.Fail()
48 }
49 if messages[0].Text != say {
50 t.Fail()
51 }
52 close(store.Get)
53 close(store.Add)
54 }
55
56 func TestMultipleListeners(t *testing.T) {
57 say := "This is your nine o'clock alarm call!"
58 at := time.Now()
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]}
66 }
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 {
71 t.FailNow()
72 }
73 if messages[0].Time != at {
74 t.Fail()
75 }
76 if messages[0].Text != say {
77 t.Fail()
78 }
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 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."
105 base := time.Now()
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 {
115 runtime.Gosched()
116 }
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 {
121 t.FailNow()
122 }
123 if messages[0].Time != at2 {
124 t.Fail()
125 }
126 if messages[0].Text != say2 {
127 t.Fail()
128 }
129 if messages[1].Time != at3 {
130 t.Fail()
131 }
132 if messages[1].Text != say3 {
133 t.Fail()
134 }
135 close(store.Get)
136 close(store.Add)
137 }