blob: 392b9eca0ac762f3ed3ccb0221b9fde6be2db1a7 [file] [log] [blame]
Tom Joseph74f27c72021-05-16 07:58:53 -07001#include "mock_request.hpp"
2
George Liuc453e162022-12-21 17:16:23 +08003#include <libpldm/base.h>
4
Tom Joseph74f27c72021-05-16 07:58:53 -07005#include <sdbusplus/timer.hpp>
6#include <sdeventplus/event.hpp>
7
8#include <gmock/gmock.h>
9#include <gtest/gtest.h>
10
11using namespace pldm::requester;
12using namespace std::chrono;
13using ::testing::AtLeast;
14using ::testing::Between;
15using ::testing::Exactly;
16using ::testing::Return;
17
18class RequestIntfTest : public testing::Test
19{
20 protected:
21 RequestIntfTest() : event(sdeventplus::Event::get_default())
22 {}
23
24 /** @brief This function runs the sd_event_run in a loop till all the events
25 * in the testcase are dispatched and exits when there are no events
26 * for the timeout time.
27 *
28 * @param[in] timeout - maximum time to wait for an event
29 */
30 void waitEventExpiry(milliseconds timeout)
31 {
32 while (1)
33 {
34 auto sleepTime = duration_cast<microseconds>(timeout);
35 // Returns 0 on timeout
36 if (!sd_event_run(event.get(), sleepTime.count()))
37 {
38 break;
39 }
40 }
41 }
42
43 int fd = 0;
44 mctp_eid_t eid = 0;
45 sdeventplus::Event event;
46 std::vector<uint8_t> requestMsg;
47};
48
49TEST_F(RequestIntfTest, 0Retries100msTimeout)
50{
51 MockRequest request(fd, eid, event, std::move(requestMsg), 0,
Manojkiran Eda9fffea22021-10-27 16:03:27 +053052 milliseconds(100), 90000, false);
Tom Joseph74f27c72021-05-16 07:58:53 -070053 EXPECT_CALL(request, send())
54 .Times(Exactly(1))
55 .WillOnce(Return(PLDM_SUCCESS));
56 auto rc = request.start();
Tom Josepha5ed6582021-06-17 22:08:47 -070057 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -070058}
59
60TEST_F(RequestIntfTest, 2Retries100msTimeout)
61{
62 MockRequest request(fd, eid, event, std::move(requestMsg), 2,
Manojkiran Eda9fffea22021-10-27 16:03:27 +053063 milliseconds(100), 90000, false);
Tom Joseph74f27c72021-05-16 07:58:53 -070064 // send() is called a total of 3 times, the original plus two retries
65 EXPECT_CALL(request, send()).Times(3).WillRepeatedly(Return(PLDM_SUCCESS));
66 auto rc = request.start();
Tom Josepha5ed6582021-06-17 22:08:47 -070067 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -070068 waitEventExpiry(milliseconds(500));
69}
70
71TEST_F(RequestIntfTest, 9Retries100msTimeoutRequestStoppedAfter1sec)
72{
73 MockRequest request(fd, eid, event, std::move(requestMsg), 9,
Manojkiran Eda9fffea22021-10-27 16:03:27 +053074 milliseconds(100), 90000, false);
Tom Joseph74f27c72021-05-16 07:58:53 -070075 // send() will be called a total of 10 times, the original plus 9 retries.
76 // In a ideal scenario send() would have been called 10 times in 1 sec (when
77 // the timer is stopped) with a timeout of 100ms. Because there are delays
78 // in dispatch, the range is kept between 5 and 10. This recreates the
79 // situation where the Instance ID expires before the all the retries have
80 // been completed and the timer is stopped.
81 EXPECT_CALL(request, send())
82 .Times(Between(5, 10))
83 .WillRepeatedly(Return(PLDM_SUCCESS));
84 auto rc = request.start();
Tom Josepha5ed6582021-06-17 22:08:47 -070085 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -070086
87 auto requestStopCallback = [&](void) { request.stop(); };
88 phosphor::Timer timer(event.get(), requestStopCallback);
89 timer.start(duration_cast<microseconds>(seconds(1)));
90
91 waitEventExpiry(milliseconds(500));
92}
93
94TEST_F(RequestIntfTest, 2Retries100msTimeoutsendReturnsError)
95{
96 MockRequest request(fd, eid, event, std::move(requestMsg), 2,
Manojkiran Eda9fffea22021-10-27 16:03:27 +053097 milliseconds(100), 90000, false);
Tom Joseph74f27c72021-05-16 07:58:53 -070098 EXPECT_CALL(request, send()).Times(Exactly(1)).WillOnce(Return(PLDM_ERROR));
99 auto rc = request.start();
Tom Josepha5ed6582021-06-17 22:08:47 -0700100 EXPECT_EQ(rc, PLDM_ERROR);
Tom Joseph74f27c72021-05-16 07:58:53 -0700101}