#include <gtest/gtest.h>

#include "session_pool.h"

TEST(SESSION_POOL, POP_PUSH)
{
    struct session *sess1 = NULL;
    struct session *sess2 = NULL;
    struct session *sess3 = NULL;
    struct session *sess4 = NULL;
    struct session_pool *sess_pool = NULL;

    sess_pool = session_pool_new(3);
    EXPECT_TRUE(sess_pool != NULL);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 3);
    EXPECT_TRUE(session_pool_capacity_size(sess_pool) == 3);

    sess1 = session_pool_pop(sess_pool);
    EXPECT_TRUE(sess1 != NULL);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 2);
    sess2 = session_pool_pop(sess_pool);
    EXPECT_TRUE(sess2 != NULL);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 1);
    sess3 = session_pool_pop(sess_pool);
    EXPECT_TRUE(sess3 != NULL);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 0);
    sess4 = session_pool_pop(sess_pool);
    EXPECT_TRUE(sess4 == NULL);

    session_pool_push(sess_pool, sess1);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 1);
    session_pool_push(sess_pool, sess2);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 2);
    session_pool_push(sess_pool, sess3);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 3);

    sess1 = session_pool_pop(sess_pool);
    EXPECT_TRUE(sess1 != NULL);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 2);
    sess2 = session_pool_pop(sess_pool);
    EXPECT_TRUE(sess2 != NULL);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 1);
    sess3 = session_pool_pop(sess_pool);
    EXPECT_TRUE(sess3 != NULL);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 0);
    sess4 = session_pool_pop(sess_pool);
    EXPECT_TRUE(sess4 == NULL);

    session_pool_push(sess_pool, sess1);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 1);
    session_pool_push(sess_pool, sess2);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 2);
    session_pool_push(sess_pool, sess3);
    EXPECT_TRUE(session_pool_available_num(sess_pool) == 3);

    session_pool_free(sess_pool);
}

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
