From e5d16dd2d5b3aded159c2d9037ed4a9ee63a85cc Mon Sep 17 00:00:00 2001 From: Stephen Searles Date: Sat, 5 Aug 2017 18:06:34 -0700 Subject: [PATCH] skipping pushing message to client if there are no messages; fixes flaky test and de-intensifies read loop --- deltas.go | 10 ++++++++-- doc_test.go | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/deltas.go b/deltas.go index 9c81797..b891d15 100644 --- a/deltas.go +++ b/deltas.go @@ -92,8 +92,9 @@ func (ch *CaddyHugo) handleConn(conn DeltaConn, doc *docref) (int, error) { wroteMessagesCh := make(chan Message, 2) client := doc.doc.Client(acedoc.DeltaHandlerFunc(func(ds []acedoc.Delta) error { - wroteMessagesCh <- Message{Deltas: ds} - return conn.WriteJSON(ch.Message(ds...)) + m := ch.Message(ds...) + wroteMessagesCh <- m + return conn.WriteJSON(m) })) ch.mtx.Lock() @@ -119,6 +120,11 @@ func (ch *CaddyHugo) handleConn(conn DeltaConn, doc *docref) (int, error) { } ch.ObserveLTime(message.LTime) + if len(message.Deltas) == 0 { + time.Sleep(10 * time.Microsecond) + continue + } + err = client.PushDeltas(message.Deltas...) if err != nil { errCh <- err diff --git a/doc_test.go b/doc_test.go index f266755..12d15d7 100644 --- a/doc_test.go +++ b/doc_test.go @@ -233,7 +233,7 @@ func TestDeltasDouble(t *testing.T) { // send the second message, via clientB clientB.ReceiveJSON(w.CH.Message(acedoc.Insert(0, 0, "b"))) - time.Sleep(200 * time.Millisecond) + time.Sleep(400 * time.Millisecond) clientA.mtx.Lock() clientB.mtx.Lock()