-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ability to pass TracingEventReceiver to dbr (#154)
* 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
Showing
9 changed files
with
57 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
|
@@ -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) { | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++ } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters