X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=src%2Ftype3_AndroidCloud%2Fanbox-master%2Ftests%2Fanbox%2Fcommon%2Fmessage_channel_tests.cpp;fp=src%2Ftype3_AndroidCloud%2Fanbox-master%2Ftests%2Fanbox%2Fcommon%2Fmessage_channel_tests.cpp;h=e81181a6e002a2173a8f1d82a01c48b71c709aaf;hb=e26c1ec581be598521517829adba8c8dd23a768f;hp=0000000000000000000000000000000000000000;hpb=6699c1aea74eeb0eb400e6299079f0c7576f716f;p=iec.git diff --git a/src/type3_AndroidCloud/anbox-master/tests/anbox/common/message_channel_tests.cpp b/src/type3_AndroidCloud/anbox-master/tests/anbox/common/message_channel_tests.cpp new file mode 100644 index 0000000..e81181a --- /dev/null +++ b/src/type3_AndroidCloud/anbox-master/tests/anbox/common/message_channel_tests.cpp @@ -0,0 +1,94 @@ +// Copyright 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "anbox/common/message_channel.h" + +#include + +#include +#include + +namespace anbox { +namespace common { +TEST(MessageChannel, SingleThreadWithInt) { + MessageChannel channel; + channel.send(1); + channel.send(2); + channel.send(3); + + int ret; + channel.receive(&ret); + EXPECT_EQ(1, ret); + channel.receive(&ret); + EXPECT_EQ(2, ret); + channel.receive(&ret); + EXPECT_EQ(3, ret); +} + +TEST(MessageChannel, SingleThreadWithStdString) { + MessageChannel channel; + channel.send(std::string("foo")); + channel.send(std::string("bar")); + channel.send(std::string("zoo")); + + std::string str; + channel.receive(&str); + EXPECT_STREQ("foo", str.c_str()); + channel.receive(&str); + EXPECT_STREQ("bar", str.c_str()); + channel.receive(&str); + EXPECT_STREQ("zoo", str.c_str()); +} + +TEST(MessageChannel, TwoThreadsPingPong) { + struct PingPongState { + MessageChannel in; + MessageChannel out; + }; + + PingPongState state; + + std::thread thread([&](){ + for (;;) { + std::string str; + state.in.receive(&str); + state.out.send(str); + if (str == "quit") + break; + } + return 0; + }); + + std::string str; + const size_t kCount = 100; + for (size_t n = 0; n < kCount; ++n) { + state.in.send(std::string("foo")); + state.in.send(std::string("bar")); + state.in.send(std::string("zoo")); + state.out.receive(&str); + EXPECT_STREQ("foo", str.c_str()); + state.out.receive(&str); + EXPECT_STREQ("bar", str.c_str()); + state.out.receive(&str); + EXPECT_STREQ("zoo", str.c_str()); + } + state.in.send(std::string("quit")); + state.out.receive(&str); + EXPECT_STREQ("quit", str.c_str()); + + thread.join(); +} + +} // namespace common +} // namespace anbox