This is an automated email from the ASF dual-hosted git repository.
jbarrett pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode-native.git
The following commit(s) were added to refs/heads/develop by this push:
new 91f0af5 GEODE-5898: Throw CacheClosedException after Cache is closed.
91f0af5 is described below
commit 91f0af515bbbbdc62c9dc443f08c70111af8b7c3
Author: Jacob Barrett <[email protected]>
AuthorDate: Thu Oct 18 13:35:02 2018 -0700
GEODE-5898: Throw CacheClosedException after Cache is closed.
* Adds unit test to assert.
---
cppcache/src/CacheImpl.cpp | 52 +++++++++++++++++++++++-------------
cppcache/src/CacheImpl.hpp | 35 +++++++++++++++++++++---
cppcache/test/CMakeLists.txt | 1 +
cppcache/test/CacheTest.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 129 insertions(+), 22 deletions(-)
diff --git a/cppcache/src/CacheImpl.cpp b/cppcache/src/CacheImpl.cpp
index 2ac428e..f18c67d 100644
--- a/cppcache/src/CacheImpl.cpp
+++ b/cppcache/src/CacheImpl.cpp
@@ -241,6 +241,8 @@ CacheImpl::~CacheImpl() {
}
const std::string& CacheImpl::getName() const {
+ this->throwIfClosed();
+
return m_distributedSystem.getName();
}
@@ -257,7 +259,10 @@ DistributedSystem& CacheImpl::getDistributedSystem() {
return m_distributedSystem;
}
-TypeRegistry& CacheImpl::getTypeRegistry() { return *m_typeRegistry; }
+TypeRegistry& CacheImpl::getTypeRegistry() {
+ this->throwIfClosed();
+ return *m_typeRegistry;
+}
void CacheImpl::sendNotificationCloseMsgs() {
for (const auto& iter : getPoolManager().getAll()) {
@@ -269,6 +274,8 @@ void CacheImpl::sendNotificationCloseMsgs() {
}
void CacheImpl::close(bool keepalive) {
+ this->throwIfClosed();
+
TcrMessage::setKeepAlive(keepalive);
// bug #247 fix for durable clients missing events when recycled
sendNotificationCloseMsgs();
@@ -391,9 +398,7 @@ void CacheImpl::createRegion(std::string name,
}
}
- if (m_closed || m_destroyPending) {
- throw CacheClosedException("Cache::createRegion: cache closed");
- }
+ this->throwIfClosed();
if (name.find('/') != std::string::npos) {
throw IllegalArgumentException(
@@ -483,21 +488,11 @@ void CacheImpl::createRegion(std::string name,
rpImpl->releaseReadLock();
}
-/**
- * Return the existing region (or subregion) with the specified
- * path that already exists or is already mapped into the cache.
- * Whether or not the path starts with a forward slash it is interpreted as a
- * full path starting at a root.
- *
- * @param path the path to the region
- * @param[out] rptr the region pointer that is returned
- * @return the Region or null if not found
- * @throws IllegalArgumentException if path is null, the empty string, or "/"
- */
-
std::shared_ptr<Region> CacheImpl::getRegion(const std::string& path) {
LOGDEBUG("Cache::getRegion " + path);
+ this->throwIfClosed();
+
TryReadGuard guardCacheDestroy(m_destroyCacheMutex, m_destroyPending);
if (m_destroyPending) {
@@ -608,6 +603,8 @@ std::shared_ptr<RegionInternal>
CacheImpl::createRegion_internal(
}
std::vector<std::shared_ptr<Region>> CacheImpl::rootRegions() {
+ this->throwIfClosed();
+
std::vector<std::shared_ptr<Region>> regions;
MapOfRegionGuard guard(m_regions->mutex());
@@ -627,12 +624,13 @@ std::vector<std::shared_ptr<Region>>
CacheImpl::rootRegions() {
}
void CacheImpl::initializeDeclarativeCache(const std::string& cacheXml) {
- CacheXmlParser* xmlParser = CacheXmlParser::parse(cacheXml.c_str(), m_cache);
+ this->throwIfClosed();
+
+ auto xmlParser = std::unique_ptr<CacheXmlParser>(
+ CacheXmlParser::parse(cacheXml.c_str(), m_cache));
xmlParser->setAttributes(m_cache);
initServices();
xmlParser->create(m_cache);
- delete xmlParser;
- xmlParser = nullptr;
}
EvictionController* CacheImpl::getEvictionController() {
@@ -640,6 +638,8 @@ EvictionController* CacheImpl::getEvictionController() {
}
void CacheImpl::readyForEvents() {
+ this->throwIfClosed();
+
bool autoReadyForEvents =
m_distributedSystem.getSystemProperties().autoReadyForEvents();
bool isDurable = m_tcrConnectionManager->isDurable();
@@ -756,6 +756,8 @@ int CacheImpl::getPoolSize(const char* poolName) {
}
RegionFactory CacheImpl::createRegionFactory(RegionShortcut preDefinedRegion) {
+ this->throwIfClosed();
+
return RegionFactory(preDefinedRegion, this);
}
@@ -802,6 +804,8 @@ std::shared_ptr<SerializationRegistry>
CacheImpl::getSerializationRegistry()
ThreadPool* CacheImpl::getThreadPool() { return m_threadPool; }
std::shared_ptr<CacheTransactionManager>
CacheImpl::getCacheTransactionManager() {
+ this->throwIfClosed();
+
return m_cacheTXManager;
}
@@ -814,10 +818,14 @@ CacheImpl::getMemberListForVersionStamp() {
}
DataOutput CacheImpl::createDataOutput() const {
+ this->throwIfClosed();
+
return CacheImpl::createDataOutput(nullptr);
}
DataOutput CacheImpl::createDataOutput(Pool* pool) const {
+ this->throwIfClosed();
+
if (!pool) {
pool = this->getPoolManager().getDefaultPool().get();
}
@@ -826,6 +834,8 @@ DataOutput CacheImpl::createDataOutput(Pool* pool) const {
}
DataInput CacheImpl::createDataInput(const uint8_t* buffer, size_t len) const {
+ this->throwIfClosed();
+
return CacheImpl::createDataInput(buffer, len, nullptr);
}
@@ -839,6 +849,8 @@ DataInput CacheImpl::createDataInput(const uint8_t* buffer,
size_t len,
PdxInstanceFactory CacheImpl::createPdxInstanceFactory(
const std::string& className) const {
+ this->throwIfClosed();
+
return PdxInstanceFactory(
className, *m_cacheStats, *m_pdxTypeRegistry, *this,
m_distributedSystem.getSystemProperties().getEnableTimeStatistics());
@@ -847,6 +859,8 @@ PdxInstanceFactory CacheImpl::createPdxInstanceFactory(
AuthenticatedView CacheImpl::createAuthenticatedView(
std::shared_ptr<Properties> userSecurityProperties,
const std::string& poolName) {
+ this->throwIfClosed();
+
if (poolName.empty()) {
auto pool = m_poolManager->getDefaultPool();
if (!this->isClosed() && pool != nullptr) {
diff --git a/cppcache/src/CacheImpl.hpp b/cppcache/src/CacheImpl.hpp
index 38c1ae8..f0f6908 100644
--- a/cppcache/src/CacheImpl.hpp
+++ b/cppcache/src/CacheImpl.hpp
@@ -183,6 +183,17 @@ class APACHE_GEODE_EXPORT CacheImpl : private NonCopyable,
void createRegion(std::string name, RegionAttributes aRegionAttributes,
std::shared_ptr<Region>& regionPtr);
+ /**
+ * Return the existing region (or subregion) with the specified
+ * path that already exists or is already mapped into the cache.
+ * Whether or not the path starts with a forward slash it is interpreted as a
+ * full path starting at a root.
+ *
+ * @param path the path to the region
+ * @param[out] rptr the region pointer that is returned
+ * @return the Region or null if not found
+ * @throws IllegalArgumentException if path is null, the empty string, or "/"
+ */
std::shared_ptr<Region> getRegion(const std::string& path);
/**
@@ -259,7 +270,11 @@ class APACHE_GEODE_EXPORT CacheImpl : private NonCopyable,
return m_attributes == nullptr ? false : m_attributes->m_cacheMode;
}
- bool getPdxIgnoreUnreadFields() { return m_ignorePdxUnreadFields; }
+ bool getPdxIgnoreUnreadFields() {
+ this->throwIfClosed();
+
+ return m_ignorePdxUnreadFields;
+ }
void setPdxIgnoreUnreadFields(bool ignore) {
m_ignorePdxUnreadFields = ignore;
@@ -267,7 +282,10 @@ class APACHE_GEODE_EXPORT CacheImpl : private NonCopyable,
void setPdxReadSerialized(bool val) { m_readPdxSerialized = val; }
- bool getPdxReadSerialized() { return m_readPdxSerialized; }
+ bool getPdxReadSerialized() {
+ this->throwIfClosed();
+ return m_readPdxSerialized;
+ }
bool isCacheDestroyPending() const;
@@ -278,13 +296,18 @@ class APACHE_GEODE_EXPORT CacheImpl : private NonCopyable,
std::shared_ptr<SerializationRegistry> getSerializationRegistry() const;
inline CachePerfStats& getCachePerfStats() { return *m_cacheStats; }
- PoolManager& getPoolManager() const { return *m_poolManager; }
+ PoolManager& getPoolManager() const {
+ this->throwIfClosed();
+ return *m_poolManager;
+ }
const std::shared_ptr<Pool>& getDefaultPool() {
return m_poolManager->getDefaultPool();
}
SystemProperties& getSystemProperties() const {
+ this->throwIfClosed();
+
return m_distributedSystem.getSystemProperties();
}
@@ -381,6 +404,12 @@ class APACHE_GEODE_EXPORT CacheImpl : private NonCopyable,
const std::shared_ptr<AuthInitialize> m_authInitialize;
std::unique_ptr<TypeRegistry> m_typeRegistry;
+ inline void throwIfClosed() const {
+ if (m_closed) {
+ throw CacheClosedException("Cache is closed.");
+ }
+ }
+
friend class CacheFactory;
friend class Cache;
friend class DistributedSystemImpl;
diff --git a/cppcache/test/CMakeLists.txt b/cppcache/test/CMakeLists.txt
index 1fa77c9..9cb1e22 100644
--- a/cppcache/test/CMakeLists.txt
+++ b/cppcache/test/CMakeLists.txt
@@ -25,6 +25,7 @@ add_executable(apache-geode_unittests
ByteArrayFixture.cpp
ByteArrayFixture.hpp
ByteArrayTest.cpp
+ CacheTest.cpp
CacheableKeyCreateTests.cpp
CacheableKeysTest.cpp
CacheableStringEqualityTest.cpp
diff --git a/cppcache/test/CacheTest.cpp b/cppcache/test/CacheTest.cpp
new file mode 100644
index 0000000..7b51843
--- /dev/null
+++ b/cppcache/test/CacheTest.cpp
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+#include <gtest/gtest.h>
+
+#include <geode/AuthenticatedView.hpp>
+#include <geode/Cache.hpp>
+#include <geode/PoolManager.hpp>
+#include <geode/RegionFactory.hpp>
+#include <geode/RegionShortcut.hpp>
+
+using apache::geode::client::CacheClosedException;
+using apache::geode::client::CacheFactory;
+using apache::geode::client::RegionShortcut;
+
+/**
+ * Cache should close and throw exceptions on methods called after close.
+ */
+TEST(CacheTest, close) {
+ auto cache = CacheFactory{}.set("log-level", "none").create();
+ ASSERT_FALSE(cache.isClosed());
+
+ cache.close();
+ ASSERT_TRUE(cache.isClosed());
+
+ EXPECT_THROW(cache.close(), CacheClosedException);
+ EXPECT_THROW(cache.close(true), CacheClosedException);
+ EXPECT_THROW(cache.createAuthenticatedView(nullptr, "pool"),
+ CacheClosedException);
+ EXPECT_THROW(cache.createDataInput(nullptr, 0), CacheClosedException);
+ EXPECT_THROW(cache.createDataOutput(), CacheClosedException);
+ EXPECT_THROW(cache.createPdxInstanceFactory("classname"),
+ CacheClosedException);
+ EXPECT_THROW(cache.createRegionFactory(RegionShortcut::LOCAL),
+ CacheClosedException);
+ EXPECT_THROW(cache.getCacheTransactionManager(), CacheClosedException);
+ EXPECT_THROW(cache.getName(), CacheClosedException);
+ EXPECT_THROW(cache.getPdxIgnoreUnreadFields(), CacheClosedException);
+ EXPECT_THROW(cache.getPdxReadSerialized(), CacheClosedException);
+ EXPECT_THROW(cache.getPoolManager(), CacheClosedException);
+ EXPECT_THROW(cache.getQueryService(), CacheClosedException);
+ EXPECT_THROW(cache.getQueryService("pool"), CacheClosedException);
+ EXPECT_THROW(cache.getRegion("region"), CacheClosedException);
+ EXPECT_THROW(cache.getSystemProperties(), CacheClosedException);
+ EXPECT_THROW(cache.getTypeRegistry(), CacheClosedException);
+ EXPECT_THROW(cache.initializeDeclarativeCache(""), CacheClosedException);
+ EXPECT_THROW(cache.readyForEvents(), CacheClosedException);
+ EXPECT_THROW(cache.rootRegions(), CacheClosedException);
+}