Skip to content

Commit

Permalink
Test for FileEventDataListener. Re #4812.
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellTaylor committed Feb 27, 2012
1 parent e755e02 commit 34a85ef
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/DataHandling/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ set ( TEST_FILES
test/DefineGaugeVolumeTest.h
test/DeleteTableRowsTest.h
test/FakeEventDataListenerTest.h
#test/FileEventDataListenerTest.h
test/FileEventDataListenerTest.h
test/FindDetectorsInShapeTest.h
test/FindDetectorsParTest.h
test/GetMaskedDetectorsTest.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,18 @@ namespace Mantid
bool isConnected();

private:
const std::string m_filename; ///< The file to read
int m_numChunks; ///< The number of pieces to divide the file into
int m_nextChunk; ///< The number of the next chunk to be loaded
const std::string m_filename; ///< The file to read
const std::string m_tempWSname; ///< The name of the hidden workspace that holds the next chunk
int m_numChunks; ///< The number of pieces to divide the file into
int m_nextChunk; ///< The number of the next chunk to be loaded

/// Future that holds the result of the latest call to LoadEventPreNexus
Poco::ActiveResult<bool> * m_chunkload;
void loadChunk();
/// Shared pointer to the LoadEventPreNexus instance - it needs to be kept alive.
API::Algorithm_sptr m_loader;

static Kernel::Logger& g_log; ///< reference to the logger class
static Kernel::Logger& g_log; ///< reference to the logger class
};

} // namespace DataHandling
Expand Down
24 changes: 17 additions & 7 deletions Code/Mantid/Framework/DataHandling/src/FileEventDataListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ namespace DataHandling
/// Constructor
FileEventDataListener::FileEventDataListener() : ILiveListener(),
m_filename(ConfigService::Instance().getString("fileeventdatalistener.filename")),
m_nextChunk(1), m_chunkload(NULL)
m_tempWSname("__filelistenerchunk"), m_nextChunk(1), m_chunkload(NULL)
{
if ( m_filename.empty() ) g_log.error("Configuration property fileeventdatalistener.filename not found");
if ( m_filename.empty() ) g_log.error("Configuration property fileeventdatalistener.filename not found. The algorithm will fail!");

if ( ! ConfigService::Instance().getValue("fileeventdatalistener.chunks",m_numChunks) )
{
g_log.error("Configuration property fileeventdatalistener.chunks not found");
g_log.error("Configuration property fileeventdatalistener.chunks not found. The algorithm will fail!");
m_numChunks = 0; // Set it to 0 so the algorithm just fails
}
}

Expand All @@ -33,8 +34,8 @@ namespace DataHandling
{
// Don't disappear until any running job has finished or bad things happen!
if ( m_chunkload ) m_chunkload->wait();
// Clean up the hidden workspace
AnalysisDataService::Instance().remove("__filelistenerchunk");
// Clean up the hidden workspace if necessary
if ( AnalysisDataService::Instance().doesExist(m_tempWSname) ) AnalysisDataService::Instance().remove(m_tempWSname);
// Don't leak memory
delete m_chunkload;
}
Expand Down Expand Up @@ -68,11 +69,20 @@ namespace DataHandling
if ( ! m_chunkload->data() ) throw std::runtime_error("LoadEventPreNexus failed for some reason.");
// The loading succeeded: get the workspace from the ADS.
MatrixWorkspace_sptr chunk = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("__filelistenerchunk");
// Remove the workspace from the ADS now we've extracted it
AnalysisDataService::Instance().remove(m_tempWSname);
// Delete the ActiveResult to signify that we're done with it.
delete m_chunkload;
m_chunkload = NULL;
// Kick off the loading of the next chunk (unless we're at the end of the file)
if ( m_nextChunk <= m_numChunks ) loadChunk();
if ( m_nextChunk <= m_numChunks )
{
loadChunk();
}
else
{
m_loader.reset(); // Clear the algorithm so that it releases its handle on the workspace
}

return chunk;
}
Expand All @@ -87,7 +97,7 @@ namespace DataHandling
m_loader->setPropertyValue("EventFilename",m_filename);
m_loader->setProperty("ChunkNumber",m_nextChunk++); // post-increment
m_loader->setProperty("TotalChunks",m_numChunks);
m_loader->setPropertyValue("OutputWorkspace","__filelistenerchunk"); // Goes into 'hidden' workspace
m_loader->setPropertyValue("OutputWorkspace",m_tempWSname); // Goes into 'hidden' workspace
m_chunkload = new Poco::ActiveResult<bool>(m_loader->executeAsync());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef MANTID_DATAHANDLING_FILEEVENTDATALISTENERTEST_H_
#define MANTID_DATAHANDLING_FILEEVENTDATALISTENERTEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidAPI/LiveListenerFactory.h"
#include "MantidDataObjects/EventWorkspace.h"

using namespace Mantid::Kernel;
using namespace Mantid::API;

class FileEventDataListenerTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static FileEventDataListenerTest *createSuite() { return new FileEventDataListenerTest(); }
static void destroySuite( FileEventDataListenerTest *suite ) { delete suite; }

// This is just a test class to help with development, so let's keep the test simple and all in one method
void testTheListener()
{
// Set the properties that are required by this listener
ConfigService::Instance().setString("fileeventdatalistener.filename","REF_L_32035_neutron_event.dat");
ConfigService::Instance().setString("fileeventdatalistener.chunks","2");

// Create the listener. Remember: this will call connect()
ILiveListener_sptr listener = LiveListenerFactory::Instance().create("FileEventDataListener");

// Test the 'property' methods
TS_ASSERT( listener )
TS_ASSERT_EQUALS( listener->name(), "FileEventDataListener")
TS_ASSERT( ! listener->supportsHistory() )
TS_ASSERT( listener->buffersEvents() )
TS_ASSERT( listener->isConnected() )

TS_ASSERT_THROWS_NOTHING( listener->start(0) );

MatrixWorkspace_const_sptr buffer;
TS_ASSERT_THROWS_NOTHING( buffer = listener->extractData())
// Check this is the only surviving reference to it
TS_ASSERT_EQUALS( buffer.use_count(), 1 )
TS_ASSERT_EQUALS( buffer->getNumberHistograms(), 77824 )

MatrixWorkspace_const_sptr buffer2;
// Call extractData again
TS_ASSERT_THROWS_NOTHING( buffer2 = listener->extractData())
// Check this is the only surviving reference to it
TS_ASSERT_EQUALS( buffer2.use_count(), 1 )
// Check it's a different workspace to last time
TS_ASSERT_DIFFERS( buffer.get(), buffer2.get() )
TS_ASSERT_EQUALS( buffer2->getNumberHistograms(), 77824 )

// Calling it again will throw as it's the end of the file
TS_ASSERT_THROWS( listener->extractData(), std::runtime_error )
}

};


#endif /* MANTID_DATAHANDLING_FILEEVENTDATALISTENERTEST_H_ */

0 comments on commit 34a85ef

Please sign in to comment.