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.
acedoc/client_test.go

55 lines
1.1 KiB

package acedoc
import (
"testing"
"time"
)
func TestClient(t *testing.T) {
doc := NewString("")
aliceCh := make(chan []Delta, 5)
alice := doc.Client(DeltaHandlerFunc(func(ds []Delta) error {
aliceCh <- ds
return nil
}))
defer alice.Close()
bobCh := make(chan []Delta, 5)
bob := doc.Client(DeltaHandlerFunc(func(ds []Delta) error {
bobCh <- ds
return nil
}))
defer bob.Close()
delta := Insert(0, 0, "alphabet")
err := alice.PushDeltas(delta)
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")
}
if got := doc.Contents(); got != "alphabet" {
t.Errorf("saw doc %q, expected %q", got, "alphabet")
}
select {
case <-bobCh:
t.Error("got an unexpected message via Bob")
case <-aliceCh:
t.Error("got an unexpected message via Alice")
case <-time.After(100 * time.Millisecond):
// expected
}
}