This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
#include "mojo/system/message_pipe.h"
#include "base/memory/ref_counted.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "mojo/system/waiter.h"
#include "mojo/system/waiter_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace system {
namespace {
TEST(MessagePipeTest, Basic) {
scoped_refptr<MessagePipe> mp(new MessagePipe());
int32_t buffer[2];
const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer));
uint32_t buffer_size;
buffer[0] = 123;
buffer[1] = 456;
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
mp->ReadMessage(0,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(kBufferSize, buffer_size);
EXPECT_EQ(123, buffer[0]);
EXPECT_EQ(456, buffer[1]);
buffer[0] = 123;
buffer[1] = 456;
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
mp->ReadMessage(1,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
buffer[0] = 789012345;
buffer[1] = 0;
EXPECT_EQ(MOJO_RESULT_OK,
mp->WriteMessage(1,
buffer, static_cast<uint32_t>(sizeof(buffer[0])),
NULL,
MOJO_WRITE_MESSAGE_FLAG_NONE));
buffer[0] = 123;
buffer[1] = 456;
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_OK,
mp->ReadMessage(0,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(static_cast<uint32_t>(sizeof(buffer[0])), buffer_size);
EXPECT_EQ(789012345, buffer[0]);
EXPECT_EQ(456, buffer[1]);
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
mp->ReadMessage(0,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
buffer[0] = 123456789;
buffer[1] = 0;
EXPECT_EQ(MOJO_RESULT_OK,
mp->WriteMessage(0,
buffer, static_cast<uint32_t>(sizeof(buffer[0])),
NULL,
MOJO_WRITE_MESSAGE_FLAG_NONE));
buffer[0] = 234567890;
buffer[1] = 0;
EXPECT_EQ(MOJO_RESULT_OK,
mp->WriteMessage(0,
buffer, static_cast<uint32_t>(sizeof(buffer[0])),
NULL,
MOJO_WRITE_MESSAGE_FLAG_NONE));
buffer_size = 0;
EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
mp->ReadMessage(1,
NULL, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(static_cast<uint32_t>(sizeof(buffer[0])), buffer_size);
buffer[0] = 123;
buffer[1] = 456;
buffer_size = 1;
EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
mp->ReadMessage(1,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(static_cast<uint32_t>(sizeof(buffer[0])), buffer_size);
EXPECT_EQ(123, buffer[0]);
EXPECT_EQ(456, buffer[1]);
buffer[0] = 123;
buffer[1] = 456;
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_OK,
mp->ReadMessage(1,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(static_cast<uint32_t>(sizeof(buffer[0])), buffer_size);
EXPECT_EQ(123456789, buffer[0]);
EXPECT_EQ(456, buffer[1]);
buffer[0] = 123;
buffer[1] = 456;
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_OK,
mp->ReadMessage(1,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(static_cast<uint32_t>(sizeof(buffer[0])), buffer_size);
EXPECT_EQ(234567890, buffer[0]);
EXPECT_EQ(456, buffer[1]);
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
mp->ReadMessage(1,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
buffer[0] = 345678901;
buffer[1] = 0;
EXPECT_EQ(MOJO_RESULT_OK,
mp->WriteMessage(0,
buffer, static_cast<uint32_t>(sizeof(buffer[0])),
NULL,
MOJO_WRITE_MESSAGE_FLAG_NONE));
mp->Close(0);
buffer[0] = 456789012;
buffer[1] = 0;
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
mp->WriteMessage(1,
buffer, static_cast<uint32_t>(sizeof(buffer[0])),
NULL,
MOJO_WRITE_MESSAGE_FLAG_NONE));
buffer[0] = 123;
buffer[1] = 456;
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_OK,
mp->ReadMessage(1,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(static_cast<uint32_t>(sizeof(buffer[0])), buffer_size);
EXPECT_EQ(345678901, buffer[0]);
EXPECT_EQ(456, buffer[1]);
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
mp->ReadMessage(1,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
mp->Close(1);
}
TEST(MessagePipeTest, CloseWithQueuedIncomingMessages) {
scoped_refptr<MessagePipe> mp(new MessagePipe());
int32_t buffer[1];
const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer));
uint32_t buffer_size;
for (int32_t i = 0; i < 5; i++) {
buffer[0] = i;
EXPECT_EQ(MOJO_RESULT_OK,
mp->WriteMessage(1,
buffer, kBufferSize,
NULL,
MOJO_WRITE_MESSAGE_FLAG_NONE));
}
buffer_size = 0;
EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
mp->ReadMessage(0,
NULL, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(kBufferSize, buffer_size);
mp->Close(0);
mp->Close(1);
}
TEST(MessagePipeTest, DiscardMode) {
scoped_refptr<MessagePipe> mp(new MessagePipe());
int32_t buffer[2];
const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer));
uint32_t buffer_size;
buffer[0] = 789012345;
buffer[1] = 0;
EXPECT_EQ(MOJO_RESULT_OK,
mp->WriteMessage(1,
buffer, static_cast<uint32_t>(sizeof(buffer[0])),
NULL,
MOJO_WRITE_MESSAGE_FLAG_NONE));
buffer_size = 0;
EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
mp->ReadMessage(0,
NULL, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_MAY_DISCARD));
EXPECT_EQ(static_cast<uint32_t>(sizeof(buffer[0])), buffer_size);
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
mp->ReadMessage(0,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_MAY_DISCARD));
buffer[0] = 890123456;
buffer[1] = 0;
EXPECT_EQ(MOJO_RESULT_OK,
mp->WriteMessage(1,
buffer, static_cast<uint32_t>(sizeof(buffer[0])),
NULL,
MOJO_WRITE_MESSAGE_FLAG_NONE));
buffer[0] = 123;
buffer[1] = 456;
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_OK,
mp->ReadMessage(0,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_MAY_DISCARD));
EXPECT_EQ(static_cast<uint32_t>(sizeof(buffer[0])), buffer_size);
EXPECT_EQ(890123456, buffer[0]);
EXPECT_EQ(456, buffer[1]);
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
mp->ReadMessage(0,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_MAY_DISCARD));
buffer[0] = 901234567;
buffer[1] = 0;
EXPECT_EQ(MOJO_RESULT_OK,
mp->WriteMessage(1,
buffer, static_cast<uint32_t>(sizeof(buffer[0])),
NULL,
MOJO_WRITE_MESSAGE_FLAG_NONE));
buffer_size = 1;
EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
mp->ReadMessage(0,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_MAY_DISCARD));
EXPECT_EQ(static_cast<uint32_t>(sizeof(buffer[0])), buffer_size);
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
mp->ReadMessage(0,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_MAY_DISCARD));
buffer[0] = 123456789;
buffer[1] = 0;
EXPECT_EQ(MOJO_RESULT_OK,
mp->WriteMessage(1,
buffer, static_cast<uint32_t>(sizeof(buffer[0])),
NULL,
MOJO_WRITE_MESSAGE_FLAG_NONE));
buffer_size = 1;
EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
mp->ReadMessage(0,
NULL, NULL,
0, NULL,
MOJO_READ_MESSAGE_FLAG_MAY_DISCARD));
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
mp->ReadMessage(0,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_MAY_DISCARD));
mp->Close(0);
mp->Close(1);
}
TEST(MessagePipeTest, BasicWaiting) {
scoped_refptr<MessagePipe> mp(new MessagePipe());
Waiter waiter;
int32_t buffer[1];
const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer));
uint32_t buffer_size;
waiter.Init();
EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
mp->AddWaiter(0, &waiter, MOJO_WAIT_FLAG_WRITABLE, 0));
waiter.Init();
EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
mp->AddWaiter(0,
&waiter,
MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE,
0));
waiter.Init();
EXPECT_EQ(MOJO_RESULT_OK,
mp->AddWaiter(0, &waiter, MOJO_WAIT_FLAG_READABLE, 1));
EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0));
mp->RemoveWaiter(0, &waiter);
buffer[0] = 123456789;
EXPECT_EQ(MOJO_RESULT_OK,
mp->WriteMessage(0,
buffer, kBufferSize,
NULL,
MOJO_WRITE_MESSAGE_FLAG_NONE));
waiter.Init();
EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
mp->AddWaiter(1, &waiter, MOJO_WAIT_FLAG_READABLE, 2));
waiter.Init();
EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
mp->AddWaiter(1,
&waiter,
MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE,
0));
waiter.Init();
EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
mp->AddWaiter(1, &waiter, MOJO_WAIT_FLAG_WRITABLE, 3));
mp->Close(0);
waiter.Init();
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
mp->AddWaiter(1, &waiter, MOJO_WAIT_FLAG_WRITABLE, 4));
waiter.Init();
EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
mp->AddWaiter(1, &waiter, MOJO_WAIT_FLAG_READABLE, 5));
buffer[0] = 0;
buffer_size = kBufferSize;
EXPECT_EQ(MOJO_RESULT_OK,
mp->ReadMessage(1,
buffer, &buffer_size,
0, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(123456789, buffer[0]);
waiter.Init();
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
mp->AddWaiter(1, &waiter, MOJO_WAIT_FLAG_READABLE, 6));
mp->Close(1);
}
TEST(MessagePipeTest, ThreadedWaiting) {
int32_t buffer[1];
const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer));
MojoResult result;
{
scoped_refptr<MessagePipe> mp(new MessagePipe());
test::SimpleWaiterThread thread(&result);
thread.waiter()->Init();
EXPECT_EQ(MOJO_RESULT_OK,
mp->AddWaiter(1, thread.waiter(), MOJO_WAIT_FLAG_READABLE, 0));
thread.Start();
buffer[0] = 123456789;
EXPECT_EQ(MOJO_RESULT_OK,
mp->WriteMessage(0,
buffer, kBufferSize,
NULL,
MOJO_WRITE_MESSAGE_FLAG_NONE));
mp->RemoveWaiter(1, thread.waiter());
mp->Close(0);
mp->Close(1);
}
EXPECT_EQ(0, result);
{
scoped_refptr<MessagePipe> mp(new MessagePipe());
test::SimpleWaiterThread thread(&result);
thread.waiter()->Init();
EXPECT_EQ(MOJO_RESULT_OK,
mp->AddWaiter(1, thread.waiter(), MOJO_WAIT_FLAG_READABLE, 0));
thread.Start();
mp->CancelAllWaiters(1);
mp->Close(1);
mp->Close(0);
}
EXPECT_EQ(MOJO_RESULT_CANCELLED, result);
{
scoped_refptr<MessagePipe> mp(new MessagePipe());
test::SimpleWaiterThread thread(&result);
thread.waiter()->Init();
EXPECT_EQ(MOJO_RESULT_OK,
mp->AddWaiter(1, thread.waiter(), MOJO_WAIT_FLAG_READABLE, 0));
thread.Start();
mp->CancelAllWaiters(0);
mp->Close(0);
mp->RemoveWaiter(1, thread.waiter());
mp->CancelAllWaiters(1);
mp->Close(1);
}
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result);
}
}
}
}