<commit_before>/**
 * Copyright 2008 <NAME>
 * Licensed 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 "testpp.h"
#include "shession_store.h"


/**
 * A mock timer class for injecting into the shession_store
 * for testing expiration.
 */
class mock_timer_c
: public shession_store_c::timer_c
{
public:
	/**
	 * Construct the timer, starting at 0
	 */
	mock_timer_c()
	: m_seconds( 0 )
	{}
	/**
	 * Return the constant number of seconds.
	 */
	virtual time_t operator () () const
	{
		return m_seconds;
	}
	/**
	 * Add time to this timer.
	 */
	virtual void operator += ( time_t seconds )
	{
		m_seconds += seconds;
	}

private:
	time_t m_seconds;
};


/**
 * Test 4 basic sessions to make sure they can be added and killed
 * and things work as expected.
 */
TESTPP( test_4_sessions )
{
	shession_store_c store( 1 );

	const char *sid_1 = "dog";
	const char *sid_2 = "cat";
	const char *sid_3 = "mouse";
	const char *sid_4 = "flea";
	std::string empty;

	store.create_session( sid_1, empty );
	store.create_session( sid_2, empty );
	store.create_session( sid_3, empty );
	store.create_session( sid_4, empty );

	true == actual( store.renew_session( sid_1 ) );
	true == actual( store.renew_session( sid_2 ) );
	true == actual( store.renew_session( sid_3 ) );
	true == actual( store.renew_session( sid_4 ) );

	store.kill_session( sid_1 );
	store.kill_session( sid_2 );
	store.kill_session( sid_3 );
	store.kill_session( sid_4 );

	false == actual( store.renew_session( sid_1 ) );
	false == actual( store.renew_session( sid_2 ) );
	false == actual( store.renew_session( sid_3 ) );
	false == actual( store.renew_session( sid_4 ) );
}

/**
 * Test that expiration works as expected.
 */
TESTPP( test_expiration )
{
	shession_store_c store( 5 );
	mock_timer_c mock_timer;
	store.set_timer( mock_timer );

	const char *sid_1 = "dog";
	const char *sid_2 = "cat";
	const char *sid_3 = "mouse";
	const char *sid_4 = "flea";
	std::string user_id;

	store.create_session( sid_1, user_id );
	store.create_session( sid_2, user_id );
	store.create_session( sid_3, user_id );
	store.create_session( sid_4, user_id );

	mock_timer += 3;
	true == actual( store.renew_session( sid_4 ) );

	mock_timer += 4;
	true == actual( store.renew_session( sid_4 ) );
	4 == actual( store.size() );
	false == actual( store.renew_session( sid_1 ) );
	3 == actual( store.size() );
	2 == actual( store.kill_expired() );
	1 == actual( store.size() );

	mock_timer += 4;
	true == actual( store.renew_session( sid_4 ) );
}
<commit_msg>upgraded shession_store_test.cpp to use the new assertion syntax<commit_after>/**
 * Copyright 2008 <NAME>
 * Licensed 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 "testpp.h"
#include "shession_store.h"


/**
 * A mock timer class for injecting into the shession_store
 * for testing expiration.
 */
class mock_timer_c
: public shession_store_c::timer_c
{
public:
	/**
	 * Construct the timer, starting at 0
	 */
	mock_timer_c()
	: m_seconds( 0 )
	{}
	/**
	 * Return the constant number of seconds.
	 */
	virtual time_t operator () () const
	{
		return m_seconds;
	}
	/**
	 * Add time to this timer.
	 */
	virtual void operator += ( time_t seconds )
	{
		m_seconds += seconds;
	}

private:
	time_t m_seconds;
};


/**
 * Test 4 basic sessions to make sure they can be added and killed
 * and things work as expected.
 */
TESTPP( test_4_sessions )
{
	shession_store_c store( 1 );

	const char *sid_1 = "dog";
	const char *sid_2 = "cat";
	const char *sid_3 = "mouse";
	const char *sid_4 = "flea";
	std::string empty;

	store.create_session( sid_1, empty );
	store.create_session( sid_2, empty );
	store.create_session( sid_3, empty );
	store.create_session( sid_4, empty );

	assertpp( store.renew_session( sid_1 ) ).t();
	assertpp( store.renew_session( sid_2 ) ).t();
	assertpp( store.renew_session( sid_3 ) ).t();
	assertpp( store.renew_session( sid_4 ) ).t();

	store.kill_session( sid_1 );
	store.kill_session( sid_2 );
	store.kill_session( sid_3 );
	store.kill_session( sid_4 );

	assertpp( store.renew_session( sid_1 ) ).f();
	assertpp( store.renew_session( sid_2 ) ).f();
	assertpp( store.renew_session( sid_3 ) ).f();
	assertpp( store.renew_session( sid_4 ) ).f();
}

/**
 * Test that expiration works as expected.
 */
TESTPP( test_expiration )
{
	shession_store_c store( 5 );
	mock_timer_c mock_timer;
	store.set_timer( mock_timer );

	const char *sid_1 = "dog";
	const char *sid_2 = "cat";
	const char *sid_3 = "mouse";
	const char *sid_4 = "flea";
	std::string user_id;

	store.create_session( sid_1, user_id );
	store.create_session( sid_2, user_id );
	store.create_session( sid_3, user_id );
	store.create_session( sid_4, user_id );

	mock_timer += 3;
	assertpp( store.renew_session( sid_4 ) ).t();

	mock_timer += 4;
	assertpp( store.renew_session( sid_4 ) ).t();
	assertpp( store.size() ) == 4;
	assertpp( store.renew_session( sid_1 ) ).f();
	assertpp( store.size() ) == 3;
	assertpp( store.kill_expired() ) == 2;
	assertpp( store.size() ) == 1;

	mock_timer += 4;
	assertpp( store.renew_session( sid_4 ) ).t();
}

{'fraction_non_alphanumeric': 0.13045634920634921, 'fraction_numerical': 0.017526455026455025, 'mean_word_length': 3.9379591836734695, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7031614', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2082, 'n_words': 629}