-
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
Changes from 8 commits
95c3c13
e67176f
56b727a
19be9b1
badac9d
6d24dfa
be8bc11
3d7b697
f4310cc
efd68a1
5799b7c
a5a13f8
cd4699d
b90ed6e
a680f72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// 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 ( | ||
"testing" | ||
|
||
"go.mongodb.org/mongo-driver/v2/bson" | ||
"go.mongodb.org/mongo-driver/v2/internal/assert" | ||
"go.mongodb.org/mongo-driver/v2/internal/require" | ||
) | ||
|
||
func TestOPMSGMockDeployment(t *testing.T) { | ||
var md *MockDeployment | ||
|
||
|
||
t.Run("NewMockDeployment", func(t *testing.T) { | ||
md = NewMockDeployment() | ||
require.NotNil(t, md, "unexpected error from NewMockDeployment") | ||
}) | ||
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)) | ||
}) | ||
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, 4, "expected 4 responses, got %v", len(md.conn.responses)) | ||
}) | ||
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)) | ||
}) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.