Skip to content

Commit

Permalink
cleanup and fix some test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lrstanley committed Aug 19, 2017
1 parent b2151fb commit 2e74745
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 47 deletions.
54 changes: 20 additions & 34 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package girc

import (
"context"
"io"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -100,17 +98,16 @@ func TestClientUptime(t *testing.T) {
c, conn, server := genMockConn()
defer conn.Close()
defer server.Close()
go mockReadBuffer(conn)

ctx, cancel := context.WithCancel(context.Background())
c.Handlers.Add(INITIALIZED, func(c *Client, e Event) {
cancel()
})
done := make(chan struct{}, 1)
c.Handlers.Add(INITIALIZED, func(c *Client, e Event) { close(done) })

go c.MockConnect(server)
defer c.Close()

select {
case <-ctx.Done():
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("Client.Uptime() timed out")
}
Expand Down Expand Up @@ -146,17 +143,16 @@ func TestClientGet(t *testing.T) {
c, conn, server := genMockConn()
defer conn.Close()
defer server.Close()
go mockReadBuffer(conn)

ctx, cancel := context.WithCancel(context.Background())
c.Handlers.Add(INITIALIZED, func(c *Client, e Event) {
cancel()
})
done := make(chan struct{}, 1)
c.Handlers.Add(INITIALIZED, func(c *Client, e Event) { close(done) })

go c.MockConnect(server)
defer c.Close()

select {
case <-ctx.Done():
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("timed out during connect")
}
Expand All @@ -178,37 +174,27 @@ func TestClientClose(t *testing.T) {
c, conn, server := genMockConn()
defer server.Close()
defer conn.Close()
go mockReadBuffer(conn)

errchan := make(chan error, 1)
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{}, 1)

c.Handlers.AddBg(STOPPED, func(c *Client, e Event) {
cancel()
})
c.Handlers.AddBg(INITIALIZED, func(c *Client, e Event) {
c.Close()
})
c.Handlers.AddBg(STOPPED, func(c *Client, e Event) { close(done) })
c.Handlers.AddBg(INITIALIZED, func(c *Client, e Event) { c.Close() })

go func() {
errchan <- c.MockConnect(server)
}()
defer c.Close()
go func() { errchan <- c.MockConnect(server) }()

select {
case <-time.After(5 * time.Second):
t.Fatal("Client.Close() timed out")
cancel()
case <-ctx.Done():
}
defer c.Close()

select {
case err := <-errchan:
if err != nil && err != io.ErrClosedPipe {
t.Fatalf("connect returned with error when close was invoked: %s", err)
if err == nil {
break
}

t.Fatalf("connect returned with error when close was invoked: %s", err)
case <-time.After(5 * time.Second):
t.Fatal("timed out while waiting for connect to return")
t.Fatal("Client.Close() timed out")
case <-done:
}

close(errchan)
}
12 changes: 12 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,15 @@ func genMockConn() (client *Client, clientConn net.Conn, serverConn net.Conn) {

return client, conn1, conn2
}

func mockReadBuffer(conn net.Conn) {
// Accept all outgoing writes from the client.
b := bufio.NewReader(conn)
for {
conn.SetReadDeadline(time.Now().Add(300 * time.Second))
_, err := b.ReadString(byte('\n'))
if err != nil {
return
}
}
}
14 changes: 1 addition & 13 deletions state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package girc

import (
"bufio"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -59,18 +58,7 @@ const dummyEndState = `:nick2!nick2@other.int QUIT :example reason
func TestState(t *testing.T) {
c, conn, server := genMockConn()
defer c.Close()

go func() {
// Accept all outgoing writes from the client.
b := bufio.NewReader(conn)
for {
conn.SetReadDeadline(time.Now().Add(300 * time.Second))
_, err := b.ReadString(byte('\n'))
if err != nil {
return
}
}
}()
go mockReadBuffer(conn)

go func() {
err := c.MockConnect(server)
Expand Down

0 comments on commit 2e74745

Please sign in to comment.