@@ -27,6 +27,8 @@ import com.mongodb.ServerAddress
27
27
import com.mongodb.async.FutureResultCallback
28
28
import com.mongodb.async.SingleResultCallback
29
29
import com.mongodb.event.ConnectionListener
30
+ import com.mongodb.event.ConnectionMessageReceivedEvent
31
+ import com.mongodb.event.ConnectionMessagesSentEvent
30
32
import org.bson.BsonBinaryWriter
31
33
import org.bson.BsonDocument
32
34
import org.bson.BsonInt32
@@ -50,6 +52,7 @@ import java.util.concurrent.ExecutorService
50
52
import java.util.concurrent.Executors
51
53
52
54
import static MongoNamespace.COMMAND_COLLECTION_NAME
55
+ import static com.mongodb.CustomMatchers.compare
53
56
import static com.mongodb.connection.ConnectionDescription.getDefaultMaxMessageSize
54
57
import static com.mongodb.connection.ConnectionDescription.getDefaultMaxWriteBatchSize
55
58
import static com.mongodb.connection.ServerDescription.getDefaultMaxDocumentSize
@@ -61,7 +64,9 @@ class InternalStreamConnectionSpecification extends Specification {
61
64
62
65
def helper = new StreamHelper ()
63
66
def serverAddress = new ServerAddress ()
64
- def connectionDescription = new ConnectionDescription (new ConnectionId (SERVER_ID , 1 , 1 ), new ServerVersion (), ServerType . STANDALONE ,
67
+ def connectionId = new ConnectionId (SERVER_ID , 1 , 1 )
68
+
69
+ def connectionDescription = new ConnectionDescription (connectionId, new ServerVersion (), ServerType . STANDALONE ,
65
70
getDefaultMaxWriteBatchSize(), getDefaultMaxDocumentSize(),
66
71
getDefaultMaxMessageSize())
67
72
def stream = Mock (Stream ) {
@@ -106,23 +111,31 @@ class InternalStreamConnectionSpecification extends Specification {
106
111
given :
107
112
def connection = getOpenedConnection()
108
113
def (buffers1, messageId1) = helper. isMaster()
109
- stream. read(_) >>> helper. read([messageId1])
114
+ def messageSize = helper. remaining(buffers1)
115
+ stream. write(_) >> {
116
+ helper. write(buffers1)
117
+ }
110
118
when :
111
119
connection. sendMessage(buffers1, messageId1)
112
120
121
+
113
122
then :
114
- 1 * listener. messagesSent(_)
123
+ 1 * listener. messagesSent {
124
+ compare(new ConnectionMessagesSentEvent (connectionId, messageId1, messageSize), it)
125
+ }
115
126
}
116
127
117
128
@Category (Async )
118
129
@IgnoreIf ({ javaVersion < 1.7 })
119
130
def ' should fire message sent event asynchronously' () {
120
- stream. writeAsync(_, _) >> { List<ByteBuf > buffers , AsyncCompletionHandler<Void > callback ->
121
- callback. completed(null )
122
- }
123
131
def (buffers1, messageId1) = helper. isMaster()
132
+ def messageSize = helper. remaining(buffers1)
124
133
def connection = getOpenedConnection()
125
134
def latch = new CountDownLatch (1 );
135
+ stream. writeAsync(_, _) >> { List<ByteBuf > buffers , AsyncCompletionHandler<Void > callback ->
136
+ helper. write(buffers1)
137
+ callback. completed(null )
138
+ }
126
139
127
140
when :
128
141
connection. sendMessageAsync(buffers1, messageId1, new SingleResultCallback<Void > () {
@@ -134,7 +147,9 @@ class InternalStreamConnectionSpecification extends Specification {
134
147
latch. await()
135
148
136
149
then :
137
- 1 * listener. messagesSent(_)
150
+ 1 * listener. messagesSent {
151
+ compare(new ConnectionMessagesSentEvent (connectionId, messageId1, messageSize), it)
152
+ }
138
153
}
139
154
140
155
def ' should fire message received event' () {
@@ -147,7 +162,9 @@ class InternalStreamConnectionSpecification extends Specification {
147
162
connection. receiveMessage(messageId1)
148
163
149
164
then :
150
- 1 * listener. messageReceived(_)
165
+ 1 * listener. messageReceived {
166
+ compare(new ConnectionMessageReceivedEvent (connectionId, messageId1, 110 ), it)
167
+ }
151
168
}
152
169
153
170
@Category (Async )
@@ -175,7 +192,9 @@ class InternalStreamConnectionSpecification extends Specification {
175
192
latch. await()
176
193
177
194
then :
178
- 1 * listener. messageReceived(_)
195
+ 1 * listener. messageReceived {
196
+ compare(new ConnectionMessageReceivedEvent (connectionId, messageId1, 110 ), it)
197
+ }
179
198
}
180
199
181
200
def ' should change the connection description when opened' () {
@@ -653,6 +672,21 @@ class InternalStreamConnectionSpecification extends Specification {
653
672
class StreamHelper {
654
673
int nextMessageId = 900000 // Generates a message then adds one to the id
655
674
675
+ def remaining (List<ByteBuf > buffers ) {
676
+ int remaining = 0
677
+ buffers. each {
678
+ remaining + = it. remaining()
679
+ }
680
+ remaining
681
+ }
682
+
683
+ def write (List<ByteBuf > buffers ) {
684
+ buffers. each {
685
+ it. get(new byte [it. remaining()])
686
+ }
687
+ }
688
+
689
+
656
690
def read (List<Integer > messageIds ) {
657
691
read(messageIds, true )
658
692
}
@@ -706,10 +740,10 @@ class InternalStreamConnectionSpecification extends Specification {
706
740
def command = new CommandMessage (new MongoNamespace (' admin' , COMMAND_COLLECTION_NAME ). getFullName(),
707
741
new BsonDocument (' ismaster' , new BsonInt32 (1 )),
708
742
false , MessageSettings . builder(). build());
709
- OutputBuffer buffer = new BasicOutputBuffer ();
710
- command. encode(buffer );
743
+ OutputBuffer outputBuffer = new BasicOutputBuffer ();
744
+ command. encode(outputBuffer );
711
745
nextMessageId++
712
- [buffer . byteBuffers, nextMessageId]
746
+ [outputBuffer . byteBuffers, nextMessageId]
713
747
}
714
748
715
749
def isMasterAsync () {
0 commit comments