Skip to content

Commit

Permalink
Fix ability to pass TracingEventReceiver to dbr (#154)
Browse files Browse the repository at this point in the history
* add failing tests that exercise TracingEventReceiver

* pass EventReceiver explicitly between types rather than embedded parent struct

* Code review comments for #154

* address code review comments for #154
  • Loading branch information
cce authored and taylorchu committed Oct 29, 2018
1 parent 018a08e commit 042fe86
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 18 deletions.
8 changes: 7 additions & 1 deletion dbr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
)

func createSession(driver, dsn string) *Session {
conn, err := Open(driver, dsn, nil)
conn, err := Open(driver, dsn, &testTraceReceiver{})
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -88,6 +88,8 @@ func reset(t *testing.T, sess *Session) {
_, err := sess.Exec(v)
require.NoError(t, err)
}
// clear test data collected by testTraceReceiver
sess.EventReceiver = &testTraceReceiver{}
}

func TestBasicCRUD(t *testing.T) {
Expand Down Expand Up @@ -174,15 +176,19 @@ func TestTimeout(t *testing.T) {
var people []dbrPerson
_, err := sess.Select("*").From("dbr_people").Load(&people)
require.Equal(t, context.DeadlineExceeded, err)
require.Equal(t, 1, sess.EventReceiver.(*testTraceReceiver).errored)

_, err = sess.InsertInto("dbr_people").Columns("name", "email").Values("test", "[email protected]").Exec()
require.Equal(t, context.DeadlineExceeded, err)
require.Equal(t, 2, sess.EventReceiver.(*testTraceReceiver).errored)

_, err = sess.Update("dbr_people").Set("name", "test1").Exec()
require.Equal(t, context.DeadlineExceeded, err)
require.Equal(t, 3, sess.EventReceiver.(*testTraceReceiver).errored)

_, err = sess.DeleteFrom("dbr_people").Exec()
require.Equal(t, context.DeadlineExceeded, err)
require.Equal(t, 4, sess.EventReceiver.(*testTraceReceiver).errored)

// tx op timeout
sess.Timeout = 0
Expand Down
8 changes: 4 additions & 4 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func DeleteFrom(table string) *DeleteStmt {
func (sess *Session) DeleteFrom(table string) *DeleteStmt {
b := DeleteFrom(table)
b.runner = sess
b.EventReceiver = sess
b.EventReceiver = sess.EventReceiver
b.Dialect = sess.Dialect
return b
}
Expand All @@ -68,7 +68,7 @@ func (sess *Session) DeleteFrom(table string) *DeleteStmt {
func (tx *Tx) DeleteFrom(table string) *DeleteStmt {
b := DeleteFrom(table)
b.runner = tx
b.EventReceiver = tx
b.EventReceiver = tx.EventReceiver
b.Dialect = tx.Dialect
return b
}
Expand All @@ -88,7 +88,7 @@ func DeleteBySql(query string, value ...interface{}) *DeleteStmt {
func (sess *Session) DeleteBySql(query string, value ...interface{}) *DeleteStmt {
b := DeleteBySql(query, value...)
b.runner = sess
b.EventReceiver = sess
b.EventReceiver = sess.EventReceiver
b.Dialect = sess.Dialect
return b
}
Expand All @@ -97,7 +97,7 @@ func (sess *Session) DeleteBySql(query string, value ...interface{}) *DeleteStmt
func (tx *Tx) DeleteBySql(query string, value ...interface{}) *DeleteStmt {
b := DeleteBySql(query, value...)
b.runner = tx
b.EventReceiver = tx
b.EventReceiver = tx.EventReceiver
b.Dialect = tx.Dialect
return b
}
Expand Down
18 changes: 18 additions & 0 deletions event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dbr

import (
"context"
)

type testTraceReceiver struct {
NullEventReceiver
started []struct{ eventName, query string }
errored, finished int
}

func (t *testTraceReceiver) SpanStart(ctx context.Context, eventName, query string) context.Context {
t.started = append(t.started, struct{ eventName, query string }{eventName, query})
return ctx
}
func (t *testTraceReceiver) SpanError(ctx context.Context, err error) { t.errored++ }
func (t *testTraceReceiver) SpanFinish(ctx context.Context) { t.finished++ }
8 changes: 4 additions & 4 deletions insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func InsertInto(table string) *InsertStmt {
func (sess *Session) InsertInto(table string) *InsertStmt {
b := InsertInto(table)
b.runner = sess
b.EventReceiver = sess
b.EventReceiver = sess.EventReceiver
b.Dialect = sess.Dialect
return b
}
Expand All @@ -97,7 +97,7 @@ func (sess *Session) InsertInto(table string) *InsertStmt {
func (tx *Tx) InsertInto(table string) *InsertStmt {
b := InsertInto(table)
b.runner = tx
b.EventReceiver = tx
b.EventReceiver = tx.EventReceiver
b.Dialect = tx.Dialect
return b
}
Expand All @@ -116,7 +116,7 @@ func InsertBySql(query string, value ...interface{}) *InsertStmt {
func (sess *Session) InsertBySql(query string, value ...interface{}) *InsertStmt {
b := InsertBySql(query, value...)
b.runner = sess
b.EventReceiver = sess
b.EventReceiver = sess.EventReceiver
b.Dialect = sess.Dialect
return b
}
Expand All @@ -125,7 +125,7 @@ func (sess *Session) InsertBySql(query string, value ...interface{}) *InsertStmt
func (tx *Tx) InsertBySql(query string, value ...interface{}) *InsertStmt {
b := InsertBySql(query, value...)
b.runner = tx
b.EventReceiver = tx
b.EventReceiver = tx.EventReceiver
b.Dialect = tx.Dialect
return b
}
Expand Down
7 changes: 7 additions & 0 deletions insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ func TestPostgresReturning(t *testing.T) {
Returning("id").Load(&person.Id)
require.NoError(t, err)
require.True(t, person.Id > 0)
require.Len(t, sess.EventReceiver.(*testTraceReceiver).started, 1)
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].eventName, "dbr.select")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "INSERT")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "dbr_people")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "name")
require.Equal(t, 1, sess.EventReceiver.(*testTraceReceiver).finished)
require.Equal(t, 0, sess.EventReceiver.(*testTraceReceiver).errored)
}

func BenchmarkInsertValuesSQL(b *testing.B) {
Expand Down
8 changes: 4 additions & 4 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func prepareSelect(a []string) []interface{} {
func (sess *Session) Select(column ...string) *SelectStmt {
b := Select(prepareSelect(column)...)
b.runner = sess
b.EventReceiver = sess
b.EventReceiver = sess.EventReceiver
b.Dialect = sess.Dialect
return b
}
Expand All @@ -164,7 +164,7 @@ func (sess *Session) Select(column ...string) *SelectStmt {
func (tx *Tx) Select(column ...string) *SelectStmt {
b := Select(prepareSelect(column)...)
b.runner = tx
b.EventReceiver = tx
b.EventReceiver = tx.EventReceiver
b.Dialect = tx.Dialect
return b
}
Expand All @@ -185,7 +185,7 @@ func SelectBySql(query string, value ...interface{}) *SelectStmt {
func (sess *Session) SelectBySql(query string, value ...interface{}) *SelectStmt {
b := SelectBySql(query, value...)
b.runner = sess
b.EventReceiver = sess
b.EventReceiver = sess.EventReceiver
b.Dialect = sess.Dialect
return b
}
Expand All @@ -194,7 +194,7 @@ func (sess *Session) SelectBySql(query string, value ...interface{}) *SelectStmt
func (tx *Tx) SelectBySql(query string, value ...interface{}) *SelectStmt {
b := SelectBySql(query, value...)
b.runner = tx
b.EventReceiver = tx
b.EventReceiver = tx.EventReceiver
b.Dialect = tx.Dialect
return b
}
Expand Down
2 changes: 1 addition & 1 deletion transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (sess *Session) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, err
sess.Event("dbr.begin")

return &Tx{
EventReceiver: sess,
EventReceiver: sess.EventReceiver,
Dialect: sess.Dialect,
Tx: tx,
Timeout: sess.GetTimeout(),
Expand Down
8 changes: 8 additions & 0 deletions transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ func TestTransactionCommit(t *testing.T) {

result, err := tx.InsertInto("dbr_people").Columns("id", "name", "email").Values(id, "Barack", "[email protected]").Exec()
require.NoError(t, err)
require.Len(t, sess.EventReceiver.(*testTraceReceiver).started, 1)
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].eventName, "dbr.exec")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "INSERT")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "dbr_people")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "name")
require.Equal(t, 1, sess.EventReceiver.(*testTraceReceiver).finished)
require.Equal(t, 0, sess.EventReceiver.(*testTraceReceiver).errored)

rowsAffected, err := result.RowsAffected()
require.NoError(t, err)
Expand All @@ -29,6 +36,7 @@ func TestTransactionCommit(t *testing.T) {
var person dbrPerson
err = tx.Select("*").From("dbr_people").Where(Eq("id", id)).LoadOne(&person)
require.Error(t, err)
require.Equal(t, 1, sess.EventReceiver.(*testTraceReceiver).errored)
}
}

Expand Down
8 changes: 4 additions & 4 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Update(table string) *UpdateStmt {
func (sess *Session) Update(table string) *UpdateStmt {
b := Update(table)
b.runner = sess
b.EventReceiver = sess
b.EventReceiver = sess.EventReceiver
b.Dialect = sess.Dialect
return b
}
Expand All @@ -90,7 +90,7 @@ func (sess *Session) Update(table string) *UpdateStmt {
func (tx *Tx) Update(table string) *UpdateStmt {
b := Update(table)
b.runner = tx
b.EventReceiver = tx
b.EventReceiver = tx.EventReceiver
b.Dialect = tx.Dialect
return b
}
Expand All @@ -111,7 +111,7 @@ func UpdateBySql(query string, value ...interface{}) *UpdateStmt {
func (sess *Session) UpdateBySql(query string, value ...interface{}) *UpdateStmt {
b := UpdateBySql(query, value...)
b.runner = sess
b.EventReceiver = sess
b.EventReceiver = sess.EventReceiver
b.Dialect = sess.Dialect
return b
}
Expand All @@ -120,7 +120,7 @@ func (sess *Session) UpdateBySql(query string, value ...interface{}) *UpdateStmt
func (tx *Tx) UpdateBySql(query string, value ...interface{}) *UpdateStmt {
b := UpdateBySql(query, value...)
b.runner = tx
b.EventReceiver = tx
b.EventReceiver = tx.EventReceiver
b.Dialect = tx.Dialect
return b
}
Expand Down

0 comments on commit 042fe86

Please sign in to comment.