TEST(LogMessagePoolTests, ReturnTooLargeMessage) {
  static const size_t INITIAL_CAPACITY=128;
  static const size_t MAX_CAPACITY= 1024;
  static const size_t MAX_RETURNED_MESSAGE_SIZE= 256;
  static const size_t INITIAL_POOL_SIZE= 4;
  static const size_t MAX_POOL_SIZE= 8;
  TestingLogMessagePool factory(INITIAL_CAPACITY, MAX_CAPACITY,
				MAX_RETURNED_MESSAGE_SIZE, INITIAL_POOL_SIZE,
				MAX_POOL_SIZE);
  std::vector<LogMessage*> messagesInPool;
  std::vector<LogMessage*> newMessagesInPool;
  LogMessage* msg;

  messagesInPool.reserve(INITIAL_POOL_SIZE);
  factory.getMessagesInPool(std::back_inserter(messagesInPool));
  EXPECT_EQ(messagesInPool.size(), INITIAL_POOL_SIZE);

  msg= factory.get();
  EXPECT_EQ(msg->capacity(), factory.initialMessageSize());
  EXPECT_EQ(msg->maxCapacity(), factory.maxMessageSize());
  EXPECT_TRUE(msg->empty());

  // Verify the message came from the pool
  EXPECT_EQ(factory.numMessagesInPool(), INITIAL_POOL_SIZE-1);
  EXPECT_TRUE(std::find(messagesInPool.begin(), messagesInPool.end(), msg) != messagesInPool.end());

  // Make the message too big to be returned to the pool
  msg->increaseCapacity(MAX_RETURNED_MESSAGE_SIZE+16);
  ASSERT_GT(msg->capacity(), MAX_RETURNED_MESSAGE_SIZE);
  
  // Verify the message destroyed upon release and not put back in the pool
  factory.release(msg);
  EXPECT_EQ(factory.numMessagesInPool(), INITIAL_POOL_SIZE-1);
}
TEST(LogMessagePoolTests, ReleaseNonEmptyMessage) {
  static const size_t INITIAL_CAPACITY=128;
  static const size_t MAX_CAPACITY= 1024;
  static const size_t MAX_RETURNED_MESSAGE_SIZE= 256;
  static const size_t INITIAL_POOL_SIZE= 4;
  static const size_t MAX_POOL_SIZE= 8;
  TestingLogMessagePool factory(INITIAL_CAPACITY, MAX_CAPACITY,
				MAX_RETURNED_MESSAGE_SIZE, INITIAL_POOL_SIZE,
				MAX_POOL_SIZE);
  std::vector<LogMessage*> messages;
  LogMessage* msg;

  msg= factory.get();
  EXPECT_EQ(msg->capacity(), factory.initialMessageSize());
  EXPECT_EQ(msg->maxCapacity(), factory.maxMessageSize());
  EXPECT_TRUE(msg->empty());
  ASSERT_EQ(factory.numMessagesInPool(), INITIAL_POOL_SIZE-1);

  // Make the message not empty and return it to the pool
  msg->setEnd(msg->begin() + msg->capacity()/2);
  ASSERT_FALSE(msg->empty());
  factory.release(msg);
  ASSERT_EQ(factory.numMessagesInPool(), INITIAL_POOL_SIZE);

  // Now get all messages in the pool, verify that one of them is msg,
  // and that all messages (including msg) are empty after leaving the pool
  for (int i=0;i<INITIAL_POOL_SIZE;++i) {
    messages.push_back(factory.get());
    EXPECT_TRUE(messages.back()->empty());
  }
  EXPECT_EQ(factory.numMessagesInPool(), 0);
  EXPECT_TRUE(std::find(messages.begin(), messages.end(), msg) != messages.end());

  // Put everything back
  std::for_each(messages.begin(), messages.end(),
		[&factory](LogMessage* m) { factory.release(m); });
}
TEST(LogMessagePoolTests, GetAndRelease) {
  static const size_t INITIAL_CAPACITY=128;
  static const size_t MAX_CAPACITY= 1024;
  static const size_t MAX_RETURNED_MESSAGE_SIZE= 256;
  static const size_t INITIAL_POOL_SIZE= 4;
  static const size_t MAX_POOL_SIZE= 8;
  TestingLogMessagePool factory(INITIAL_CAPACITY, MAX_CAPACITY,
				MAX_RETURNED_MESSAGE_SIZE, INITIAL_POOL_SIZE,
				MAX_POOL_SIZE);
  std::vector<LogMessage*> messagesInPool;
  std::vector<LogMessage*> newMessagesInPool;
  LogMessage* msg;

  messagesInPool.reserve(INITIAL_POOL_SIZE);
  factory.getMessagesInPool(std::back_inserter(messagesInPool));
  EXPECT_EQ(messagesInPool.size(), INITIAL_POOL_SIZE);

  msg= factory.get();
  EXPECT_EQ(msg->capacity(), factory.initialMessageSize());
  EXPECT_EQ(msg->maxCapacity(), factory.maxMessageSize());
  EXPECT_TRUE(msg->empty());

  // Verify the message came from the pool
  EXPECT_EQ(factory.numMessagesInPool(), INITIAL_POOL_SIZE-1);
  EXPECT_TRUE(std::find(messagesInPool.begin(), messagesInPool.end(), msg) != messagesInPool.end());

  // Verify the message returned to the pool
  factory.release(msg);
  EXPECT_EQ(factory.numMessagesInPool(), INITIAL_POOL_SIZE);
  
  factory.getMessagesInPool(std::back_inserter(newMessagesInPool));
  std::sort(messagesInPool.begin(), messagesInPool.end());
  std::sort(newMessagesInPool.begin(), newMessagesInPool.end());
  if (messagesInPool != newMessagesInPool) {
    std::ostringstream details;
    details << "messagesInPool != newMessagesInPool\n  messagesInPool = [ "
	    << join(messagesInPool.begin(), messagesInPool.end(), ", ")
	    << " ]\n  newMessagesInPool = [ "
	    << join(newMessagesInPool.begin(), newMessagesInPool.end(), ", ")
	    << " ]";
    EXPECT_TRUE(false) << details.str();
  }
}