Skip to content

Commit 365213f

Browse files
committed
JAVA-2383: Allow elapsed time of zero nanoseconds for VMs that have imprecise nanosecond clocks
1 parent a191980 commit 365213f

File tree

6 files changed

+105
-8
lines changed

6 files changed

+105
-8
lines changed

driver-core/src/main/com/mongodb/event/CommandFailedEvent.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.concurrent.TimeUnit;
2020

21+
import static com.mongodb.assertions.Assertions.isTrueArgument;
22+
2123
/**
2224
* An event representing the failure of a MongoDB database command.
2325
*
@@ -33,12 +35,13 @@ public final class CommandFailedEvent extends CommandEvent {
3335
* @param requestId the requestId
3436
* @param connectionDescription the connection description
3537
* @param commandName the command name
36-
* @param elapsedTimeNanos the elapsed time in nanoseconds for the operation to complete
38+
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
3739
* @param throwable the throwable cause of the failure
3840
*/
3941
public CommandFailedEvent(final int requestId, final ConnectionDescription connectionDescription, final String commandName,
4042
final long elapsedTimeNanos, final Throwable throwable) {
4143
super(requestId, connectionDescription, commandName);
44+
isTrueArgument("elapsed time is not negative", elapsedTimeNanos >= 0);
4245
this.elapsedTimeNanos = elapsedTimeNanos;
4346
this.throwable = throwable;
4447
}

driver-core/src/main/com/mongodb/event/CommandSucceededEvent.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import java.util.concurrent.TimeUnit;
2121

22+
import static com.mongodb.assertions.Assertions.isTrueArgument;
23+
2224
/**
2325
* An event representing the completion of a MongoDB database command.
2426
*
@@ -34,12 +36,13 @@ public final class CommandSucceededEvent extends CommandEvent {
3436
* @param connectionDescription the connection description
3537
* @param commandName the command name
3638
* @param response the command response
37-
* @param elapsedTimeNanos the elapsed time in nanoseconds for the operation to complete
39+
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds for the operation to complete
3840
*/
3941
public CommandSucceededEvent(final int requestId, final ConnectionDescription connectionDescription,
4042
final String commandName, final BsonDocument response, final long elapsedTimeNanos) {
4143
super(requestId, connectionDescription, commandName);
4244
this.response = response;
45+
isTrueArgument("elapsed time is not negative", elapsedTimeNanos >= 0);
4346
this.elapsedTimeNanos = elapsedTimeNanos;
4447
}
4548

driver-core/src/main/com/mongodb/event/ServerHeartbeatFailedEvent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import java.util.concurrent.TimeUnit;
2323

24-
import static com.mongodb.assertions.Assertions.isTrue;
24+
import static com.mongodb.assertions.Assertions.isTrueArgument;
2525
import static com.mongodb.assertions.Assertions.notNull;
2626

2727
/**
@@ -38,12 +38,12 @@ public final class ServerHeartbeatFailedEvent {
3838
* Construct an instance.
3939
*
4040
* @param connectionId the non-null connectionId
41-
* @param elapsedTimeNanos the positive elapsed time in nanoseconds
41+
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds
4242
* @param throwable the non-null exception that caused the failure
4343
*/
4444
public ServerHeartbeatFailedEvent(final ConnectionId connectionId, final long elapsedTimeNanos, final Throwable throwable) {
4545
this.connectionId = notNull("connectionId", connectionId);
46-
isTrue("elapsed time is positive", elapsedTimeNanos > 0);
46+
isTrueArgument("elapsed time is not negative", elapsedTimeNanos >= 0);
4747
this.elapsedTimeNanos = elapsedTimeNanos;
4848
this.throwable = notNull("throwable", throwable);
4949
}

driver-core/src/main/com/mongodb/event/ServerHeartbeatSucceededEvent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import java.util.concurrent.TimeUnit;
2323

24-
import static com.mongodb.assertions.Assertions.isTrue;
24+
import static com.mongodb.assertions.Assertions.isTrueArgument;
2525
import static com.mongodb.assertions.Assertions.notNull;
2626

2727
/**
@@ -39,12 +39,12 @@ public final class ServerHeartbeatSucceededEvent {
3939
*
4040
* @param connectionId the non-null connectionId
4141
* @param reply the non-null reply to an isMaster command
42-
* @param elapsedTimeNanos the positive elapsted time in nanoseconds
42+
* @param elapsedTimeNanos the non-negative elapsed time in nanoseconds
4343
*/
4444
public ServerHeartbeatSucceededEvent(final ConnectionId connectionId, final BsonDocument reply, final long elapsedTimeNanos) {
4545
this.connectionId = notNull("connectionId", connectionId);
4646
this.reply = notNull("reply", reply);
47-
isTrue("elapsed time is positive", elapsedTimeNanos > 0);
47+
isTrueArgument("elapsed time is not negative", elapsedTimeNanos >= 0);
4848
this.elapsedTimeNanos = elapsedTimeNanos;
4949
}
5050

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2016 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.mongodb.event
19+
20+
import com.mongodb.ServerAddress
21+
import com.mongodb.connection.ClusterId
22+
import com.mongodb.connection.ConnectionDescription
23+
import com.mongodb.connection.ServerId
24+
import org.bson.BsonDocument
25+
import org.bson.BsonInt32
26+
import spock.lang.Specification
27+
28+
class CommandEventSpecification extends Specification {
29+
def 'should fail if elapsed time is negative'() {
30+
when:
31+
new CommandSucceededEvent(1, new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress())), 'ping',
32+
new BsonDocument('ok', new BsonInt32(1)), -1)
33+
34+
then:
35+
def e = thrown(IllegalArgumentException)
36+
e.getMessage() == 'state should be: elapsed time is not negative'
37+
38+
when:
39+
new CommandFailedEvent(1, new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress())), 'ping', -1,
40+
new Throwable())
41+
42+
then:
43+
e = thrown(IllegalArgumentException)
44+
e.getMessage() == 'state should be: elapsed time is not negative'
45+
}
46+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2016 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.mongodb.event
19+
20+
import com.mongodb.ServerAddress
21+
import com.mongodb.connection.ClusterId
22+
import com.mongodb.connection.ConnectionId
23+
import com.mongodb.connection.ServerId
24+
import org.bson.BsonDocument
25+
import org.bson.BsonInt32
26+
import spock.lang.Specification
27+
28+
class ServerHeartbeatEventSpecification extends Specification {
29+
def 'should fail if elapsed time is negative'() {
30+
when:
31+
new ServerHeartbeatSucceededEvent(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())),
32+
new BsonDocument('ok', new BsonInt32(1)), -1)
33+
34+
then:
35+
def e = thrown(IllegalArgumentException)
36+
e.getMessage() == 'state should be: elapsed time is not negative'
37+
38+
when:
39+
new ServerHeartbeatFailedEvent(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())), -1, new Throwable())
40+
41+
then:
42+
e = thrown(IllegalArgumentException)
43+
e.getMessage() == 'state should be: elapsed time is not negative'
44+
}
45+
}

0 commit comments

Comments
 (0)