|
| 1 | +// DBTCPConnectorTest.java |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright (C) 2011 10gen Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.mongodb; |
| 20 | + |
| 21 | +import com.mongodb.util.TestCase; |
| 22 | +import org.testng.annotations.BeforeClass; |
| 23 | +import org.testng.annotations.BeforeMethod; |
| 24 | +import org.testng.annotations.Test; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.net.UnknownHostException; |
| 28 | + |
| 29 | +/** |
| 30 | + * Testing functionality of database TCP connector |
| 31 | + */ |
| 32 | +public class DBTCPConnectorTest extends TestCase { |
| 33 | + |
| 34 | + @BeforeClass |
| 35 | + public void beforeClass() throws UnknownHostException { |
| 36 | + cleanupMongo = new Mongo("127.0.0.1"); |
| 37 | + cleanupDB = "com_mongodb_unittest_DBTCPConnectorTest"; |
| 38 | + _db = cleanupMongo.getDB(cleanupDB); |
| 39 | + _collection = _db.getCollection("testCol"); |
| 40 | + } |
| 41 | + |
| 42 | + @BeforeMethod |
| 43 | + public void beforeMethod() throws UnknownHostException { |
| 44 | + _connector = new DBTCPConnector(cleanupMongo, new ServerAddress("127.0.0.1")); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Test request reservation |
| 49 | + */ |
| 50 | + @Test |
| 51 | + public void testRequestReservation() { |
| 52 | + assertEquals(false, _connector.getMyPort()._inRequest); |
| 53 | + _connector.requestStart(); |
| 54 | + assertNull(_connector.getMyPort()._requestPort); |
| 55 | + assertEquals(true, _connector.getMyPort()._inRequest); |
| 56 | + _connector.requestDone(); |
| 57 | + assertEquals(false, _connector.getMyPort()._inRequest); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Tests that same connections is used for sequential writes |
| 62 | + */ |
| 63 | + @Test |
| 64 | + public void testConnectionReservationForWrites() { |
| 65 | + _connector.requestStart(); |
| 66 | + _connector.say(_db, createOutMessageForInsert(), WriteConcern.SAFE); |
| 67 | + assertNotNull(_connector.getMyPort()._requestPort); |
| 68 | + DBPort requestPort = _connector.getMyPort()._requestPort; |
| 69 | + _connector.say(_db, createOutMessageForInsert(), WriteConcern.SAFE); |
| 70 | + assertEquals(requestPort, _connector.getMyPort()._requestPort); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Tests that same connections is used for write followed by read |
| 75 | + */ |
| 76 | + @Test |
| 77 | + public void testConnectionReservationForWriteThenRead() { |
| 78 | + _connector.requestStart(); |
| 79 | + _connector.say(_db, createOutMessageForInsert(), WriteConcern.SAFE); |
| 80 | + DBPort requestPort = _connector.getMyPort()._requestPort; |
| 81 | + _connector.call(_db, _collection, |
| 82 | + OutMessage.query(cleanupMongo, 0, _collection.getFullName(), 0, -1, new BasicDBObject(), new BasicDBObject(), ReadPreference.PRIMARY), |
| 83 | + null, 0); |
| 84 | + assertEquals(requestPort, _connector.getMyPort()._requestPort); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Tests that same connections is used for sequential reads |
| 89 | + */ |
| 90 | + @Test |
| 91 | + public void testConnectionReservationForReads() { |
| 92 | + _connector.requestStart(); |
| 93 | + _connector.call(_db, _collection, |
| 94 | + OutMessage.query(cleanupMongo, 0, _collection.getFullName(), 0, -1, new BasicDBObject(), new BasicDBObject(), ReadPreference.PRIMARY), |
| 95 | + null, 0); |
| 96 | + assertNotNull(_connector.getMyPort()._requestPort); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + private OutMessage createOutMessageForInsert() { |
| 101 | + OutMessage om = new OutMessage( cleanupMongo , 2002, new DefaultDBEncoder() ); |
| 102 | + |
| 103 | + int flags = 0; |
| 104 | + om.writeInt( flags ); |
| 105 | + om.writeCString(_collection.getFullName()); |
| 106 | + om.putObject( new BasicDBObject() ); |
| 107 | + |
| 108 | + return om; |
| 109 | + } |
| 110 | + |
| 111 | + private DB _db; |
| 112 | + private DBCollection _collection; |
| 113 | + private DBTCPConnector _connector; |
| 114 | +} |
0 commit comments