forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_connection.go
66 lines (57 loc) · 1.39 KB
/
channel_connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package internal
import (
"context"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/core/wiremessage"
"testing"
)
// Implements the connection.Connection interface by reading and writing wire messages
// to a channel
type ChannelConn struct {
T *testing.T
WriteErr error
Written chan wiremessage.WireMessage
ReadResp chan wiremessage.WireMessage
ReadErr chan error
}
func (c *ChannelConn) WriteWireMessage(ctx context.Context, wm wiremessage.WireMessage) error {
select {
case c.Written <- wm:
default:
c.T.Error("could not write wiremessage to written channel")
}
return c.WriteErr
}
func (c *ChannelConn) ReadWireMessage(ctx context.Context) (wiremessage.WireMessage, error) {
var wm wiremessage.WireMessage
var err error
select {
case wm = <-c.ReadResp:
case err = <-c.ReadErr:
case <-ctx.Done():
}
return wm, err
}
func (c *ChannelConn) Close() error {
return nil
}
func (c *ChannelConn) Expired() bool {
return false
}
func (c *ChannelConn) Alive() bool {
return true
}
func (c *ChannelConn) ID() string {
return "faked"
}
// Create a OP_REPLY wiremessage from a BSON document
func MakeReply(t *testing.T, doc *bson.Document) wiremessage.WireMessage {
rdr, err := doc.MarshalBSON()
if err != nil {
t.Fatalf("Could not create document: %v", err)
}
return wiremessage.Reply{
NumberReturned: 1,
Documents: []bson.Reader{rdr},
}
}