blob: 2e3060df6aec96f77e0aaff8d5a9f362452e0a3c [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:
Patrick Williams6da4f912023-05-10 07:50:53 -050021 RequestIntfTest() : event(sdeventplus::Event::get_default()) {}
Tom Joseph74f27c72021-05-16 07:58:53 -070022
23 /** @brief This function runs the sd_event_run in a loop till all the events
24 * in the testcase are dispatched and exits when there are no events
25 * for the timeout time.
26 *
27 * @param[in] timeout - maximum time to wait for an event
28 */
29 void waitEventExpiry(milliseconds timeout)
30 {
31 while (1)
32 {
33 auto sleepTime = duration_cast<microseconds>(timeout);
34 // Returns 0 on timeout
35 if (!sd_event_run(event.get(), sleepTime.count()))
36 {
37 break;
38 }
39 }
40 }
41
42 int fd = 0;
43 mctp_eid_t eid = 0;
44 sdeventplus::Event event;
45 std::vector<uint8_t> requestMsg;
46};
47
48TEST_F(RequestIntfTest, 0Retries100msTimeout)
49{
50 MockRequest request(fd, eid, event, std::move(requestMsg), 0,
Manojkiran Eda9fffea22021-10-27 16:03:27 +053051 milliseconds(100), 90000, false);
Tom Joseph74f27c72021-05-16 07:58:53 -070052 EXPECT_CALL(request, send())
53 .Times(Exactly(1))
54 .WillOnce(Return(PLDM_SUCCESS));
55 auto rc = request.start();
Tom Josepha5ed6582021-06-17 22:08:47 -070056 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -070057}
58
59TEST_F(RequestIntfTest, 2Retries100msTimeout)
60{
61 MockRequest request(fd, eid, event, std::move(requestMsg), 2,
Manojkiran Eda9fffea22021-10-27 16:03:27 +053062 milliseconds(100), 90000, false);
Tom Joseph74f27c72021-05-16 07:58:53 -070063 // send() is called a total of 3 times, the original plus two retries
64 EXPECT_CALL(request, send()).Times(3).WillRepeatedly(Return(PLDM_SUCCESS));
65 auto rc = request.start();
Tom Josepha5ed6582021-06-17 22:08:47 -070066 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -070067 waitEventExpiry(milliseconds(500));
68}
69
70TEST_F(RequestIntfTest, 9Retries100msTimeoutRequestStoppedAfter1sec)
71{
72 MockRequest request(fd, eid, event, std::move(requestMsg), 9,
Manojkiran Eda9fffea22021-10-27 16:03:27 +053073 milliseconds(100), 90000, false);
Tom Joseph74f27c72021-05-16 07:58:53 -070074 // send() will be called a total of 10 times, the original plus 9 retries.
75 // In a ideal scenario send() would have been called 10 times in 1 sec (when
76 // the timer is stopped) with a timeout of 100ms. Because there are delays
77 // in dispatch, the range is kept between 5 and 10. This recreates the
78 // situation where the Instance ID expires before the all the retries have
79 // been completed and the timer is stopped.
80 EXPECT_CALL(request, send())
81 .Times(Between(5, 10))
82 .WillRepeatedly(Return(PLDM_SUCCESS));
83 auto rc = request.start();
Tom Josepha5ed6582021-06-17 22:08:47 -070084 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -070085
86 auto requestStopCallback = [&](void) { request.stop(); };
87 phosphor::Timer timer(event.get(), requestStopCallback);
88 timer.start(duration_cast<microseconds>(seconds(1)));
89
90 waitEventExpiry(milliseconds(500));
91}
92
93TEST_F(RequestIntfTest, 2Retries100msTimeoutsendReturnsError)
94{
95 MockRequest request(fd, eid, event, std::move(requestMsg), 2,
Manojkiran Eda9fffea22021-10-27 16:03:27 +053096 milliseconds(100), 90000, false);
Tom Joseph74f27c72021-05-16 07:58:53 -070097 EXPECT_CALL(request, send()).Times(Exactly(1)).WillOnce(Return(PLDM_ERROR));
98 auto rc = request.start();
Tom Josepha5ed6582021-06-17 22:08:47 -070099 EXPECT_EQ(rc, PLDM_ERROR);
Tom Joseph74f27c72021-05-16 07:58:53 -0700100}