Skip to content

Commit 03adad0

Browse files
committed
Rename NewSessionWithID to NewSessionWithLSID and update docs and tests.
1 parent 9eb374b commit 03adad0

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

internal/integration/mongointernal_test.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
2121
)
2222

23-
func TestNewSessionWithID(t *testing.T) {
23+
func TestNewSessionWithLSID(t *testing.T) {
2424
mt := mtest.New(t)
2525

2626
mt.Run("can be used to pass a specific session ID to CRUD commands", func(mt *mtest.T) {
@@ -32,7 +32,7 @@ func TestNewSessionWithID(t *testing.T) {
3232
AppendBinary("id", 4, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}).
3333
Build())
3434

35-
sess := mongo.NewSessionWithID(mt.Client, sessionID)
35+
sess := mongo.NewSessionWithLSID(mt.Client, sessionID)
3636

3737
ctx := mongo.NewSessionContext(context.Background(), sess)
3838
_, err := mt.Coll.InsertOne(ctx, bson.D{{"foo", "bar"}})
@@ -52,22 +52,47 @@ func TestNewSessionWithID(t *testing.T) {
5252
mt.Parallel()
5353

5454
sessionID := bson.Raw(bsoncore.NewDocumentBuilder().
55-
AppendBinary("id", 4, []byte{}).
55+
AppendBinary("id", 4, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}).
5656
Build())
57-
sess := mongo.NewSessionWithID(mt.Client, sessionID)
57+
sess := mongo.NewSessionWithLSID(mt.Client, sessionID)
5858

5959
// Use a defer-recover block to catch the expected panic and assert that
6060
// the recovered error is not nil.
6161
defer func() {
6262
err := recover()
63-
assert.NotNil(mt, err, "expected panic error to not be nil")
63+
assert.NotNil(mt, err, "expected EndSession to panic")
6464
}()
6565

66+
// Expect this call to panic.
6667
sess.EndSession(context.Background())
6768

6869
// We expect that calling EndSession on a Session returned by
69-
// NewSessionWithID panics. This code will only be reached if EndSession
70+
// NewSessionWithLSID panics. This code will only be reached if EndSession
7071
// doesn't panic.
7172
t.Errorf("expected EndSession to panic")
7273
})
74+
75+
mt.Run("ClientSession.SetServer panics", func(mt *mtest.T) {
76+
mt.Parallel()
77+
78+
sessionID := bson.Raw(bsoncore.NewDocumentBuilder().
79+
AppendBinary("id", 4, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}).
80+
Build())
81+
sess := mongo.NewSessionWithLSID(mt.Client, sessionID)
82+
83+
// Use a defer-recover block to catch the expected panic and assert that
84+
// the recovered error is not nil.
85+
defer func() {
86+
err := recover()
87+
assert.NotNil(mt, err, "expected ClientSession.SetServer to panic")
88+
}()
89+
90+
// Expect this call to panic.
91+
sess.ClientSession().SetServer()
92+
93+
// We expect that calling ClientSession.SetServer on a Session returned
94+
// by NewSessionWithLSID panics. This code will only be reached if
95+
// ClientSession.SetServer doesn't panic.
96+
t.Errorf("expected ClientSession.SetServer to panic")
97+
})
7398
}

mongo/mongointernal.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ import (
1616
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/session"
1717
)
1818

19-
// NewSessionWithID returns a Session with the given sessionID document. The
19+
// NewSessionWithLSID returns a Session with the given sessionID document. The
2020
// sessionID is a BSON document with key "id" containing a 16-byte UUID (binary
2121
// subtype 4).
2222
//
23-
// Sessions returned by NewSessionWithID are never added to the driver's session
24-
// pool. Calling EndSession on a Session returned by NewSessionWithID will
25-
// panic.
23+
// Sessions returned by NewSessionWithLSID are never added to the driver's
24+
// session pool. Calling "EndSession" or "ClientSession.SetServer" on a Session
25+
// returned by NewSessionWithLSID will panic.
2626
//
27-
// NewSessionWithID is intended only for internal use and may be changed or
27+
// NewSessionWithLSID is intended only for internal use and may be changed or
2828
// removed at any time.
29-
func NewSessionWithID(client *Client, sessionID bson.Raw) *Session {
29+
func NewSessionWithLSID(client *Client, sessionID bson.Raw) *Session {
3030
return &Session{
3131
clientSession: &session.Client{
3232
Server: &session.Server{

0 commit comments

Comments
 (0)