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 "anbox/common/message_channel.h"
17 #include <gtest/gtest.h>
24 TEST(MessageChannel, SingleThreadWithInt) {
25 MessageChannel<int, 3U> channel;
31 channel.receive(&ret);
33 channel.receive(&ret);
35 channel.receive(&ret);
39 TEST(MessageChannel, SingleThreadWithStdString) {
40 MessageChannel<std::string, 5U> channel;
41 channel.send(std::string("foo"));
42 channel.send(std::string("bar"));
43 channel.send(std::string("zoo"));
46 channel.receive(&str);
47 EXPECT_STREQ("foo", str.c_str());
48 channel.receive(&str);
49 EXPECT_STREQ("bar", str.c_str());
50 channel.receive(&str);
51 EXPECT_STREQ("zoo", str.c_str());
54 TEST(MessageChannel, TwoThreadsPingPong) {
55 struct PingPongState {
56 MessageChannel<std::string, 3U> in;
57 MessageChannel<std::string, 3U> out;
62 std::thread thread([&](){
65 state.in.receive(&str);
74 const size_t kCount = 100;
75 for (size_t n = 0; n < kCount; ++n) {
76 state.in.send(std::string("foo"));
77 state.in.send(std::string("bar"));
78 state.in.send(std::string("zoo"));
79 state.out.receive(&str);
80 EXPECT_STREQ("foo", str.c_str());
81 state.out.receive(&str);
82 EXPECT_STREQ("bar", str.c_str());
83 state.out.receive(&str);
84 EXPECT_STREQ("zoo", str.c_str());
86 state.in.send(std::string("quit"));
87 state.out.receive(&str);
88 EXPECT_STREQ("quit", str.c_str());