-
Notifications
You must be signed in to change notification settings - Fork 918
GODRIVER-3241 Move mock OPMSG deployment to the experimental package #1831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
95c3c13
move mtest to experimental
joyjwang e67176f
fix fmt
joyjwang 56b727a
Revert "fix fmt"
joyjwang 19be9b1
Revert "move mtest to experimental"
joyjwang badac9d
rename and move
joyjwang 6d24dfa
Merge remote-tracking branch 'origin/master' into GODRIVER-3241
joyjwang be8bc11
test
joyjwang 3d7b697
unit testing
joyjwang f4310cc
test using client
joyjwang efd68a1
add comment
joyjwang 5799b7c
move to drivertest
joyjwang a5a13f8
Merge branch 'master' into GODRIVER-3241
joyjwang cd4699d
adding wireversion range
joyjwang b90ed6e
adding documentation to avoid infinite loop
joyjwang a680f72
Revert "adding documentation to avoid infinite loop"
joyjwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package mtest | ||
package integration | ||
|
||
import ( | ||
"context" | ||
|
@@ -122,60 +122,60 @@ func (*connection) Stale() bool { | |
return false | ||
} | ||
|
||
// mockDeployment wraps a connection and implements the driver.Deployment interface. | ||
type mockDeployment struct { | ||
// MockDeployment wraps a connection and implements the driver.Deployment interface. | ||
type MockDeployment struct { | ||
conn *connection | ||
updates chan description.Topology | ||
} | ||
|
||
var _ driver.Deployment = &mockDeployment{} | ||
var _ driver.Server = &mockDeployment{} | ||
var _ driver.Connector = &mockDeployment{} | ||
var _ driver.Disconnector = &mockDeployment{} | ||
var _ driver.Subscriber = &mockDeployment{} | ||
var _ driver.Deployment = &MockDeployment{} | ||
var _ driver.Server = &MockDeployment{} | ||
var _ driver.Connector = &MockDeployment{} | ||
var _ driver.Disconnector = &MockDeployment{} | ||
var _ driver.Subscriber = &MockDeployment{} | ||
|
||
// SelectServer implements the Deployment interface. This method does not use the | ||
// description.SelectedServer provided and instead returns itself. The Connections returned from the | ||
// Connection method have a no-op Close method. | ||
func (md *mockDeployment) SelectServer(context.Context, description.ServerSelector) (driver.Server, error) { | ||
func (md *MockDeployment) SelectServer(context.Context, description.ServerSelector) (driver.Server, error) { | ||
return md, nil | ||
} | ||
|
||
// GetServerSelectionTimeout returns zero as a server selection timeout is not | ||
// applicable for mock deployments. | ||
func (*mockDeployment) GetServerSelectionTimeout() time.Duration { | ||
func (*MockDeployment) GetServerSelectionTimeout() time.Duration { | ||
return 0 | ||
} | ||
|
||
// Kind implements the Deployment interface. It always returns description.TopologyKindSingle. | ||
func (md *mockDeployment) Kind() description.TopologyKind { | ||
func (md *MockDeployment) Kind() description.TopologyKind { | ||
return description.TopologyKindSingle | ||
} | ||
|
||
// Connection implements the driver.Server interface. | ||
func (md *mockDeployment) Connection(context.Context) (*mnet.Connection, error) { | ||
func (md *MockDeployment) Connection(context.Context) (*mnet.Connection, error) { | ||
return mnet.NewConnection(md.conn), nil | ||
} | ||
|
||
// RTTMonitor implements the driver.Server interface. | ||
func (md *mockDeployment) RTTMonitor() driver.RTTMonitor { | ||
func (md *MockDeployment) RTTMonitor() driver.RTTMonitor { | ||
return &csot.ZeroRTTMonitor{} | ||
} | ||
|
||
// Connect is a no-op method which implements the driver.Connector interface. | ||
func (md *mockDeployment) Connect() error { | ||
func (md *MockDeployment) Connect() error { | ||
return nil | ||
} | ||
|
||
// Disconnect is a no-op method which implements the driver.Disconnector interface { | ||
func (md *mockDeployment) Disconnect(context.Context) error { | ||
func (md *MockDeployment) Disconnect(context.Context) error { | ||
close(md.updates) | ||
return nil | ||
} | ||
|
||
// Subscribe returns a subscription from which new topology descriptions can be retrieved. | ||
// Subscribe implements the driver.Subscriber interface. | ||
func (md *mockDeployment) Subscribe() (*driver.Subscription, error) { | ||
func (md *MockDeployment) Subscribe() (*driver.Subscription, error) { | ||
if md.updates == nil { | ||
md.updates = make(chan description.Topology, 1) | ||
|
||
|
@@ -190,23 +190,23 @@ func (md *mockDeployment) Subscribe() (*driver.Subscription, error) { | |
} | ||
|
||
// Unsubscribe is a no-op method which implements the driver.Subscriber interface. | ||
func (md *mockDeployment) Unsubscribe(*driver.Subscription) error { | ||
func (md *MockDeployment) Unsubscribe(*driver.Subscription) error { | ||
return nil | ||
} | ||
|
||
// addResponses adds responses to this mock deployment. | ||
func (md *mockDeployment) addResponses(responses ...bson.D) { | ||
// AddResponses adds responses to this mock deployment. | ||
func (md *MockDeployment) AddResponses(responses ...bson.D) { | ||
md.conn.responses = append(md.conn.responses, responses...) | ||
} | ||
|
||
// clearResponses clears all remaining responses in this mock deployment. | ||
func (md *mockDeployment) clearResponses() { | ||
// ClearResponses clears all remaining responses in this mock deployment. | ||
func (md *MockDeployment) ClearResponses() { | ||
md.conn.responses = md.conn.responses[:0] | ||
} | ||
|
||
// newMockDeployment returns a mock driver.Deployment that responds with OP_MSG wire messages. | ||
func newMockDeployment(responses ...bson.D) *mockDeployment { | ||
return &mockDeployment{ | ||
// NewMockDeployment returns a mock driver.Deployment that responds with OP_MSG wire messages. | ||
func NewMockDeployment(responses ...bson.D) *MockDeployment { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joyjwang We should note in the comment that for most use cases it's better to test with an actual database. CC: @matthewdale |
||
return &MockDeployment{ | ||
conn: &connection{ | ||
responses: responses, | ||
}, | ||
|
This file contains hidden or 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,59 @@ | ||
// Copyright (C) MongoDB, Inc. 2017-present. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package integration | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"go.mongodb.org/mongo-driver/v2/bson" | ||
"go.mongodb.org/mongo-driver/v2/internal/assert" | ||
"go.mongodb.org/mongo-driver/v2/internal/require" | ||
"go.mongodb.org/mongo-driver/v2/mongo" | ||
"go.mongodb.org/mongo-driver/v2/mongo/options" | ||
) | ||
|
||
func TestOPMSGMockDeployment(t *testing.T) { | ||
md := NewMockDeployment() | ||
|
||
opts := options.Client() | ||
opts.Opts = append(opts.Opts, func(co *options.ClientOptions) error { | ||
co.Deployment = md | ||
|
||
return nil | ||
}) | ||
client, err := mongo.Connect(opts) | ||
|
||
t.Run("NewMockDeployment connect to client", func(t *testing.T) { | ||
require.NoError(t, err, "unexpected error from connect to mockdeployment") | ||
}) | ||
t.Run("AddResponses with one", func(t *testing.T) { | ||
res := bson.D{{"ok", 1}} | ||
md.AddResponses(res) | ||
assert.NotNil(t, md.conn.responses, "expected non-nil responses") | ||
assert.Len(t, md.conn.responses, 1, "expected 1 response, got %v", len(md.conn.responses)) | ||
err = client.Ping(context.Background(), nil) | ||
require.NoError(t, err) | ||
}) | ||
t.Run("AddResponses with multiple", func(t *testing.T) { | ||
res1 := bson.D{{"ok", 1}} | ||
res2 := bson.D{{"ok", 2}} | ||
res3 := bson.D{{"ok", 3}} | ||
md.AddResponses(res1, res2, res3) | ||
assert.NotNil(t, md.conn.responses, "expected non-nil responses") | ||
assert.Len(t, md.conn.responses, 3, "expected 3 responses, got %v", len(md.conn.responses)) | ||
err = client.Ping(context.Background(), nil) | ||
require.NoError(t, err) | ||
}) | ||
t.Run("ClearResponses", func(t *testing.T) { | ||
md.ClearResponses() | ||
assert.NotNil(t, md.conn.responses, "expected non-nil responses") | ||
assert.Len(t, md.conn.responses, 0, "expected 0 responses, got %v", len(md.conn.responses)) | ||
err = client.Ping(context.Background(), nil) | ||
require.Error(t, err, "expected Ping error, got nil") | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.