From: Scott Worley Date: Sat, 28 Jul 2012 19:38:34 +0000 (-0700) Subject: Test partial fetch X-Git-Url: http://git.scottworley.com/reliable-chat/commitdiff_plain/d69a32e6e69f53f03cd69d13042de685130b3b06?hp=86945f8d2e10192da9f9ad7f5678f382d83b55fd Test partial fetch --- diff --git a/server/server_test.go b/server/server_test.go index fd4a4c8..13ad4a9 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -2,6 +2,7 @@ package main import "testing" import "runtime" +import "strconv" import "time" func TestMessageInsertAndRetreive(t *testing.T) { @@ -14,7 +15,7 @@ func TestMessageInsertAndRetreive(t *testing.T) { store.Get <- &StoreRequest{zero_time, messages_from_store} messages := <-messages_from_store if len(messages) != 1 { - t.Fail() + t.FailNow() } if messages[0].Time != at { t.Fail() @@ -40,7 +41,7 @@ func TestFetchBlocksUntilSpeak(t *testing.T) { store.Add <- &Message{at, say} messages := <-messages_from_store if len(messages) != 1 { - t.Fail() + t.FailNow() } if messages[0].Time != at { t.Fail() @@ -67,7 +68,7 @@ func TestMultipleListeners(t *testing.T) { for i := 0; i < num_clients; i++ { messages := <-messages_from_store[i] if len(messages) != 1 { - t.Fail() + t.FailNow() } if messages[0].Time != at { t.Fail() @@ -79,3 +80,58 @@ func TestMultipleListeners(t *testing.T) { close(store.Get) close(store.Add) } + +func parseDuration(s string) time.Duration { + d, err := time.ParseDuration(s) + if err != nil { + panic(err) + } + return d +} + +func atoi(s string) int { + i, err := strconv.Atoi(s) + if err != nil { + panic(err) + } + return i +} + +func TestPartialRetreive(t *testing.T) { + start_speak_count := atoi(speak_count.String()) + say1 := "No, no.....No, 'e's stunned!" + say2 := "You stunned him, just as he was wakin' up!" + say3 := "Norwegian Blues stun easily, major." + base := time.Now() + at1 := base.Add(parseDuration("-4m")) + since := base.Add(parseDuration("-3m")) + at2 := base.Add(parseDuration("-2m")) + at3 := base.Add(parseDuration("-1m")) + store := start_store() + store.Add <- &Message{at1, say1} + store.Add <- &Message{at2, say2} + store.Add <- &Message{at3, say3} + for atoi(speak_count.String()) != start_speak_count+3 { + runtime.Gosched() + } + messages_from_store := make(chan []Message, 1) + store.Get <- &StoreRequest{since, messages_from_store} + messages := <-messages_from_store + if len(messages) != 2 { + t.FailNow() + } + if messages[0].Time != at2 { + t.Fail() + } + if messages[0].Text != say2 { + t.Fail() + } + if messages[1].Time != at3 { + t.Fail() + } + if messages[1].Text != say3 { + t.Fail() + } + close(store.Get) + close(store.Add) +}