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"
)
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) {
doc := NewString("")
aliceCh := make(chan []Delta, 5)
alice := doc.Client(DeltaHandlerFunc(func(ds []Delta) error {
aliceCh <- ds
return nil
}))
alice, aliceCh := makeClient(doc)
defer alice.Close()
bobCh := make(chan []Delta, 5)
bob := doc.Client(DeltaHandlerFunc(func(ds []Delta) error {
bobCh <- ds
return nil
}))
bob, bobCh := makeClient(doc)
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 {
t.Error("got error pushing delta:", err)
}
select {
case got := <-bobCh:
if len(got) != 1 {
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)
}
case <-time.After(10 * time.Millisecond):
t.Error("didn't get any delta")
err = bob.PushDeltas(deltaB)
if err != nil {
t.Error("got error pushing delta:", err)
}
err = receiveMessages(aliceCh, deltaB)
if err != nil {
t.Error("alice didn't receive the correct messages:", err)
}
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" {
t.Errorf("saw doc %q, expected %q", got, "alphabet")
if got := doc.Contents(); got != "alphabet\nbatman" {
t.Errorf("saw doc %q, expected %q", got, "alphabet\nbatman")
}
select {
@ -49,6 +81,8 @@ func TestClient(t *testing.T) {
t.Error("got an unexpected message via Bob")
case <-aliceCh:
t.Error("got an unexpected message via Alice")
case <-charlesCh:
t.Error("got an unexpected message via Charles")
case <-time.After(100 * time.Millisecond):
// expected
}

Loading…
Cancel
Save