Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Dec 24, 2019
1 parent 7e43702 commit 6e39fe9
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions aio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,82 @@ func TestEchoHuge(t *testing.T) {
conn.Close()
}

func TestBufferedDone(t *testing.T) {
ln := echoServer(t)
conn, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatal(err)
}

w, err := CreateWatcher(bufSize)
if err != nil {
t.Fatal(err)
}

fd, err := w.Watch(conn)
if err != nil {
t.Fatal(err)
}

err = w.Read(fd, make(chan OpResult, 1))
if err != ErrBufferedChan {
t.Fatal("misbehavior")
}
conn.Close()
}

func TestBidirectionWatcher(t *testing.T) {
ln := echoServer(t)
conn, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatal(err)
}

w, err := CreateWatcher(bufSize)
if err != nil {
t.Fatal(err)
}

fd, err := w.Watch(conn)
if err != nil {
t.Fatal(err)
}

tx := []byte("hello world")
doneR := make(chan OpResult)
doneW := make(chan OpResult)
die := make(chan struct{})
go func() {
for {
select {
case res := <-doneW:
// recv
if res.Err != nil {
t.Fatal(res.Err)
}

t.Log("written:", res.Err, res.Size)
err = w.Read(fd, doneR)
if err != nil {
t.Fatal(err)
}
case res := <-doneR:
t.Log("read:", res.Err, res.Size)
close(die)
return
}
}
}()

// send
err = w.Write(fd, tx, doneW)
if err != nil {
t.Fatal(err)
}
<-die
conn.Close()
}

func BenchmarkEcho(b *testing.B) {
ln := echoServer(b)

Expand Down

0 comments on commit 6e39fe9

Please sign in to comment.