Skip to content

Commit 56cd727

Browse files
committed
Relaxed command event assertions of requestId. Since the requestId is based on a static AtomicInteger, it may be changed by other threads.
Therefore it's not reliable to assert anything that's based on it. Instead, simply assert that the requestId of a CommandSucceededEvent or CommandFailedEvent matches the requestId of the previous CommandStartedEvent. Changed TestCommandListener#eventsWereDelivered to use assertions directly instead of returning a boolean
1 parent a425c7e commit 56cd727

File tree

2 files changed

+48
-72
lines changed

2 files changed

+48
-72
lines changed

driver-core/src/test/functional/com/mongodb/connection/TestCommandListener.java

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@
3737
import java.util.List;
3838
import java.util.concurrent.TimeUnit;
3939

40+
import static org.junit.Assert.assertEquals;
41+
import static org.junit.Assert.assertNull;
42+
import static org.junit.Assert.assertTrue;
43+
4044
public class TestCommandListener implements CommandListener {
4145
private final List<CommandEvent> events = new ArrayList<CommandEvent>();
42-
private int firstRequestId = RequestMessage.getCurrentGlobalId();
4346
private static final CodecRegistry CODEC_REGISTRY_HACK;
4447

4548
static {
@@ -59,7 +62,6 @@ public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
5962

6063
public void reset() {
6164
events.clear();
62-
firstRequestId = RequestMessage.getCurrentGlobalId();
6365
}
6466

6567
public List<CommandEvent> getEvents() {
@@ -92,79 +94,54 @@ public void commandFailed(final CommandFailedEvent event) {
9294
events.add(event);
9395
}
9496

95-
public boolean eventsWereDelivered(final List<CommandEvent> expectedEvents) {
96-
if (expectedEvents.size() != events.size()) {
97-
return false;
98-
}
99-
int currentlyExpectedRequestId = firstRequestId;
97+
public void eventsWereDelivered(final List<CommandEvent> expectedEvents) {
98+
assertEquals(expectedEvents.size(), events.size());
99+
100+
int currentlyExpectedRequestId = 0;
100101
for (int i = 0; i < events.size(); i++) {
101102
CommandEvent actual = events.get(i);
102103
CommandEvent expected = expectedEvents.get(i);
103-
if (!actual.getClass().equals(expected.getClass())) {
104-
return false;
105-
}
106104

107-
if (actual.getRequestId() != currentlyExpectedRequestId) {
108-
return false;
109-
}
105+
assertEquals(expected.getClass(), actual.getClass());
110106

111-
if (!(actual instanceof CommandStartedEvent)) {
112-
currentlyExpectedRequestId++;
107+
if (actual instanceof CommandStartedEvent) {
108+
currentlyExpectedRequestId = actual.getRequestId();
109+
} else {
110+
assertEquals(currentlyExpectedRequestId, actual.getRequestId());
113111
}
114112

115-
if (!actual.getConnectionDescription().equals(expected.getConnectionDescription())) {
116-
return false;
117-
}
113+
assertEquals(expected.getConnectionDescription(), actual.getConnectionDescription());
118114

119-
if (!actual.getCommandName().equals(expected.getCommandName())) {
120-
return false;
121-
}
115+
assertEquals(expected.getCommandName(), actual.getCommandName());
122116

123117
if (actual.getClass().equals(CommandStartedEvent.class)) {
124-
if (!isEquivalent((CommandStartedEvent) actual, (CommandStartedEvent) expected)) {
125-
return false;
126-
}
118+
assertEquivalence((CommandStartedEvent) actual, (CommandStartedEvent) expected);
127119
} else if (actual.getClass().equals(CommandSucceededEvent.class)) {
128-
if (!isEquivalent((CommandSucceededEvent) actual, (CommandSucceededEvent) expected)) {
129-
return false;
130-
}
120+
assertEquivalence((CommandSucceededEvent) actual, (CommandSucceededEvent) expected);
131121
} else if (actual.getClass().equals(CommandFailedEvent.class)) {
132-
if (!isEquivalent((CommandFailedEvent) actual, (CommandFailedEvent) expected)) {
133-
return false;
134-
}
122+
assertEquivalence((CommandFailedEvent) actual, (CommandFailedEvent) expected);
135123
} else {
136124
throw new UnsupportedOperationException("Unsupported event type: " + actual.getClass());
137125
}
138126
}
139-
140-
return true;
141127
}
142128

143-
private boolean isEquivalent(final CommandFailedEvent actual, final CommandFailedEvent expected) {
144-
if (!actual.getThrowable().equals(expected.getThrowable())) {
145-
return false;
146-
}
147-
return true;
129+
private void assertEquivalence(final CommandFailedEvent actual, final CommandFailedEvent expected) {
130+
assertEquals(expected.getThrowable(), actual.getThrowable());
148131
}
149132

150-
private boolean isEquivalent(final CommandSucceededEvent actual, final CommandSucceededEvent expected) {
133+
private void assertEquivalence(final CommandSucceededEvent actual, final CommandSucceededEvent expected) {
151134
if (actual.getResponse() == null) {
152-
return expected.getResponse() == null;
135+
assertNull(expected.getResponse());
136+
} else {
137+
// ignore extra elements in the actual response
138+
assertTrue("Expected response contains elements not in the actual response",
139+
actual.getResponse().entrySet().containsAll(expected.getResponse().entrySet()));
153140
}
154-
// ignore extra elements in the actual response
155-
if (!actual.getResponse().entrySet().containsAll(expected.getResponse().entrySet())) {
156-
return false;
157-
}
158-
return true;
159141
}
160142

161-
private boolean isEquivalent(final CommandStartedEvent actual, final CommandStartedEvent expected) {
162-
if (!actual.getDatabaseName().equals(expected.getDatabaseName())) {
163-
return false;
164-
}
165-
if (!actual.getCommand().equals(expected.getCommand())) {
166-
return false;
167-
}
168-
return true;
143+
private void assertEquivalence(final CommandStartedEvent actual, final CommandStartedEvent expected) {
144+
assertEquals(expected.getDatabaseName(), actual.getDatabaseName());
145+
assertEquals(expected.getCommand(), actual.getCommand());
169146
}
170147
}

driver-core/src/test/functional/com/mongodb/connection/WriteProtocolCommandEventSpecification.groovy

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,31 +110,30 @@ class WriteProtocolCommandEventSpecification extends OperationFunctionalSpecific
110110
def commandListener = new TestCommandListener()
111111
protocol.commandListener = commandListener
112112

113-
def expectedEvents = [
113+
when:
114+
protocol.execute(connection)
115+
116+
then:
117+
commandListener.eventsWereDelivered([
114118
new CommandStartedEvent(1, connection.getDescription(), getDatabaseName(), 'insert',
115-
new BsonDocument('insert', new BsonString(getCollectionName()))
116-
.append('ordered', BsonBoolean.TRUE)
117-
.append('writeConcern',
118-
new BsonDocument('w', new BsonInt32(0)))
119-
.append('documents',
120-
new BsonArray([documentOne, documentTwo,
121-
documentThree]))),
119+
new BsonDocument('insert', new BsonString(getCollectionName()))
120+
.append('ordered', BsonBoolean.TRUE)
121+
.append('writeConcern',
122+
new BsonDocument('w', new BsonInt32(0)))
123+
.append('documents',
124+
new BsonArray([documentOne, documentTwo,
125+
documentThree]))),
122126
new CommandSucceededEvent(1, connection.getDescription(), 'insert',
123-
new BsonDocument('ok', new BsonInt32(1)), 0),
127+
new BsonDocument('ok', new BsonInt32(1)), 0),
124128
new CommandStartedEvent(1, connection.getDescription(), getDatabaseName(), 'insert',
125-
new BsonDocument('insert', new BsonString(getCollectionName()))
126-
.append('ordered', BsonBoolean.TRUE)
127-
.append('writeConcern',
128-
new BsonDocument('w', new BsonInt32(0)))
129-
.append('documents', new BsonArray([documentFour]))),
129+
new BsonDocument('insert', new BsonString(getCollectionName()))
130+
.append('ordered', BsonBoolean.TRUE)
131+
.append('writeConcern',
132+
new BsonDocument('w', new BsonInt32(0)))
133+
.append('documents', new BsonArray([documentFour]))),
130134
new CommandSucceededEvent(1, connection.getDescription(), 'insert',
131-
new BsonDocument('ok', new BsonInt32(1)), 0)]
135+
new BsonDocument('ok', new BsonInt32(1)), 0)])
132136

133-
when:
134-
protocol.execute(connection)
135-
136-
then:
137-
commandListener.eventsWereDelivered(expectedEvents)
138137
cleanup:
139138
// force acknowledgement
140139
new CommandProtocol(getDatabaseName(), new BsonDocument('drop', new BsonString(getCollectionName())),

0 commit comments

Comments
 (0)