X-Git-Url: http://git.scottworley.com/reliable-chat/blobdiff_plain/b199796a3d2c82a84243e3a5ad3099315d8f1b55..7310ffeebfda8540d2113fc8c846971a0f913d57:/server/server_test.go?ds=sidebyside diff --git a/server/server_test.go b/server/server_test.go index 0634b8f..7f3434c 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -1,3 +1,20 @@ +/* reliable-chat - multipath chat + * Copyright (C) 2012 Scott Worley + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + package main import "testing" @@ -5,12 +22,29 @@ import "runtime" import "strconv" import "time" +func expectMessage(t *testing.T, m *Message, at time.Time, id, say string) { + if m.Time != at { + t.Fail() + } + if m.ID != id { + t.Fail() + } + if m.Text != say { + t.Fail() + } +} + func TestMessageInsertAndRetreive(t *testing.T) { id := "1" say := "'Ello, Mister Polly Parrot!" at := time.Now() var zero_time time.Time store := start_store() + defer func() { + close(store.Get) + close(store.Add) + }() + store.Add <- &Message{at, id, say} messages_from_store := make(chan []Message, 1) store.Get <- &StoreRequest{zero_time, messages_from_store} @@ -18,17 +52,7 @@ func TestMessageInsertAndRetreive(t *testing.T) { if len(messages) != 1 { t.FailNow() } - if messages[0].Time != at { - t.Fail() - } - if messages[0].ID != id { - t.Fail() - } - if messages[0].Text != say { - t.Fail() - } - close(store.Get) - close(store.Add) + expectMessage(t, &messages[0], at, id, say) } func TestFetchBlocksUntilSpeak(t *testing.T) { @@ -38,6 +62,11 @@ func TestFetchBlocksUntilSpeak(t *testing.T) { at := time.Now() var zero_time time.Time store := start_store() + defer func() { + close(store.Get) + close(store.Add) + }() + messages_from_store := make(chan []Message, 1) store.Get <- &StoreRequest{zero_time, messages_from_store} for start_fetch_wait_count == fetch_wait_count.String() { @@ -48,17 +77,7 @@ func TestFetchBlocksUntilSpeak(t *testing.T) { if len(messages) != 1 { t.FailNow() } - if messages[0].Time != at { - t.Fail() - } - if messages[0].ID != id { - t.Fail() - } - if messages[0].Text != say { - t.Fail() - } - close(store.Get) - close(store.Add) + expectMessage(t, &messages[0], at, id, say) } func TestMultipleListeners(t *testing.T) { @@ -67,6 +86,11 @@ func TestMultipleListeners(t *testing.T) { at := time.Now() var zero_time time.Time store := start_store() + defer func() { + close(store.Get) + close(store.Add) + }() + const num_clients = 13 var messages_from_store [num_clients]chan []Message for i := 0; i < num_clients; i++ { @@ -79,18 +103,8 @@ func TestMultipleListeners(t *testing.T) { if len(messages) != 1 { t.FailNow() } - if messages[0].Time != at { - t.Fail() - } - if messages[0].ID != id { - t.Fail() - } - if messages[0].Text != say { - t.Fail() - } + expectMessage(t, &messages[0], at, id, say) } - close(store.Get) - close(store.Add) } func parseDuration(s string) time.Duration { @@ -123,6 +137,11 @@ func TestPartialRetreive(t *testing.T) { at2 := base.Add(parseDuration("-2m")) at3 := base.Add(parseDuration("-1m")) store := start_store() + defer func() { + close(store.Get) + close(store.Add) + }() + store.Add <- &Message{at1, id1, say1} store.Add <- &Message{at2, id2, say2} store.Add <- &Message{at3, id3, say3} @@ -135,26 +154,8 @@ func TestPartialRetreive(t *testing.T) { if len(messages) != 2 { t.FailNow() } - if messages[0].Time != at2 { - t.Fail() - } - if messages[0].ID != id2 { - t.Fail() - } - if messages[0].Text != say2 { - t.Fail() - } - if messages[1].Time != at3 { - t.Fail() - } - if messages[1].ID != id3 { - t.Fail() - } - if messages[1].Text != say3 { - t.Fail() - } - close(store.Get) - close(store.Add) + expectMessage(t, &messages[0], at2, id2, say2) + expectMessage(t, &messages[1], at3, id3, say3) } func TestPrecisePartialRetreive(t *testing.T) { @@ -171,6 +172,11 @@ func TestPrecisePartialRetreive(t *testing.T) { at3 := base.Add(parseDuration("-1m")) since := at2 store := start_store() + defer func() { + close(store.Get) + close(store.Add) + }() + store.Add <- &Message{at1, id1, say1} store.Add <- &Message{at2, id2, say2} store.Add <- &Message{at3, id3, say3} @@ -183,17 +189,7 @@ func TestPrecisePartialRetreive(t *testing.T) { if len(messages) != 1 { t.FailNow() } - if messages[0].Time != at3 { - t.Fail() - } - if messages[0].ID != id3 { - t.Fail() - } - if messages[0].Text != say3 { - t.Fail() - } - close(store.Get) - close(store.Add) + expectMessage(t, &messages[0], at3, id3, say3) } func TestTypicalFlow(t *testing.T) { @@ -202,6 +198,10 @@ func TestTypicalFlow(t *testing.T) { say1 := "The Norwegian Blue prefers kippin' on it's back!" say2 := "Remarkable bird, innit, squire? Lovely plumage!" store := start_store() + defer func() { + close(store.Get) + close(store.Add) + }() // A waiting zero-time fetch. var zero_time time.Time @@ -219,15 +219,7 @@ func TestTypicalFlow(t *testing.T) { if len(messages1) != 1 { t.FailNow() } - if messages1[0].Time != at1 { - t.Fail() - } - if messages1[0].ID != id1 { - t.Fail() - } - if messages1[0].Text != say1 { - t.Fail() - } + expectMessage(t, &messages1[0], at1, id1, say1) // Upon recipt, client blocks on fetch with since=at1 prev_fetch_wait_count = fetch_wait_count.String() @@ -247,16 +239,48 @@ func TestTypicalFlow(t *testing.T) { if len(messages2) != 1 { t.FailNow() } - if messages2[0].Time != at2 { - t.Fail() + expectMessage(t, &messages2[0], at2, id2, say2) +} + +func TestExpiryDueToLimit(t *testing.T) { + previous_limit := *max_messages + defer func() { *max_messages = previous_limit }() + *max_messages = 2 + + start_speak_count := atoi(speak_count.String()) + start_drop_count := atoi(drop_due_to_limit_count.String()) + id1 := "12" + id2 := "13" + id3 := "14" + say1 := "'E's passed on!" + say2 := "This parrot is no more!" + say3 := "He has ceased to be!" + base := time.Now() + at1 := base.Add(parseDuration("-3m")) + at2 := base.Add(parseDuration("-2m")) + at3 := base.Add(parseDuration("-1m")) + store := start_store() + defer func() { + close(store.Get) + close(store.Add) + }() + + store.Add <- &Message{at1, id1, say1} + store.Add <- &Message{at2, id2, say2} + store.Add <- &Message{at3, id3, say3} + for atoi(speak_count.String()) != start_speak_count+3 { + runtime.Gosched() } - if messages2[0].ID != id2 { + if atoi(drop_due_to_limit_count.String()) != start_drop_count+1 { t.Fail() } - if messages2[0].Text != say2 { - t.Fail() + messages_from_store := make(chan []Message, 1) + var zero_time time.Time + store.Get <- &StoreRequest{zero_time, messages_from_store} + messages := <-messages_from_store + if len(messages) != 2 { + t.FailNow() } - - close(store.Get) - close(store.Add) + expectMessage(t, &messages[0], at2, id2, say2) + expectMessage(t, &messages[1], at3, id3, say3) }