You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tcp_server_tests.cc 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "ps/comm/tcp_client.h"
  17. #include "ps/comm/tcp_server.h"
  18. #include "common/common_test.h"
  19. #include <thread>
  20. namespace mindspore {
  21. namespace ps {
  22. namespace comm {
  23. class TestTcpServer : public UT::Common {
  24. public:
  25. TestTcpServer() = default;
  26. void SetUp() override {
  27. server_ = new TcpServer("127.0.0.1", 9000);
  28. std::unique_ptr<std::thread> http_server_thread_(nullptr);
  29. http_server_thread_ = std::make_unique<std::thread>([&]() {
  30. server_->ReceiveMessage([](const TcpServer &server, const TcpConnection &conn, const void *buffer, size_t num) {
  31. EXPECT_STREQ(std::string(reinterpret_cast<const char *>(buffer), num).c_str(), "TCP_MESSAGE");
  32. server.SendMessage(conn, buffer, num);
  33. });
  34. server_->InitServer();
  35. server_->Start();
  36. });
  37. http_server_thread_->detach();
  38. std::this_thread::sleep_for(std::chrono::milliseconds(2000));
  39. }
  40. void TearDown() override {
  41. std::this_thread::sleep_for(std::chrono::milliseconds(2000));
  42. client_->Stop();
  43. std::this_thread::sleep_for(std::chrono::milliseconds(2000));
  44. server_->Stop();
  45. }
  46. TcpClient *client_;
  47. TcpServer *server_;
  48. const std::string test_message_ = "TCP_MESSAGE";
  49. };
  50. TEST_F(TestTcpServer, ServerSendMessage) {
  51. client_ = new TcpClient("127.0.0.1", 9000);
  52. std::unique_ptr<std::thread> http_client_thread(nullptr);
  53. http_client_thread = std::make_unique<std::thread>([&]() {
  54. client_->ReceiveMessage([](const TcpClient &client, const void *buffer, size_t num) {
  55. EXPECT_STREQ(std::string(reinterpret_cast<const char *>(buffer), num).c_str(), "TCP_MESSAGE");
  56. });
  57. client_->InitTcpClient();
  58. client_->SendMessage(test_message_.c_str(), test_message_.size());
  59. client_->Start();
  60. });
  61. http_client_thread->detach();
  62. }
  63. } // namespace comm
  64. } // namespace ps
  65. } // namespace mindspore