chore: refactor (#1814)
This commit is contained in:
@@ -473,9 +473,10 @@ func (p keepablePromise) keep(err error) error {
|
||||
func acceptable(err error) bool {
|
||||
return err == nil || err == mongo.ErrNoDocuments || err == mongo.ErrNilValue ||
|
||||
err == mongo.ErrNilDocument || err == mongo.ErrNilCursor || err == mongo.ErrEmptySlice ||
|
||||
// session err
|
||||
err == session.ErrSessionEnded || err == session.ErrNoTransactStarted || err == session.ErrTransactInProgress ||
|
||||
err == session.ErrAbortAfterCommit || err == session.ErrAbortTwice || err == session.ErrCommitAfterAbort ||
|
||||
// session errors
|
||||
err == session.ErrSessionEnded || err == session.ErrNoTransactStarted ||
|
||||
err == session.ErrTransactInProgress || err == session.ErrAbortAfterCommit ||
|
||||
err == session.ErrAbortTwice || err == session.ErrCommitAfterAbort ||
|
||||
err == session.ErrUnackWCUnsupported || err == session.ErrSnapshotTransaction
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ type (
|
||||
opts []Option
|
||||
}
|
||||
|
||||
wrapSession struct {
|
||||
wrappedSession struct {
|
||||
mongo.Session
|
||||
brk breaker.Breaker
|
||||
}
|
||||
@@ -74,7 +74,10 @@ func (m *Model) StartSession(opts ...*mopt.SessionOptions) (sess mongo.Session,
|
||||
return sessionErr
|
||||
}
|
||||
|
||||
sess = &wrapSession{Session: session, brk: m.brk}
|
||||
sess = &wrappedSession{
|
||||
Session: session,
|
||||
brk: m.brk,
|
||||
}
|
||||
|
||||
return nil
|
||||
}, acceptable)
|
||||
@@ -166,7 +169,7 @@ func (m *Model) FindOneAndUpdate(ctx context.Context, v, filter interface{}, upd
|
||||
return res.Decode(v)
|
||||
}
|
||||
|
||||
func (w *wrapSession) AbortTransaction(ctx context.Context) error {
|
||||
func (w *wrappedSession) AbortTransaction(ctx context.Context) error {
|
||||
ctx, span := startSpan(ctx)
|
||||
defer span.End()
|
||||
|
||||
@@ -175,7 +178,7 @@ func (w *wrapSession) AbortTransaction(ctx context.Context) error {
|
||||
}, acceptable)
|
||||
}
|
||||
|
||||
func (w *wrapSession) CommitTransaction(ctx context.Context) error {
|
||||
func (w *wrappedSession) CommitTransaction(ctx context.Context) error {
|
||||
ctx, span := startSpan(ctx)
|
||||
defer span.End()
|
||||
|
||||
@@ -184,7 +187,11 @@ func (w *wrapSession) CommitTransaction(ctx context.Context) error {
|
||||
}, acceptable)
|
||||
}
|
||||
|
||||
func (w *wrapSession) WithTransaction(ctx context.Context, fn func(sessCtx mongo.SessionContext) (interface{}, error), opts ...*mopt.TransactionOptions) (res interface{}, err error) {
|
||||
func (w *wrappedSession) WithTransaction(
|
||||
ctx context.Context,
|
||||
fn func(sessCtx mongo.SessionContext) (interface{}, error),
|
||||
opts ...*mopt.TransactionOptions,
|
||||
) (res interface{}, err error) {
|
||||
ctx, span := startSpan(ctx)
|
||||
defer span.End()
|
||||
|
||||
@@ -196,7 +203,7 @@ func (w *wrapSession) WithTransaction(ctx context.Context, fn func(sessCtx mongo
|
||||
return
|
||||
}
|
||||
|
||||
func (w *wrapSession) EndSession(ctx context.Context) {
|
||||
func (w *wrappedSession) EndSession(ctx context.Context) {
|
||||
ctx, span := startSpan(ctx)
|
||||
defer span.End()
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ func TestModel_StartSession(t *testing.T) {
|
||||
m := createModel(mt)
|
||||
sess, err := m.StartSession()
|
||||
assert.Nil(t, err)
|
||||
defer sess.EndSession(context.Background())
|
||||
|
||||
_, err = sess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
|
||||
_ = sessCtx.StartTransaction()
|
||||
@@ -26,10 +27,8 @@ func TestModel_StartSession(t *testing.T) {
|
||||
return nil, nil
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.NoError(t, sess.CommitTransaction(context.Background()))
|
||||
assert.Error(t, sess.AbortTransaction(context.Background()))
|
||||
sess.EndSession(context.Background())
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user