]> git.scottworley.com Git - reliable-chat/blame - server/server_test.go
Test fetch blocking
[reliable-chat] / server / server_test.go
CommitLineData
cc9bd370
SW
1package main
2
3import "testing"
ca612dcb 4import "runtime"
cc9bd370
SW
5import "time"
6
7func TestMessageInsertAndRetreive(t *testing.T) {
ca612dcb 8 say := "'Ello, Mister Polly Parrot!"
cc9bd370
SW
9 at := time.Now()
10 var zero_time time.Time
11 store := start_store()
fa5e7c1b 12 store.Add <- &Message{at, say}
cc9bd370 13 messages_from_store := make(chan []Message, 1)
fa5e7c1b 14 store.Get <- &StoreRequest{zero_time, messages_from_store}
cc9bd370
SW
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 }
c282d878
SW
25 close(store.Get)
26 close(store.Add)
cc9bd370 27}
ca612dcb
SW
28
29func 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}