From fdde6a69c68606940e4f5a4c54b9057ace617a49 Mon Sep 17 00:00:00 2001 From: Michael Hackstein Date: Tue, 8 Sep 2015 15:35:21 +0200 Subject: [PATCH] Added failure tests for new implementation of HashIndex. And fixed a memleak there. --- arangod/Indexes/HashIndex.cpp | 1 + arangod/Indexes/PathBasedIndex.cpp | 10 + js/server/tests/shell-hash-index-failures.js | 184 +++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 js/server/tests/shell-hash-index-failures.js diff --git a/arangod/Indexes/HashIndex.cpp b/arangod/Indexes/HashIndex.cpp index c5b3c0a112..b5e5a45de8 100644 --- a/arangod/Indexes/HashIndex.cpp +++ b/arangod/Indexes/HashIndex.cpp @@ -140,6 +140,7 @@ HashIndex::UniqueArray::~UniqueArray () { delete _hashArray; delete _hashElement; + delete _isEqualElElByKey; } // ----------------------------------------------------------------------------- diff --git a/arangod/Indexes/PathBasedIndex.cpp b/arangod/Indexes/PathBasedIndex.cpp index 2bbed603fa..5f667ae647 100644 --- a/arangod/Indexes/PathBasedIndex.cpp +++ b/arangod/Indexes/PathBasedIndex.cpp @@ -97,6 +97,10 @@ int PathBasedIndex::fillElement (std::vector& elements, return TRI_ERROR_INTERNAL; } + + TRI_IF_FAILURE("FillElementIllegalShape") { + return TRI_ERROR_INTERNAL; + } size_t const n = _paths.size(); std::vector shapes; @@ -115,6 +119,9 @@ int PathBasedIndex::fillElement (std::vector& elements, if (element == nullptr) { return TRI_ERROR_OUT_OF_MEMORY; } + TRI_IF_FAILURE("FillElementOOM") { + return TRI_ERROR_OUT_OF_MEMORY; + } element->document(const_cast(document)); TRI_shaped_sub_t* subObjects = element->subObjects(); @@ -144,6 +151,9 @@ int PathBasedIndex::fillElement (std::vector& elements, if (element == nullptr) { return TRI_ERROR_OUT_OF_MEMORY; } + TRI_IF_FAILURE("FillElementOOM") { + return TRI_ERROR_OUT_OF_MEMORY; + } element->document(const_cast(document)); TRI_shaped_sub_t* subObjects = element->subObjects(); diff --git a/js/server/tests/shell-hash-index-failures.js b/js/server/tests/shell-hash-index-failures.js new file mode 100644 index 0000000000..20d5d00f3e --- /dev/null +++ b/js/server/tests/shell-hash-index-failures.js @@ -0,0 +1,184 @@ +/*jshint globalstrict:false, strict:false */ +/*global fail, assertEqual, assertTrue, assertNotEqual */ + +//////////////////////////////////////////////////////////////////////////////// +/// @brief tests failure cases in HashIndex +/// +/// @file +/// +/// DISCLAIMER +/// +/// Copyright 2010-2012 triagens GmbH, Cologne, Germany +/// +/// Licensed under the Apache License, Version 2.0 (the "License"); +/// you may not use this file except in compliance with the License. +/// You may obtain a copy of the License at +/// +/// http://www.apache.org/licenses/LICENSE-2.0 +/// +/// Unless required by applicable law or agreed to in writing, software +/// distributed under the License is distributed on an "AS IS" BASIS, +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +/// See the License for the specific language governing permissions and +/// limitations under the License. +/// +/// Copyright holder is ArangoDB GmbH, Cologne, Germany +/// +/// @author Michael Hackstein +/// @author Copyright 2015, ArangoDB GmbH, Cologne, Germany +//////////////////////////////////////////////////////////////////////////////// + +var jsunity = require("jsunity"); +var internal = require("internal"); +var errors = internal.errors; +var testHelper = require("org/arangodb/test-helper").Helper; + +// ----------------------------------------------------------------------------- +// --SECTION-- basic methods +// ----------------------------------------------------------------------------- + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test suite: Unique Hash Index +//////////////////////////////////////////////////////////////////////////////// + +function UniqueHashIndexFailuresSuite () { + 'use strict'; + var cn = "UnitTestsCollectionHash"; + var collection = null; + + return { + +//////////////////////////////////////////////////////////////////////////////// +/// @brief set up +//////////////////////////////////////////////////////////////////////////////// + + setUp : function () { + internal.db._drop(cn); + collection = internal.db._create(cn); + collection.ensureUniqueConstraint("a"); + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief tear down +//////////////////////////////////////////////////////////////////////////////// + + tearDrop : function () { + internal.db._drop(cn); + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test: unique hash index insert Illegal Shape +//////////////////////////////////////////////////////////////////////////////// + + testInsertIllegalShape : function () { + internal.debugSetFailAt("FillElementIllegalShape"); + try { + collection.save({a: 1}); + fail(); + } catch (e) { + assertEqual(internal.errors.ERROR_INTERNAL.code, e.errorNum); + } + assertEqual(collection.count(), 0); + assertEqual(collection.firstExample({a: 1}), null); + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test: unique hash index fill element oom +//////////////////////////////////////////////////////////////////////////////// + + testCreateIndexElementOOM : function () { + internal.debugSetFailAt("FillElementOOM"); + try { + collection.save({a: 1}); + fail(); + } catch (e) { + assertEqual(internal.errors.ERROR_INTERNAL.code, e.errorNum); + } + assertEqual(collection.count(), 0); + assertEqual(collection.firstExample({a: 1}), null); + } + + }; +} + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test suite: Hash Index Multi +//////////////////////////////////////////////////////////////////////////////// + +function HashIndexMultiFailuresSuite () { + 'use strict'; + var cn = "UnitTestsCollectionHash"; + var collection = null; + + return { + +//////////////////////////////////////////////////////////////////////////////// +/// @brief set up +//////////////////////////////////////////////////////////////////////////////// + + setUp : function () { + internal.db._drop(cn); + collection = internal.db._create(cn); + collection.ensureHashIndex("a"); + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief tear down +//////////////////////////////////////////////////////////////////////////////// + + tearDrop : function () { + internal.db._drop(cn); + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test: hash index multi insert Illegal Shape +//////////////////////////////////////////////////////////////////////////////// + + testInsertIllegalShape : function () { + internal.debugSetFailAt("FillElementIllegalShape"); + try { + collection.save({a: 1}); + fail(); + } catch (e) { + assertEqual(internal.errors.ERROR_INTERNAL.code, e.errorNum); + } + assertEqual(collection.count(), 0); + assertEqual(collection.firstExample({a: 1}), null); + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test: hash index multi fill element oom +//////////////////////////////////////////////////////////////////////////////// + + testCreateIndexElementOOM : function () { + collection.ensureUniqueConstraint("a"); + internal.debugSetFailAt("FillElementOOM"); + try { + collection.save({a: 1}); + fail(); + } catch (e) { + assertEqual(internal.errors.ERROR_INTERNAL.code, e.errorNum); + } + assertEqual(collection.count(), 0); + assertEqual(collection.firstExample({a: 1}), null); + } + + }; +} + +//////////////////////////////////////////////////////////////////////////////// +/// @brief executes the test suites +//////////////////////////////////////////////////////////////////////////////// + +if (internal.debugCanUseFailAt()) { + jsunity.run(UniqueHashIndexFailuresSuite); + jsunity.run(HashIndexMultiFailuresSuite); +} + +return jsunity.done(); + +// Local Variables: +// mode: outline-minor +// outline-regexp: "^\\(/// @brief\\|/// @addtogroup\\|// --SECTION--\\|/// @page\\|/// @}\\)" +// End: +