1 // Copyright 2014 The Android Open Source Project
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "emugl/common/message_channel.h"
17 #include "emugl/common/testing/test_thread.h"
19 #include <gtest/gtest.h>
27 struct PingPongState {
28 MessageChannel<std::string, 3U> in;
29 MessageChannel<std::string, 3U> out;
32 void* pingPongFunction(void* param) {
34 PingPongState* s = static_cast<PingPongState*>(param);
47 TEST(MessageChannel, SingleThreadWithInt) {
48 MessageChannel<int, 3U> channel;
54 channel.receive(&ret);
56 channel.receive(&ret);
58 channel.receive(&ret);
62 TEST(MessageChannel, SingleThreadWithStdString) {
63 MessageChannel<std::string, 5U> channel;
64 channel.send(std::string("foo"));
65 channel.send(std::string("bar"));
66 channel.send(std::string("zoo"));
69 channel.receive(&str);
70 EXPECT_STREQ("foo", str.c_str());
71 channel.receive(&str);
72 EXPECT_STREQ("bar", str.c_str());
73 channel.receive(&str);
74 EXPECT_STREQ("zoo", str.c_str());
77 TEST(MessageChannel, TwoThreadsPingPong) {
79 TestThread* thread = new TestThread(pingPongFunction, &state);
82 const size_t kCount = 100;
83 for (size_t n = 0; n < kCount; ++n) {
84 state.in.send(std::string("foo"));
85 state.in.send(std::string("bar"));
86 state.in.send(std::string("zoo"));
87 state.out.receive(&str);
88 EXPECT_STREQ("foo", str.c_str());
89 state.out.receive(&str);
90 EXPECT_STREQ("bar", str.c_str());
91 state.out.receive(&str);
92 EXPECT_STREQ("zoo", str.c_str());
94 state.in.send(std::string("quit"));
95 state.out.receive(&str);
96 EXPECT_STREQ("quit", str.c_str());