aws_ros_logger_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
23 #include <gtest/gtest.h>
24 #include <ros/callback_queue.h>
25 #include <ros/ros.h>
26 #include <rosgraph_msgs/Log.h>
27 #include <unistd.h>
28 
29 #include <iostream>
30 
31 using namespace Aws::Utils::Logging;
32 
33 typedef std::shared_ptr<AWSLogSystem> aws_logger_t;
34 
35 std::atomic<int> message_count;
36 
37 void CreateTestLog(aws_logger_t logger, const char * tag, const char * message,
38  Aws::Utils::Logging::LogLevel log_level)
39 {
40  logger->Log(log_level, tag, message);
41  message_count++;
42 }
43 
44 TEST(AWSROSLoggerTest, TestLogMethod)
45 {
46  aws_logger_t logger = std::make_shared<AWSROSLogger>(Aws::Utils::Logging::LogLevel::Error);
47 
48  // Log via AWSROSLogger logger class
49  CreateTestLog(logger, "test_tag_1", "test_log_message_1", Aws::Utils::Logging::LogLevel::Error);
50  usleep(3000000); // sleep for some time before sending second log.
51  CreateTestLog(logger, "test_tag_2", "test_log_message_2", Aws::Utils::Logging::LogLevel::Fatal);
52 }
53 
54 void RosoutLoggerCallback(const rosgraph_msgs::Log & published_log)
55 {
56  std::string expected_msg_1 = "[test_tag_1] test_log_message_1";
57  std::string expected_msg_2 = "[test_tag_2] test_log_message_2";
58 
59  EXPECT_EQ(published_log.msg.compare(expected_msg_1) == 0 ||
60  published_log.msg.compare(expected_msg_2) == 0,
61  true);
62  EXPECT_EQ(message_count > 0, true);
63 
64  message_count--;
65 }
66 
67 int main(int argc, char ** argv)
68 {
69  testing::InitGoogleTest(&argc, argv);
70  ros::init(argc, argv, "test_ros_logger");
71 
73 
74  // subscrive to /rosout where all the ROS_*** logs will be emitted.
75  // The callback extracts the recieved log message verifies the contents.
76  ros::Subscriber sub = n.subscribe("/rosout", 1000, RosoutLoggerCallback);
77  int ret = RUN_ALL_TESTS();
78 
79  // spin till there are no events in events queue OR we have recieved all the expected logs.
80  while (ros::ok()) {
82  if (message_count <= 0) {
83  break;
84  }
85  }
86 
87  return ret;
88 }
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void CreateTestLog(aws_logger_t logger, const char *tag, const char *message, Aws::Utils::Logging::LogLevel log_level)
ROSCPP_DECL CallbackQueue * getGlobalCallbackQueue()
std::shared_ptr< AWSLogSystem > aws_logger_t
TEST(AWSROSLoggerTest, TestLogMethod)
ROSCPP_DECL bool ok()
void RosoutLoggerCallback(const rosgraph_msgs::Log &published_log)
std::atomic< int > message_count
int main(int argc, char **argv)


aws_ros1_common
Author(s): AWS RoboMaker
autogenerated on Thu Mar 4 2021 03:25:44