Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix leaks in ComputeResourceInfo (XML parsing and test) #508

Merged
merged 5 commits into from
Apr 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Code/Mantid/Framework/Kernel/src/ComputeResourceInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ ComputeResourceInfo::ComputeResourceInfo(const FacilityInfo *fac,

const std::string baseTag = "baseURL";
Poco::AutoPtr<Poco::XML::NodeList> nl = elem->getElementsByTagName(baseTag);
if (!nl || nl->length() != 1 || !nl->item(0) || !nl->item(0)->childNodes()) {
if (!nl || nl->length() != 1 || !nl->item(0) ||
!nl->item(0)->hasChildNodes()) {
g_log.error("Failed to get base URL for remote compute resource '" +
m_name + "'");
throw std::runtime_error("Remote compute resources must have exactly one "
Expand Down
41 changes: 18 additions & 23 deletions Code/Mantid/Framework/Kernel/test/ComputeResourceInfoTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "MantidKernel/Exception.h"
#include "MantidKernel/FacilityInfo.h"

#include <boost/make_shared.hpp>
#include <Poco/DOM/AutoPtr.h>
#include <Poco/DOM/Document.h>
#include <Poco/DOM/DOMParser.h>
Expand All @@ -14,45 +15,41 @@ using namespace Mantid::Kernel;
class ComputeResourceInfoTest : public CxxTest::TestSuite {
public:
void test_allMissing() {
FacilityInfo *fac = NULL;
boost::shared_ptr<FacilityInfo> fac;
TS_ASSERT_THROWS_NOTHING(fac =
createCRInfoInMinimalFacility(simpleInstStr));
TS_ASSERT(fac);
std::vector<ComputeResourceInfo> cri;
TS_ASSERT_THROWS_NOTHING(cri = fac->computeResInfos());
TS_ASSERT_EQUALS(cri.size(), 0);

delete fac;
fac = NULL;
TS_ASSERT_THROWS(fac = createCRInfoInMinimalFacility(
boost::shared_ptr<FacilityInfo> another;
TS_ASSERT_THROWS(another = createCRInfoInMinimalFacility(
"<computeResource fooAtt=\"barVal\"/>"),
std::runtime_error);
TS_ASSERT(!fac);
delete fac;
TS_ASSERT(!another);
}

void test_noURLTag() {
const std::string crTxt = "<computeResource name=\"foo\">"
"<u>" +
fermiURL + "</u>"
"</computeResource>";
FacilityInfo *fac = NULL;
boost::shared_ptr<FacilityInfo> fac;
TS_ASSERT_THROWS(fac = createCRInfoInMinimalFacility(crTxt),
std::runtime_error);
TS_ASSERT(!fac);
delete fac;
}

void test_wrongXML() {
const std::string crTxt = "<computeResource name=\"foo\">"
"<u_foo>" +
fermiURL + "</u_bar>"
"</compResource>";
FacilityInfo *fac = NULL;
boost::shared_ptr<FacilityInfo> fac;
TS_ASSERT_THROWS(fac = createCRInfoInMinimalFacility(crTxt),
Poco::XML::XMLException);
TS_ASSERT(!fac);
delete fac;
}

void test_normalFermi() {
Expand All @@ -62,7 +59,7 @@ class ComputeResourceInfoTest : public CxxTest::TestSuite {
fermiURL + "</baseURL>"
"</computeResource>";

FacilityInfo *fac = NULL;
boost::shared_ptr<FacilityInfo> fac;
TS_ASSERT_THROWS_NOTHING(fac = createCRInfoInMinimalFacility(fermi));
TS_ASSERT(fac);
TS_ASSERT_EQUALS(fac->name(), this->testFacilityName);
Expand Down Expand Up @@ -95,12 +92,11 @@ class ComputeResourceInfoTest : public CxxTest::TestSuite {
fermiURL + "</URL>"
"</computeResource>";

FacilityInfo *fac = NULL;
boost::shared_ptr<FacilityInfo> fac;
TS_ASSERT_THROWS(fac = createCRInfoInMinimalFacility(fermi),
std::runtime_error);

TS_ASSERT(!fac);
delete fac;
}

void test_normalSCARF() {
Expand All @@ -110,7 +106,7 @@ class ComputeResourceInfoTest : public CxxTest::TestSuite {
scarfURL + "</baseURL>"
"</computeResource>";

FacilityInfo *fac = NULL;
boost::shared_ptr<FacilityInfo> fac;
TS_ASSERT_THROWS_NOTHING(fac = createCRInfoInMinimalFacility(scarf));
TS_ASSERT(fac);
TS_ASSERT_EQUALS(fac->name(), this->testFacilityName);
Expand All @@ -119,7 +115,8 @@ class ComputeResourceInfoTest : public CxxTest::TestSuite {
TS_ASSERT_EQUALS(cri.size(), 1);

ComputeResourceInfo cr = fac->computeResInfos().front();
TS_ASSERT_THROWS(ComputeResourceInfo fail = fac->computeResource("inexistent!"),
TS_ASSERT_THROWS(ComputeResourceInfo fail =
fac->computeResource("inexistent!"),
Mantid::Kernel::Exception::NotFoundError);
ComputeResourceInfo cr2 = fac->computeResource(scarfName);
TS_ASSERT_EQUALS(cr, cr2);
Expand All @@ -133,7 +130,6 @@ class ComputeResourceInfoTest : public CxxTest::TestSuite {
TS_ASSERT_EQUALS(scarfType, cr2.remoteJobManagerType());
TS_ASSERT_EQUALS(fac->name(), cr.facility().name());
TS_ASSERT_EQUALS(fac->name(), cr2.facility().name());
delete fac;
}

void test_brokenSCARF() {
Expand All @@ -143,11 +139,10 @@ class ComputeResourceInfoTest : public CxxTest::TestSuite {
"<URL>" +
scarfURL + "</URL>"
"</computeResource>";
FacilityInfo *fac = NULL;
boost::shared_ptr<FacilityInfo> fac;
TS_ASSERT_THROWS(fac = createCRInfoInMinimalFacility(err),
std::runtime_error);
TS_ASSERT(!fac);
delete fac;
}

void test_equals() {
Expand Down Expand Up @@ -178,7 +173,7 @@ class ComputeResourceInfoTest : public CxxTest::TestSuite {
fermiURL + "</baseURL>"
"</computeResource>";

FacilityInfo *fac = NULL;
boost::shared_ptr<FacilityInfo> fac;
TS_ASSERT_THROWS_NOTHING(fac = createCRInfoInMinimalFacility(rep));
TS_ASSERT(fac);
TS_ASSERT_EQUALS(fac->computeResources().size(), 3);
Expand All @@ -205,13 +200,13 @@ class ComputeResourceInfoTest : public CxxTest::TestSuite {
!(fac->computeResource(fermiName) == fac->computeResource(thirdName)));
TS_ASSERT(
!(fac->computeResource(otherName) == fac->computeResource(thirdName)));
delete fac;
}

private:
/// make a minimal facilities file/xml string includin the compute resource
/// passed
FacilityInfo *createCRInfoInMinimalFacility(const std::string &crStr) {
boost::shared_ptr<FacilityInfo>
createCRInfoInMinimalFacility(const std::string &crStr) {
const std::string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<facilities>"
" <facility name=\"" +
Expand All @@ -223,13 +218,13 @@ class ComputeResourceInfoTest : public CxxTest::TestSuite {
return createFacility(xmlStr);
}

FacilityInfo *createFacility(const std::string &xml) {
boost::shared_ptr<FacilityInfo> createFacility(const std::string &xml) {
Poco::XML::DOMParser parser;
Poco::AutoPtr<Poco::XML::Document> pDoc = parser.parseString(xml);
Poco::XML::Element *pRootElem = pDoc->documentElement();
Poco::XML::Element *elem = pRootElem->getChildElement("facility");

return new FacilityInfo(elem);
return boost::make_shared<FacilityInfo>(elem);
}

private:
Expand Down