You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.9 KiB

package acedoc
import (
"fmt"
"testing"
"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("")
alice, aliceCh := makeClient(doc)
defer alice.Close()
bob, bobCh := makeClient(doc)
defer bob.Close()
charles, charlesCh := makeClient(doc)
defer charles.Close()
deltaA := Insert(0, 0, "alphabet")
deltaB := Insert(0, 8, "\nbatman")
err := alice.PushDeltas(deltaA)
if err != nil {
t.Error("got error pushing delta:", err)
}
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\nbatman" {
t.Errorf("saw doc %q, expected %q", got, "alphabet\nbatman")
}
select {
case <-bobCh:
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
}
}