Exemplo n.º 1
0
        public void CloseTest()
        {
            int num = 10;

            for (int i = 0; i < num; i++)
            {
                BsonDocument insertor = new BsonDocument();
                insertor.Add("num", i);
                coll.Insert(insertor);
            }

            BsonDocument dummy  = new BsonDocument();
            DBCursor     cursor = coll.Query(dummy, dummy, dummy, dummy);
            BsonDocument obj    = new BsonDocument();

            obj = cursor.Current();
            Assert.IsNotNull(obj);
            obj = cursor.Next();
            Assert.IsNotNull(obj);
            // DO:
            cursor.Close();
            // close
            try
            {
                cursor.Close();
            }catch (BaseException)
            {
                Assert.Fail();
                //Console.WriteLine("After close cursor, call Close() get errno " + e.ErrorCode );
                //Assert.IsTrue(e.ErrorType.Equals("SDB_DMS_CONTEXT_IS_CLOSE"));
            }
            // current
            try
            {
                cursor.Current();
            }
            catch (BaseException e)
            {
                Console.WriteLine("After close cursor, call Current() get errno " + e.ErrorCode);
                Assert.IsTrue(e.ErrorType.Equals("SDB_DMS_CONTEXT_IS_CLOSE"));
            }
            // next
            try
            {
                cursor.Next();
            }
            catch (BaseException e)
            {
                Console.WriteLine("After close cursor, call Next() get errno " + e.ErrorCode);
                Assert.IsTrue(e.ErrorType.Equals("SDB_DMS_CONTEXT_IS_CLOSE"));
            }
        }
Exemplo n.º 2
0
        public void CloseAllCursorsTest()
        {
            int num = 10000;

            for (int i = 0; i < num; i++)
            {
                BsonDocument insertor = new BsonDocument();
                insertor.Add("num", i);
                coll.Insert(insertor);
            }

            BsonDocument dummy   = new BsonDocument();
            DBCursor     cursor  = coll.Query(dummy, dummy, dummy, dummy);
            DBCursor     cursor1 = coll.Query(dummy, dummy, dummy, dummy);
            DBCursor     cursor2 = coll.Query(dummy, dummy, dummy, dummy);
            BsonDocument obj     = new BsonDocument();

            obj = cursor1.Next();
            obj = cursor2.Next();
            // DO:
            sdb.CloseAllCursors();
            // cursor
            try
            {
                while (null != cursor.Next())
                {
                }
            }
            catch (BaseException e)
            {
                int eno = e.ErrorCode;
                Assert.IsTrue(e.ErrorType.Equals("SDB_RTN_CONTEXT_NOTEXIST"));
            }
            // cursor1
            try
            {
                while (null != cursor1.Next())
                {
                }
            }
            catch (BaseException e)
            {
                int eno = e.ErrorCode;
                Assert.IsTrue(e.ErrorType.Equals("SDB_RTN_CONTEXT_NOTEXIST"));
            }
            // curosr2
            try
            {
                obj = cursor2.Current();
                cursor2.Close();
            }
            catch (BaseException)
            {
                Assert.Fail();
                //int eno = e.ErrorCode;
                //Assert.IsTrue(e.ErrorType.Equals("SDB_RTN_CONTEXT_NOTEXIST"));
            }
        }
