-
Notifications
You must be signed in to change notification settings - Fork 267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
selectabletimer: add mutex to start() and stop() #614
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -653,42 +653,6 @@ TEST(DBConnector, selectableevent) | |
EXPECT_EQ(value, 2); | ||
} | ||
|
||
TEST(DBConnector, selectabletimer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to timer_ut.cpp |
||
{ | ||
timespec interval = { .tv_sec = 1, .tv_nsec = 0 }; | ||
SelectableTimer timer(interval); | ||
|
||
Select s; | ||
s.addSelectable(&timer); | ||
Selectable *sel; | ||
int result; | ||
|
||
// Wait a non started timer | ||
result = s.select(&sel, 2000); | ||
ASSERT_EQ(result, Select::TIMEOUT); | ||
|
||
// Wait long enough so we got timer notification first | ||
timer.start(); | ||
result = s.select(&sel, 2000); | ||
ASSERT_EQ(result, Select::OBJECT); | ||
ASSERT_EQ(sel, &timer); | ||
|
||
// Wait short so we got select timeout first | ||
result = s.select(&sel, 10); | ||
ASSERT_EQ(result, Select::TIMEOUT); | ||
|
||
// Wait long enough so we got timer notification first | ||
result = s.select(&sel, 10000); | ||
ASSERT_EQ(result, Select::OBJECT); | ||
ASSERT_EQ(sel, &timer); | ||
|
||
// Reset and wait long enough so we got timer notification first | ||
timer.reset(); | ||
result = s.select(&sel, 10000); | ||
ASSERT_EQ(result, Select::OBJECT); | ||
ASSERT_EQ(sel, &timer); | ||
} | ||
|
||
TEST(Table, basic) | ||
{ | ||
TableBasicTest("TABLE_UT_TEST", true); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "common/select.h" | ||
#include "common/selectabletimer.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace std; | ||
using namespace swss; | ||
|
||
TEST(TIMER, selectabletimer) | ||
{ | ||
timespec interval = { .tv_sec = 1, .tv_nsec = 0 }; | ||
SelectableTimer timer(interval); | ||
|
||
Select s; | ||
s.addSelectable(&timer); | ||
Selectable *sel; | ||
int result; | ||
|
||
// Wait a non started timer | ||
result = s.select(&sel, 2000); | ||
ASSERT_EQ(result, Select::TIMEOUT); | ||
|
||
// Wait long enough so we got timer notification first | ||
timer.start(); | ||
result = s.select(&sel, 2000); | ||
ASSERT_EQ(result, Select::OBJECT); | ||
ASSERT_EQ(sel, &timer); | ||
|
||
// Wait short so we got select timeout first | ||
result = s.select(&sel, 10); | ||
ASSERT_EQ(result, Select::TIMEOUT); | ||
|
||
// Wait long enough so we got timer notification first | ||
result = s.select(&sel, 10000); | ||
ASSERT_EQ(result, Select::OBJECT); | ||
ASSERT_EQ(sel, &timer); | ||
|
||
// Reset and wait long enough so we got timer notification first | ||
timer.reset(); | ||
result = s.select(&sel, 10000); | ||
ASSERT_EQ(result, Select::OBJECT); | ||
ASSERT_EQ(sel, &timer); | ||
|
||
// Check if the timer gets reset by subsequent timer.start() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case of original code, since the timer is created with flag=0, a relative timer is started, and everytime that timer.start() gets invoked, timerfd_settime() will reset the timeout based on the current timestamp, hence it will cause unexpected delay here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean original code will fail on this line?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it'll fail at that line without the changes in the PR |
||
for (int t = 0; t < 2000; ++t) | ||
{ | ||
timer.start(); | ||
usleep(1000); | ||
} | ||
result = s.select(&sel, 1); | ||
ASSERT_EQ(result, Select::OBJECT); | ||
ASSERT_EQ(sel, &timer); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add new test case which will fail with original code? #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please refer to timer_ut.cpp: line 43-51