Skip to content

Commit

Permalink
Merge pull request #39 from shogo82148/add-test-for-register-proxy
Browse files Browse the repository at this point in the history
add an example of RegisterProxy
  • Loading branch information
shogo82148 committed Jan 4, 2020
2 parents 3bb7f3b + 1bec0ef commit d3f360d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@ This software is released under the MIT License, see LICENSE file.

## godoc

See [godoc](https://godoc.org/github.com/shogo82148/go-sql-proxy) for more imformation.
See [godoc](https://godoc.org/github.com/shogo82148/go-sql-proxy) for more information.
14 changes: 7 additions & 7 deletions proxy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// a proxy package is a proxy driver for dabase/sql.
// a proxy package is a proxy driver for database/sql.

package proxy

Expand Down Expand Up @@ -71,7 +71,7 @@ type HooksContext struct {
// `Hooks.PrePing` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Cpnn.Ping` method.
// callback is returned by the `Conn.Ping` method.
Ping func(c context.Context, ctx interface{}, conn *Conn) error

// PostPing is a callback that gets called at the end of
Expand Down Expand Up @@ -294,7 +294,7 @@ type HooksContext struct {
// `Hooks.PreClose` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Cpnn.Close` method.
// callback is returned by the `Conn.Close` method.
Close func(c context.Context, ctx interface{}, conn *Conn) error

// PostClose is a callback that gets called at the end of
Expand Down Expand Up @@ -326,7 +326,7 @@ type HooksContext struct {
// `Hooks.PreResetSession` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Cpnn.ResetSession` method.
// callback is returned by the `Conn.ResetSession` method.
ResetSession func(c context.Context, ctx interface{}, conn *Conn) error

// PostResetSession is a callback that gets called at the end of
Expand Down Expand Up @@ -551,7 +551,7 @@ type Hooks struct {
// `Hooks.PrePing` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Cpnn.Ping` method.
// callback is returned by the `Conn.Ping` method.
Ping func(ctx interface{}, conn *Conn) error

// PostPing is a callback that gets called at the end of
Expand Down Expand Up @@ -774,7 +774,7 @@ type Hooks struct {
// `Hooks.PreClose` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Cpnn.Close` method.
// callback is returned by the `Conn.Close` method.
Close func(ctx interface{}, conn *Conn) error

// PostClose is a callback that gets called at the end of
Expand Down Expand Up @@ -806,7 +806,7 @@ type Hooks struct {
// `Hooks.PreResetSession` method, and may be nil.
//
// If this callback returns an error, then the error from this
// callback is returned by the `Cpnn.ResetSession` method.
// callback is returned by the `Conn.ResetSession` method.
ResetSession func(ctx interface{}, conn *Conn) error

// PostResetSession is a callback that gets called at the end of
Expand Down
44 changes: 44 additions & 0 deletions proxy_register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package proxy_test

import (
"context"
"database/sql"
"fmt"
"log"

proxy "github.com/shogo82148/go-sql-proxy"
)

func ExampleRegisterProxy() {
proxy.RegisterProxy()
db, err := sql.Open("fakedb:proxy", `{"name":"trace"}`)
if err != nil {
log.Fatal(err)
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// proxy.RegisterProxy register hook points.
// do nothing by default.
if err := db.PingContext(ctx); err != nil {
log.Fatal(err)
}

// proxy.WithHooks enables the hooks in this context.
ctx = proxy.WithHooks(context.Background(), &proxy.HooksContext{
Ping: func(c context.Context, ctx interface{}, conn *proxy.Conn) error {
fmt.Println("Ping")
return nil
},
})
if err := db.PingContext(ctx); err != nil {
log.Fatal(err)
}
// Output:
// Ping

if err := db.Close(); err != nil {
log.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ func TestFakeDB(t *testing.T) {
}
db.Close()

// Run test queris via a proxy
// Run test queries via a proxy
tc.opt.Name = fmt.Sprintf("%s-proxy-%s", testName, name)
dbProxyName, err := json.Marshal(tc.opt)
dbProxy, err := sql.Open(driverName, string(dbProxyName))
Expand Down

0 comments on commit d3f360d

Please sign in to comment.