Skip to content

Commit dd0919b

Browse files
committed
Remove assertion of server behavior with negative limit that doesn't hold true for MongoDB 3.4
1 parent 68939d1 commit dd0919b

File tree

1 file changed

+54
-67
lines changed

1 file changed

+54
-67
lines changed

src/test/com/mongodb/DBCursorTest.java

+54-67
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,26 @@ public void testCountAndHint() {
115115

116116
c.createIndex(new BasicDBObject("i", 1));
117117

118-
assertEquals(1, c.find(new BasicDBObject("i", 1)).hint( "_id_" ).count());
119-
assertEquals(2, c.find().hint( "_id_" ).count());
118+
assertEquals(1, c.find(new BasicDBObject("i", 1)).hint("_id_").count());
119+
assertEquals(2, c.find().hint("_id_").count());
120120

121121
if (serverIsAtLeastVersion(2.6)) {
122122
try {
123-
c.find(new BasicDBObject("i", 1)).hint( "BAD HINT" ).count();
123+
c.find(new BasicDBObject("i", 1)).hint("BAD HINT").count();
124124
fail("Show have thrown");
125125
} catch (MongoException e) {
126126
// good
127127
}
128128
} else {
129-
assertEquals(1, c.find(new BasicDBObject("i", 1)).hint( "BAD HINT" ).count());
129+
assertEquals(1, c.find(new BasicDBObject("i", 1)).hint("BAD HINT").count());
130130
}
131131

132132
c.createIndex(new BasicDBObject("x", 1), new BasicDBObject("sparse", true));
133133

134134
if (serverIsAtLeastVersion(2.6)) {
135-
assertEquals(0, c.find(new BasicDBObject("i", 1)).hint( "x_1" ).count());
135+
assertEquals(0, c.find(new BasicDBObject("i", 1)).hint("x_1").count());
136136
} else {
137-
assertEquals(1, c.find(new BasicDBObject("i", 1)).hint( "x_1" ).count());
137+
assertEquals(1, c.find(new BasicDBObject("i", 1)).hint("x_1").count());
138138
}
139139
if (serverIsAtLeastVersion(3.3)) {
140140
assertEquals(0, collection.find().hint("x_1").count()); // see https://jira.mongodb.org/browse/SERVER-22041
@@ -225,8 +225,8 @@ public void testTailable() {
225225
DBObject secondDBObject = new BasicDBObject("x", 2);
226226

227227
final DBCursor cur = c.find()
228-
.sort(new BasicDBObject("$natural", 1))
229-
.addOption(Bytes.QUERYOPTION_TAILABLE);
228+
.sort(new BasicDBObject("$natural", 1))
229+
.addOption(Bytes.QUERYOPTION_TAILABLE);
230230
c.save(firstDBObject, WriteConcern.SAFE);
231231

232232
assertEquals(firstDBObject, cur.tryNext());
@@ -257,8 +257,8 @@ public void testTailableImplicitAwaitOnHasNext() throws ExecutionException, Time
257257
}
258258

259259
final DBCursor cur = c.find()
260-
.sort(new BasicDBObject("$natural", 1))
261-
.addOption(Bytes.QUERYOPTION_TAILABLE);
260+
.sort(new BasicDBObject("$natural", 1))
261+
.addOption(Bytes.QUERYOPTION_TAILABLE);
262262

263263
final CountDownLatch latch = new CountDownLatch(1);
264264
Callable<Integer> callable = new Callable<Integer>() {
@@ -300,8 +300,8 @@ public void testTailableImplicitAwaitOnNext() throws ExecutionException, Timeout
300300
}
301301

302302
final DBCursor cur = c.find()
303-
.sort(new BasicDBObject("$natural", 1))
304-
.addOption(Bytes.QUERYOPTION_TAILABLE);
303+
.sort(new BasicDBObject("$natural", 1))
304+
.addOption(Bytes.QUERYOPTION_TAILABLE);
305305

306306
final CountDownLatch latch = new CountDownLatch(1);
307307
Callable<Integer> callable = new Callable<Integer>() {
@@ -340,8 +340,8 @@ public void shouldSupportTryNextOnTailableCursors() {
340340

341341
c.save(new BasicDBObject("x", 1), WriteConcern.SAFE);
342342
DBCursor cur = c.find()
343-
.sort(new BasicDBObject("$natural", 1))
344-
.addOption(Bytes.QUERYOPTION_TAILABLE);
343+
.sort(new BasicDBObject("$natural", 1))
344+
.addOption(Bytes.QUERYOPTION_TAILABLE);
345345

346346
try {
347347
cur.tryNext();
@@ -350,67 +350,54 @@ public void shouldSupportTryNextOnTailableCursors() {
350350
}
351351
}
352352

353-
@Test(expected=IllegalArgumentException.class)
353+
@Test(expected = IllegalArgumentException.class)
354354
public void shouldThrowExceptionOnTryNextForNonTailableCursors() {
355355
DBCollection c = getDatabase().getCollection("tail1");
356356
c.drop();
357357
getDatabase().createCollection("tail1", new BasicDBObject("capped", true).append("size", 10000));
358358

359359
c.save(new BasicDBObject("x", 1), WriteConcern.SAFE);
360360
DBCursor cur = c.find()
361-
.sort(new BasicDBObject("$natural", 1))
362-
.addOption(Bytes.QUERYOPTION_AWAITDATA);
361+
.sort(new BasicDBObject("$natural", 1))
362+
.addOption(Bytes.QUERYOPTION_AWAITDATA);
363363

364364
cur.tryNext();
365365
}
366366

367367
@Test
368368
public void testBig() {
369-
DBCollection c = collection;
370-
String bigString;
371-
{
372-
StringBuilder buf = new StringBuilder(16000);
373-
for (int i = 0; i < 16000; i++) {
374-
buf.append("x");
375-
}
376-
bigString = buf.toString();
369+
StringBuilder buf = new StringBuilder(16000);
370+
for (int i = 0; i < 16000; i++) {
371+
buf.append("x");
377372
}
373+
String bigString = buf.toString();
378374

379375
int numToInsert = (15 * 1024 * 1024) / bigString.length();
376+
assert numToInsert > 800;
380377

381378
for (int i = 0; i < numToInsert; i++) {
382-
c.save(BasicDBObjectBuilder.start().add("x", i).add("s", bigString).get());
379+
collection.insert(new BasicDBObject("x", i).append("s", bigString));
383380
}
384381

385-
assert (800 < numToInsert);
386-
387-
assertEquals(numToInsert, c.find().count());
388-
assertEquals(numToInsert, c.find().toArray().size());
389-
assertEquals(numToInsert, c.find().limit(800).count());
390-
assertEquals(800, c.find().limit(800).toArray().size());
391-
392-
// negative limit works like negative batch size, for legacy reason
393-
int x = c.find().limit(-800).toArray().size();
394-
if (serverIsAtLeastVersion(3.3)) {
395-
assertEquals(800, x); // MongoDB 3.4 creates a 12MB OP_REPLY that fits all 800 documents
396-
} else {
397-
assertTrue(x < 800); // Previous versions cut off the OP_REPLY at 4MB
398-
}
382+
assertEquals(numToInsert, collection.find().count());
383+
assertEquals(numToInsert, collection.find().toArray().size());
384+
assertEquals(numToInsert, collection.find().limit(800).count());
385+
assertEquals(800, collection.find().limit(800).toArray().size());
399386

400-
DBCursor a = c.find();
387+
DBCursor a = collection.find();
401388
assertEquals(numToInsert, a.itcount());
402389

403-
DBCursor b = c.find().batchSize(10);
390+
DBCursor b = collection.find().batchSize(10);
404391
assertEquals(numToInsert, b.itcount());
405392
assertEquals(10, b.getSizes().get(0).intValue());
406393

407394
assertTrue(a.numGetMores() < b.numGetMores());
408395

409-
assertEquals(numToInsert, c.find().batchSize(2).itcount());
410-
assertEquals(numToInsert, c.find().batchSize(1).itcount());
396+
assertEquals(numToInsert, collection.find().batchSize(2).itcount());
397+
assertEquals(numToInsert, collection.find().batchSize(1).itcount());
411398

412-
assertEquals(numToInsert, _count(c.find(null, null).skip(0).batchSize(5)));
413-
assertEquals(5, _count(c.find(null, null).skip(0).batchSize(-5)));
399+
assertEquals(numToInsert, _count(collection.find(null, null).skip(0).batchSize(5)));
400+
assertEquals(5, _count(collection.find(null, null).skip(0).batchSize(-5)));
414401
}
415402

416403
@SuppressWarnings("unchecked")
@@ -680,7 +667,7 @@ public void testHasFinalizer() throws UnknownHostException {
680667
Mongo m;
681668
if (getMongoClientURI().getCredentials() != null) {
682669
m = new MongoClient(new ServerAddress(getMongoClientURI().getHosts().get(0)), asList(getMongoClientURI().getCredentials()),
683-
mongoOptions);
670+
mongoOptions);
684671
} else {
685672
m = new MongoClient(new ServerAddress(getMongoClientURI().getHosts().get(0)), mongoOptions);
686673
}
@@ -716,7 +703,7 @@ public void testMaxTimeForIterator() {
716703
public void testMaxTimeDuringGetMore() {
717704
assumeFalse(isSharded(getMongoClient()));
718705
checkServerVersion(2.5);
719-
for (int i=0; i < 20; i++) {
706+
for (int i = 0; i < 20; i++) {
720707
collection.insert(new BasicDBObject("x", 1));
721708
}
722709

@@ -727,7 +714,7 @@ public void testMaxTimeDuringGetMore() {
727714

728715
enableMaxTimeFailPoint();
729716
try {
730-
while(cursor.hasNext()) {
717+
while (cursor.hasNext()) {
731718
cursor.next();
732719
}
733720
fail("Show have thrown");
@@ -835,46 +822,46 @@ private void insertData() {
835822
public void testMaxScan() {
836823
insertData();
837824
countResults(new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
838-
.addSpecial("$maxScan", 4), 4);
825+
.addSpecial("$maxScan", 4), 4);
839826
countResults(new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary()).maxScan(4), 4);
840827
}
841828

842829
@Test
843830
public void testMax() {
844831
insertData();
845832
countResults(new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
846-
.addSpecial("$max", new BasicDBObject("x", 4)), 4);
833+
.addSpecial("$max", new BasicDBObject("x", 4)), 4);
847834
countResults(new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
848-
.max(new BasicDBObject("x", 4)), 4);
835+
.max(new BasicDBObject("x", 4)), 4);
849836
}
850837

851838
@Test
852839
public void testMin() {
853840
insertData();
854841
countResults(new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
855-
.addSpecial("$min", new BasicDBObject("x", 4)), 6);
842+
.addSpecial("$min", new BasicDBObject("x", 4)), 6);
856843
countResults(new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
857-
.min(new BasicDBObject("x", 4)), 6);
844+
.min(new BasicDBObject("x", 4)), 6);
858845
}
859846

860847
@Test
861848
public void testReturnKey() {
862849
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
863-
.addSpecial("$returnKey", true);
850+
.addSpecial("$returnKey", true);
864851
try {
865852
while (cursor.hasNext()) {
866853
Assert.assertNull(cursor.next()
867-
.get("_id"));
854+
.get("_id"));
868855
}
869856
} finally {
870857
cursor.close();
871858
}
872859
cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
873-
.returnKey();
860+
.returnKey();
874861
try {
875862
while (cursor.hasNext()) {
876863
Assert.assertNull(cursor.next()
877-
.get("_id"));
864+
.get("_id"));
878865
}
879866
} finally {
880867
cursor.close();
@@ -884,7 +871,7 @@ public void testReturnKey() {
884871
@Test
885872
public void testShowDiskLoc() {
886873
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
887-
.addSpecial("$showDiskLoc", true);
874+
.addSpecial("$showDiskLoc", true);
888875
try {
889876
while (cursor.hasNext()) {
890877
DBObject next = cursor.next();
@@ -894,7 +881,7 @@ public void testShowDiskLoc() {
894881
cursor.close();
895882
}
896883
cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
897-
.showDiskLoc();
884+
.showDiskLoc();
898885
try {
899886
while (cursor.hasNext()) {
900887
DBObject next = cursor.next();
@@ -908,8 +895,8 @@ public void testShowDiskLoc() {
908895
@Test(expected = MongoException.class)
909896
public void testSnapshotWithHint() {
910897
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
911-
.hint("x")
912-
.addSpecial("$snapshot", true);
898+
.hint("x")
899+
.addSpecial("$snapshot", true);
913900
try {
914901
while (cursor.hasNext()) {
915902
cursor.next();
@@ -922,8 +909,8 @@ public void testSnapshotWithHint() {
922909
@Test(expected = MongoException.class)
923910
public void testSnapshotWithSort() {
924911
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
925-
.sort(new BasicDBObject("x", 1))
926-
.addSpecial("$snapshot", true);
912+
.sort(new BasicDBObject("x", 1))
913+
.addSpecial("$snapshot", true);
927914
try {
928915
while (cursor.hasNext()) {
929916
cursor.next();
@@ -946,7 +933,7 @@ private void countResults(final DBCursor cursor, final int expected) {
946933
@Test
947934
public void testComment() {
948935
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
949-
.addSpecial("$comment", "test comment");
936+
.addSpecial("$comment", "test comment");
950937
while (cursor.hasNext()) {
951938
cursor.next();
952939
}
@@ -956,7 +943,7 @@ public void testComment() {
956943
@Test
957944
public void testBatchSizeTracking() {
958945
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
959-
.batchSize(2);
946+
.batchSize(2);
960947

961948
assertFalse(cursor.isBatchSizeTrackingDisabled());
962949
assertFalse(cursor.copy().isBatchSizeTrackingDisabled());
@@ -977,7 +964,7 @@ public void testBatchSizeTracking() {
977964
@Test
978965
public void testDisabledBatchSizeTracking() {
979966
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
980-
.disableBatchSizeTracking().batchSize(1);
967+
.disableBatchSizeTracking().batchSize(1);
981968
assertTrue(cursor.isBatchSizeTrackingDisabled());
982969
assertTrue(cursor.copy().isBatchSizeTrackingDisabled());
983970

0 commit comments

Comments
 (0)