From: Scott Worley Date: Sun, 29 Jul 2012 00:26:37 +0000 (-0700) Subject: Add an id to messages X-Git-Url: http://git.scottworley.com/reliable-chat/commitdiff_plain/b199796a3d2c82a84243e3a5ad3099315d8f1b55 Add an id to messages This is how clients de-dupe. --- diff --git a/server/server.go b/server/server.go index 5837032..8ce9678 100644 --- a/server/server.go +++ b/server/server.go @@ -18,6 +18,7 @@ var fetch_wake_count = expvar.NewInt("fetch_wake_count") type Message struct { Time time.Time + ID string Text string } @@ -121,7 +122,10 @@ func start_server(store Store) { }) http.HandleFunc("/speak", func(w http.ResponseWriter, r *http.Request) { - store.Add <- &Message{time.Now(), r.FormValue("text")} + store.Add <- &Message{ + time.Now(), + r.FormValue("id"), + r.FormValue("text")} }) log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil)) diff --git a/server/server_test.go b/server/server_test.go index 72e2b21..0634b8f 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -6,11 +6,12 @@ import "strconv" import "time" func TestMessageInsertAndRetreive(t *testing.T) { + id := "1" say := "'Ello, Mister Polly Parrot!" at := time.Now() var zero_time time.Time store := start_store() - store.Add <- &Message{at, say} + store.Add <- &Message{at, id, say} messages_from_store := make(chan []Message, 1) store.Get <- &StoreRequest{zero_time, messages_from_store} messages := <-messages_from_store @@ -20,6 +21,9 @@ func TestMessageInsertAndRetreive(t *testing.T) { if messages[0].Time != at { t.Fail() } + if messages[0].ID != id { + t.Fail() + } if messages[0].Text != say { t.Fail() } @@ -29,6 +33,7 @@ func TestMessageInsertAndRetreive(t *testing.T) { func TestFetchBlocksUntilSpeak(t *testing.T) { start_fetch_wait_count := fetch_wait_count.String() + id := "2" say := "I've got a lovely fresh cuttle fish for you" at := time.Now() var zero_time time.Time @@ -38,7 +43,7 @@ func TestFetchBlocksUntilSpeak(t *testing.T) { for start_fetch_wait_count == fetch_wait_count.String() { runtime.Gosched() } - store.Add <- &Message{at, say} + store.Add <- &Message{at, id, say} messages := <-messages_from_store if len(messages) != 1 { t.FailNow() @@ -46,6 +51,9 @@ func TestFetchBlocksUntilSpeak(t *testing.T) { if messages[0].Time != at { t.Fail() } + if messages[0].ID != id { + t.Fail() + } if messages[0].Text != say { t.Fail() } @@ -54,6 +62,7 @@ func TestFetchBlocksUntilSpeak(t *testing.T) { } func TestMultipleListeners(t *testing.T) { + id := "3" say := "This is your nine o'clock alarm call!" at := time.Now() var zero_time time.Time @@ -64,7 +73,7 @@ func TestMultipleListeners(t *testing.T) { messages_from_store[i] = make(chan []Message, 1) store.Get <- &StoreRequest{zero_time, messages_from_store[i]} } - store.Add <- &Message{at, say} + store.Add <- &Message{at, id, say} for i := 0; i < num_clients; i++ { messages := <-messages_from_store[i] if len(messages) != 1 { @@ -73,6 +82,9 @@ func TestMultipleListeners(t *testing.T) { if messages[0].Time != at { t.Fail() } + if messages[0].ID != id { + t.Fail() + } if messages[0].Text != say { t.Fail() } @@ -99,6 +111,9 @@ func atoi(s string) int { func TestPartialRetreive(t *testing.T) { start_speak_count := atoi(speak_count.String()) + id1 := "4" + id2 := "5" + id3 := "6" say1 := "No, no.....No, 'e's stunned!" say2 := "You stunned him, just as he was wakin' up!" say3 := "Norwegian Blues stun easily, major." @@ -108,9 +123,9 @@ func TestPartialRetreive(t *testing.T) { 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} + 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() } @@ -123,12 +138,18 @@ func TestPartialRetreive(t *testing.T) { 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() } @@ -138,6 +159,9 @@ func TestPartialRetreive(t *testing.T) { func TestPrecisePartialRetreive(t *testing.T) { start_speak_count := atoi(speak_count.String()) + id1 := "7" + id2 := "8" + id3 := "9" say1 := "Well, he's...he's, ah...probably pining for the fjords." say2 := "PININ' for the FJORDS?!?!?!?" say3 := "look, why did he fall flat on his back the moment I got 'im home?" @@ -147,9 +171,9 @@ func TestPrecisePartialRetreive(t *testing.T) { at3 := base.Add(parseDuration("-1m")) since := at2 store := start_store() - store.Add <- &Message{at1, say1} - store.Add <- &Message{at2, say2} - store.Add <- &Message{at3, say3} + 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() } @@ -162,6 +186,9 @@ func TestPrecisePartialRetreive(t *testing.T) { if messages[0].Time != at3 { t.Fail() } + if messages[0].ID != id3 { + t.Fail() + } if messages[0].Text != say3 { t.Fail() } @@ -170,6 +197,8 @@ func TestPrecisePartialRetreive(t *testing.T) { } func TestTypicalFlow(t *testing.T) { + id1 := "10" + id2 := "11" say1 := "The Norwegian Blue prefers kippin' on it's back!" say2 := "Remarkable bird, innit, squire? Lovely plumage!" store := start_store() @@ -185,7 +214,7 @@ func TestTypicalFlow(t *testing.T) { // Someone speaks. This triggers delivery. at1 := time.Now() - store.Add <- &Message{at1, say1} + store.Add <- &Message{at1, id1, say1} messages1 := <-fetch1 if len(messages1) != 1 { t.FailNow() @@ -193,6 +222,9 @@ func TestTypicalFlow(t *testing.T) { if messages1[0].Time != at1 { t.Fail() } + if messages1[0].ID != id1 { + t.Fail() + } if messages1[0].Text != say1 { t.Fail() } @@ -210,7 +242,7 @@ func TestTypicalFlow(t *testing.T) { if !at2.After(at1) { t.Fail() } - store.Add <- &Message{at2, say2} + store.Add <- &Message{at2, id2, say2} messages2 := <-fetch2 if len(messages2) != 1 { t.FailNow() @@ -218,6 +250,9 @@ func TestTypicalFlow(t *testing.T) { if messages2[0].Time != at2 { t.Fail() } + if messages2[0].ID != id2 { + t.Fail() + } if messages2[0].Text != say2 { t.Fail() }