making tests a little better

master
Stephen Searles 7 years ago
parent 266d4ea5fb
commit 07e2e20170
  1. 80
      client_test.go

@ -5,43 +5,75 @@ import (
"time" "time"
) )
func makeClient(doc *Document) (*Client, chan []Delta) {
ch := make(chan []Delta, 5)
client := doc.Client(DeltaHandlerFunc(func(ds []Delta) error {
ch <- ds
return nil
}))
return client, ch
}
func receiveMessages(ch chan []Delta, expected ...Delta) error {
select {
case got := <-ch:
if len(got) != len(expected) {
return fmt.Errorf("got %d deltas, expected %d", len(got), len(expected))
}
for i, gotD := range got {
if !gotD.Equal(expected[i]) {
return fmt.Errorf("didn't get the correct delta, saw: %v, expected: %v", gotD, expected[i])
}
}
case <-time.After(10 * time.Millisecond):
return fmt.Errorf("didn't get any delta")
}
return nil
}
func TestClient(t *testing.T) { func TestClient(t *testing.T) {
doc := NewString("") doc := NewString("")
aliceCh := make(chan []Delta, 5) alice, aliceCh := makeClient(doc)
alice := doc.Client(DeltaHandlerFunc(func(ds []Delta) error {
aliceCh <- ds
return nil
}))
defer alice.Close() defer alice.Close()
bobCh := make(chan []Delta, 5) bob, bobCh := makeClient(doc)
bob := doc.Client(DeltaHandlerFunc(func(ds []Delta) error {
bobCh <- ds
return nil
}))
defer bob.Close() defer bob.Close()
delta := Insert(0, 0, "alphabet") charles, charlesCh := makeClient(doc)
defer charles.Close()
err := alice.PushDeltas(delta) deltaA := Insert(0, 0, "alphabet")
deltaB := Insert(0, 8, "\nbatman")
err := alice.PushDeltas(deltaA)
if err != nil { if err != nil {
t.Error("got error pushing delta:", err) t.Error("got error pushing delta:", err)
} }
select { err = bob.PushDeltas(deltaB)
case got := <-bobCh: if err != nil {
if len(got) != 1 { t.Error("got error pushing delta:", err)
t.Errorf("got %d deltas, expected %d", len(got), 1) }
} else if !got[0].Equal(delta) {
t.Error("didn't get the correct delta, saw:", got[0], "expected:", delta) err = receiveMessages(aliceCh, deltaB)
} if err != nil {
case <-time.After(10 * time.Millisecond): t.Error("alice didn't receive the correct messages:", err)
t.Error("didn't get any delta") }
err = receiveMessages(bobCh, deltaA)
if err != nil {
t.Error("bob didn't receive the correct messages:", err)
}
err = receiveMessages(charlesCh, deltaA, deltaB)
if err != nil {
t.Error("charles didn't receive the correct messages:", err)
} }
if got := doc.Contents(); got != "alphabet" { if got := doc.Contents(); got != "alphabet\nbatman" {
t.Errorf("saw doc %q, expected %q", got, "alphabet") t.Errorf("saw doc %q, expected %q", got, "alphabet\nbatman")
} }
select { select {
@ -49,6 +81,8 @@ func TestClient(t *testing.T) {
t.Error("got an unexpected message via Bob") t.Error("got an unexpected message via Bob")
case <-aliceCh: case <-aliceCh:
t.Error("got an unexpected message via Alice") t.Error("got an unexpected message via Alice")
case <-charlesCh:
t.Error("got an unexpected message via Charles")
case <-time.After(100 * time.Millisecond): case <-time.After(100 * time.Millisecond):
// expected // expected
} }

Loading…
Cancel
Save