예제 #1
0
bool TestRunner::openByModeStrTest()
{
    logMsg( INFO, "openByModeStrTest" );

    // in this test we want to check the alternate open modes available for buffered i/o
    // this test is only run if we are doing buffered I/O and expects it is run after the
    // write test to guarantee the file is there
    reset();
    m_file = IDBDataFile::open(IDBDataFile::BUFFERED, m_fname.c_str(), "r+b", m_open_opts);
    if( !m_file )
    {
        ostringstream errstr;
        errstr << "Unable to open " << m_fname << " for read/write";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    // keep this fairly simple - read a block then write a block

    ssize_t readct = m_file->read(m_defbuf,BLK_SIZE);
    if( (size_t) readct != BLK_SIZE )
    {
        ostringstream errstr;
        errstr << "Only read " << readct << " bytes, expected 4";
        logMsg( ERROR, errstr.str() );
        return false;
    }
    if( m_defbuf[0] != (unsigned char) 0 )
    {
        ostringstream errstr;
        errstr << "Data error - expected " << 0 << ", read " << (int) m_defbuf[0];
        logMsg( ERROR, errstr.str() );
        return false;
    }

    // we should be at block 1
    long filepos = m_file->tell();
    if( filepos != long(BLK_SIZE) )
    {
        ostringstream errstr;
        errstr << "File position not at correct block, " << filepos << " != " << BLK_SIZE;
        logMsg( ERROR, errstr.str() );
        return false;
    }

    m_defbuf[0] = 1;
    ssize_t bytes_written = m_file->write(m_defbuf, BLK_SIZE);
    if( (size_t) bytes_written != BLK_SIZE )
    {
        ostringstream errstr;
        errstr << "Only wrote " << bytes_written << " bytes, expected 4";
        logMsg( ERROR, errstr.str() );
        return false;
    }

    return true;
}
예제 #2
0
bool TestRunner::hdfsRdwrExhaustTest()
{
	// this is going to be a self-contained test that attempts to test
	// all logic inherent in HdfsRdwr

	// choose a new filename that is specific to our thread
	ostringstream oss;
	// embed pid so that this is a new directory path
	oss << "/tmp/hdfsrdwr-" << getpid() << "-" << m_id;
	string newpath = oss.str();

	// open a file with arbitrarily small buffer
	IDBDataFile* file = IDBDataFile::open(IDBDataFile::HDFS, newpath.c_str(), "r+", 0, 8);
	assert( file );

	// check various empty file conditions
	assert( file->size() == 0 );
	assert( file->tell() == 0 );
	assert( file->seek(-1, SEEK_CUR) == -1);
	assert( file->seek(0, SEEK_SET) == 0);
	unsigned char buf[4];
	assert( file->read(buf, 4) == 0);

	// write some data
	buf[0] = 0xde; buf[1] = 0xad; buf[2] = 0xbe; buf[3] = 0xef;
	assert( file->write(buf, 4) == 4);
	assert( file->size() == 4 );
	assert( file->tell() == 4 );
	assert( file->truncate(-1) == -1 );

	// now make file empty again
	assert( file->truncate(0) == 0 );
	assert( file->size() == 0 );
	assert( file->seek(0, SEEK_SET) == 0);
	assert( file->tell() == 0 );
	assert( file->read(buf, 4) == 0);

	// write data again, this time exactly up to allocated size
	assert( file->write(buf, 4) == 4);
	assert( file->write(buf, 4) == 4);
	assert( file->size() == 8 );
	assert( file->tell() == 8 );

	// truncate back to 4
	assert( file->truncate(4) == 0 );
	assert( file->size() == 4 );
	assert( file->seek(4, SEEK_SET) == 0);
	assert( file->tell() == 4 );

	// now trigger a buffer reallocation
	assert( file->write(buf, 4) == 4);
	assert( file->write(buf, 4) == 4);
	assert( file->size() == 12 );

	// now delete and close.
	delete file;

	// check the file size through the file system
	IDBFileSystem& fs = IDBFileSystem::getFs( IDBDataFile::HDFS );
	assert( fs.size( newpath.c_str() ) == 12);

	// open again - the file is bigger than the default buffer so it triggers alternate
	// logic in the constructor
	file = IDBDataFile::open(IDBDataFile::HDFS, newpath.c_str(), "r+", 0, 8);
	assert( file );
	assert( file->size() == 12);
	unsigned char newbuf[4];
	assert( file->pread(newbuf, 4, 4) == 4);
	assert( newbuf[0] == 0xde && newbuf[1] == 0xad && newbuf[2] == 0xbe &&newbuf[3] == 0xef);
	delete file;

	fs.remove(newpath.c_str());

	return true;
}