TEST( MEMORY, CLEAR ) {
	dout << __LINE__ << std::endl;

	rlimit a;
	a.rlim_cur = rlim_t( 0 );
	a.rlim_max = rlim_t( 0 );
	if( setrlimit( RLIMIT_AS, &a ) != 0 ) {
		if( errno == EFAULT ) std::cerr << __LINE__ << ": EFAULT" << std::endl;
		else if( errno == EINVAL ) std::cerr << __LINE__ << ": EINVAL" << std::endl;
		else if( errno == EPERM ) std::cerr << __LINE__ << ": EPERM" << std::endl;
		else if( errno == ESRCH ) std::cerr << __LINE__ << ": ESRCH" << std::endl;
		else std::cerr << __LINE__ << ": SOME ERROR" << std::endl;
	}

	dout << __LINE__ << std::endl;

	treap<A> abba;

	dout << __LINE__ << std::endl;

	try {
		std::string d = "";
		while( true ) {
			d += "a";
			abba.emplace(d, d + "b", d + "c");
		}

		dout << __LINE__ << std::endl;
	} catch( std::bad_alloc const & ) {
		dout << __LINE__ << std::endl;

		try {
			while( true )
				new bool( true );

			dout << __LINE__ << std::endl;
		} catch( std::bad_alloc const & ) {
			dout << __LINE__ << std::endl;

			EXPECT_NO_THROW( abba.clear() );

			dout << __LINE__ << std::endl;

			EXPECT_EQ( 0, count_constructed - count_destroyed );

			dout << __LINE__ << std::endl;
		}

		dout << __LINE__ << std::endl;
	}

	dout << __LINE__ << std::endl;
}
示例#2
0
bool KDSoapServer::setExpectedSocketCount(int sockets)
{
    // I hit a system limit when trying to connect more than 1024 sockets in the same process.
    // strace said: socket(PF_INET, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_IP) = -1 EMFILE (Too many open files)
    // Solution: ulimit -n 4096
    // Or in C code, below.

#ifdef Q_OS_UNIX
    struct rlimit lim;
    if (getrlimit(RLIMIT_NOFILE, &lim) != 0) {
        qDebug() << "error calling getrlimit:" << strerror(errno);
        return false;
    }
    bool changingHardLimit = false;
    if (sockets > -1) {
        qDebug() << "Current limit" << lim.rlim_cur << lim.rlim_max;
        sockets += 20; // we need some file descriptors too
        if (rlim_t(sockets) <= lim.rlim_cur)
            return true; // nothing to do

        if (rlim_t(sockets) > lim.rlim_max) {
            // Seems we need to run as root then
            lim.rlim_max = sockets;
            qDebug() << "Setting rlim_max to" << sockets;
            changingHardLimit = true;
        }
    }
#ifdef OPEN_MAX
    // Mac OSX: setrlimit() no longer accepts "rlim_cur = RLIM_INFINITY" for RLIM_NOFILE.  Use "rlim_cur = min(OPEN_MAX, rlim_max)".
    lim.rlim_cur = qMin(rlim_t(OPEN_MAX), lim.rlim_max);
#else
    // Linux: does not define OPEN_MAX anymore, since it's "configurable at runtime".
    lim.rlim_cur = lim.rlim_max;
#endif
    if (setrlimit(RLIMIT_NOFILE, &lim) == 0) {
        qDebug() << "limit set to" << lim.rlim_cur;
    } else {
        if (changingHardLimit) {
            qDebug() << "WARNING: hard limit is not high enough";
        }
        qDebug() << "error calling setrlimit(" << lim.rlim_cur << "," << lim.rlim_max << ") :" << strerror(errno);
        return false;
    }
#else
    Q_UNUSED(sockets);
#endif
    return true;
}
TEST( MEMORY, ERASE_INSERT_THROW_CLEAR ) {
	dout << __LINE__ << std::endl;

	rlimit a;
	a.rlim_cur = rlim_t( 0 );
	a.rlim_max = rlim_t( 0 );
	if( setrlimit( RLIMIT_AS, &a ) != 0 ) {
		if( errno == EFAULT ) std::cerr << __LINE__ << ": EFAULT" << std::endl;
		else if( errno == EINVAL ) std::cerr << __LINE__ << ": EINVAL" << std::endl;
		else if( errno == EPERM ) std::cerr << __LINE__ << ": EPERM" << std::endl;
		else if( errno == ESRCH ) std::cerr << __LINE__ << ": ESRCH" << std::endl;
		else std::cerr << __LINE__ << ": SOME ERROR" << std::endl;
	}

	dout << __LINE__ << std::endl;

	treap<A> abba;

	dout << __LINE__ << std::endl;

	auto it = abba.emplace("a", "ab", "ac");

	dout << __LINE__ << std::endl;

	try {
		std::string d = "";
		while( true ) {
			d += "a";
			abba.emplace(d, d + "b", d + "c");
		}

		dout << __LINE__ << std::endl;
	} catch( std::bad_alloc const & ) {
		dout << __LINE__ << std::endl;

		try {
			while( true )
				new bool( true );

			dout << __LINE__ << std::endl;
		} catch( std::bad_alloc const & ) {
			dout << __LINE__ << std::endl;

			unsigned long long count_now = count_constructed - count_destroyed;

			dout << __LINE__ << std::endl;

			EXPECT_NO_THROW( abba.erase( it ) );

			dout << __LINE__ << std::endl;

			EXPECT_EQ( count_now - 1, count_constructed - count_destroyed );

			dout << __LINE__ << std::endl;

			EXPECT_THROW( abba.emplace("dsgdasbtdfgbsvakhgljhljhsdfbhfbdsfa", "dsgdasbtafshdfhdfdfgbsvasdfbhfbdsfa", "dsgdasbtdfghdsjgfbsvasdfbhfkhgbdsfa"), std::bad_alloc );

			dout << __LINE__ << std::endl;

			EXPECT_EQ( count_now - 1, count_constructed - count_destroyed );

			dout << __LINE__ << std::endl;

			EXPECT_NO_THROW( abba.emplace("a", "ab", "ac") );

			dout << __LINE__ << std::endl;

			EXPECT_EQ( count_now, count_constructed - count_destroyed );

			dout << __LINE__ << std::endl;

			EXPECT_NO_THROW( abba.clear() );

			dout << __LINE__ << std::endl;

			EXPECT_EQ( 0, count_constructed - count_destroyed );

			dout << __LINE__ << std::endl;
		}

		dout << __LINE__ << std::endl;
	}

	dout << __LINE__ << std::endl;
}