Skip to content

Commit

Permalink
-a
Browse files Browse the repository at this point in the history
Regression test for go-sql-driver#1023.
  • Loading branch information
mherr-google committed Nov 1, 2019
1 parent fa56a8f commit 65d32e4
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions driver_go110_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,55 @@ func TestConnectorTimeoutsDuringOpen(t *testing.T) {
t.Fatalf("(*Connector).Connect should have timed out")
}
}

// A connection which can only be closed.
type dummyConnection struct {
net.Conn
closed bool
}

func (d *dummyConnection) Close() error {
d.closed = true
return nil
}

func TestConnectorTimeoutsWatchCancel(t *testing.T) {
var (
cancel func() // Used to cancel the context just after connecting.
created *dummyConnection // The created connection.
)

RegisterDialContext("TestConnectorTimeoutsWatchCancel", func(ctx context.Context, addr string) (net.Conn, error) {
// Canceling at this time triggers the watchCancel error branch in Connect().
cancel()
created = &dummyConnection{}
return created, nil
})

mycnf := NewConfig()
mycnf.User = "root"
mycnf.Addr = "foo"
mycnf.Net = "TestConnectorTimeoutsWatchCancel"

conn, err := NewConnector(mycnf)
if err != nil {
t.Fatal(err)
}

db := sql.OpenDB(conn)
defer db.Close()

var ctx context.Context
ctx, cancel = context.WithCancel(context.Background())

if _, err := db.Conn(ctx); err != context.Canceled {
t.Errorf("got %v, want context.Canceled", err)
}

if created == nil {
t.Fatal("no connection created")
}
if !created.closed {
t.Errorf("connection not closed")
}
}

0 comments on commit 65d32e4

Please sign in to comment.