Exemplo n.º 3
0
        public void NumberLongToStringTest()
        {
            string       expect1 = "";
            string       expect2 = "";
            BsonDocument result  = null;
            BsonDocument obj     = new BsonDocument();

            obj.Add("a", 0);
            obj.Add("b", int.MaxValue);
            obj.Add("c", int.MinValue);
            obj.Add("e", int.MaxValue + 1L);
            obj.Add("f", int.MinValue - 1L);
            obj.Add("g", long.MaxValue);
            obj.Add("h", long.MinValue);
            coll.Insert(obj);
            cur = coll.Query(null, new BsonDocument().Add("_id", new BsonDocument().Add("$include", 0)), null, null);
            try
            {
                result = cur.Next();
            }
            finally
            {
                cur.Close();
            }

            expect1 = "{ \"a\" : 0, \"b\" : 2147483647, \"c\" : -2147483648, \"e\" : 2147483648, \"f\" : -2147483649, \"g\" : 9223372036854775807, \"h\" : -9223372036854775808 }";
            expect2 = "{ \"a\" : 0, \"b\" : 2147483647, \"c\" : -2147483648, \"e\" : 2147483648, \"f\" : -2147483649, \"g\" : { \"$numberLong\": \"9223372036854775807\" }, \"h\" : { \"$numberLong\": \"-9223372036854775808\" } }";
            // case 1:
            Console.WriteLine("case1's result is: {0}", result.ToString());
            Assert.AreEqual(expect1, result.ToString());

            // case 2:
            BsonDefaults.JsCompatibility = true;
            Console.WriteLine("case2's result is: {0}", result.ToString());
            Assert.AreEqual(expect2, result.ToString());

            // case 3:
            BsonDefaults.JsCompatibility = false;
            Console.WriteLine("case3's result is: {0}", result.ToString());
            Assert.AreEqual(expect1, result.ToString());

            // case 4:
            BsonDefaults.JsCompatibility = true;
            Console.WriteLine("case4's result is: {0}", result.ToString());
            Assert.AreEqual(expect2, result.ToString());
        }
Exemplo n.º 4
0
        public void testLobOpenWrite7()
        {
            int bytesNum = 1024 * 1024 * 4;

            byte[] bytes = new byte[bytesNum];
            Random rand  = new Random();

            rand.NextBytes(bytes);

            int offset = bytesNum / 2;

            ObjectId id  = ObjectId.GenerateNewId();
            DBLob    lob = null;

            try
            {
                lob = cl.CreateLob(id);
                lob.Seek(offset, DBLob.SDB_LOB_SEEK_SET);
                lob.Write(bytes, offset, bytesNum - offset);
            }
            finally
            {
                if (lob != null)
                {
                    lob.Close();
                }
            }

            DBCursor cursor = null;

            try
            {
                cursor = cl.ListLobs();
                BsonDocument obj = cursor.Next();
                Assert.IsNotNull(obj);
                ObjectId oid = obj.GetValue("Oid").AsObjectId;
                Assert.AreEqual(id, oid);
                if (obj.Contains(FIELD_HAS_PIECES_INFO))
                {
                    Boolean hasPiecesInfo = obj.GetValue(FIELD_HAS_PIECES_INFO).AsBoolean;
                    Assert.IsTrue(hasPiecesInfo);
                }
                Assert.IsNull(cursor.Next());
            }
            finally
            {
                if (cursor != null)
                {
                    cursor.Close();
                }
            }

            long lobSize;

            lob = null;
            try
            {
                lob = cl.OpenLob(id, DBLob.SDB_LOB_WRITE);
                lob.Write(bytes, 0, offset);
                lobSize = lob.GetSize();
            }
            finally{
                if (lob != null)
                {
                    lob.Close();
                }
            }
            cursor = null;
            try
            {
                cursor = cl.ListLobs();
                BsonDocument obj = cursor.Next();
                Assert.IsNotNull(obj);
                ObjectId oid = obj.GetValue("Oid").AsObjectId;
                Assert.AreEqual(id, oid);
                if (obj.Contains(FIELD_HAS_PIECES_INFO))
                {
                    Boolean hasPiecesInfo = obj.GetValue(FIELD_HAS_PIECES_INFO).AsBoolean;
                    Assert.IsFalse(hasPiecesInfo);
                }
                Assert.IsNull(cursor.Next());
            }
            finally
            {
                cursor.Close();
            }

            lob = null;
            try {
                lob = cl.OpenLob(id);
                Assert.AreEqual(lobSize, lob.GetSize());
                byte[] bytes2 = new byte[(int)lob.GetSize()];
                lob.Read(bytes2);
                Assert.IsTrue(TestHelper.ByteArrayEqual(bytes, bytes2));
            }
            finally
            {
                if (lob != null)
                {
                    lob.Close();
                }
            }

            cl.RemoveLob(id);
            cursor = null;
            try
            {
                cursor = cl.ListLobs();
                Assert.IsNull(cursor.Next());
            }
            finally
            {
                if (cursor != null)
                {
                    cursor.Close();
                }
            }
        }