Commit Graph

10 Commits

Author SHA1 Message Date
Tamir Duberstein 2c1df1f445 Use fake clocks in all tests
...except TCP tests and NDP tests that mutate globals. These will be
undertaken later.

Updates #5940.

PiperOrigin-RevId: 376145608
2021-05-27 05:06:04 -07:00
Tamir Duberstein fcad6f91a3 Use the stack clock everywhere
Updates #5939.
Updates #6012.

RELNOTES: n/a
PiperOrigin-RevId: 375931554
2021-05-26 06:49:57 -07:00
Tamir Duberstein 4f2439fb0e Use opaque types to represent time
Introduce tcpip.MonotonicTime; replace int64 in tcpip.Clock method
returns with time.Time and MonotonicTime to improve type safety and
ensure that monotonic clock readings are never compared to wall clock
readings.

PiperOrigin-RevId: 375775907
2021-05-25 13:00:29 -07:00
Tamir Duberstein 74b10e31a4 Remove detritus
- Unused constants
- Unused functions
- Unused arguments
- Unkeyed literals
- Unnecessary conversions

PiperOrigin-RevId: 375253464
2021-05-22 05:49:34 -07:00
Ghanan Gowripalan 279f9fcee7 Implement standard clock safely
Previously, tcpip.StdClock depended on linking with the unexposed method
time.now to implement tcpip.Clock using the time package. This change
updates the standard clock to not require manually linking to this
unexported method and use publicly documented functions from the time
package.

PiperOrigin-RevId: 371805101
2021-05-03 16:45:48 -07:00
Sam Balana 82a5cada59 Add AfterFunc to tcpip.Clock
Changes the API of tcpip.Clock to also provide a method for scheduling and
rescheduling work after a specified duration. This change also implements the
AfterFunc method for existing implementations of tcpip.Clock.

This is the groundwork required to mock time within tests. All references to
CancellableTimer has been replaced with the tcpip.Job interface, allowing for
custom implementations of scheduling work.

This is a BREAKING CHANGE for clients that implement their own tcpip.Clock or
use tcpip.CancellableTimer. Migration plan:
 1. Add AfterFunc(d, f) to tcpip.Clock
 2. Replace references of tcpip.CancellableTimer with tcpip.Job
 3. Replace calls to tcpip.CancellableTimer#StopLocked with tcpip.Job#Cancel
 4. Replace calls to tcpip.CancellableTimer#Reset with tcpip.Job#Schedule
 5. Replace calls to tcpip.NewCancellableTimer with tcpip.NewJob.

PiperOrigin-RevId: 322906897
2020-07-23 18:00:43 -07:00
Ghanan Gowripalan 1ceee04529 Do not copy tcpip.CancellableTimer
A CancellableTimer's AfterFunc timer instance creates a closure over the
CancellableTimer's address. This closure makes a CancellableTimer unsafe
to copy.

No behaviour change, existing tests pass.

PiperOrigin-RevId: 308306664
2020-04-24 12:46:56 -07:00
Ghanan Gowripalan 782041509f Prevent race when reassigning CancellableTimer
Capture a timer's locker for each instance of a CancellableTimer so that
reassigning a tcpip.CancellableTimer does not cause a data race.

Reassigning a tcpip.CancellableTimer updates its underlying locker. When
a timer fires, it does a read of the timer's locker variable to lock it.
This read of the locker was not synchronized so a race existed where one
goroutine may reassign the timer (updating the locker) and another
handles the timer firing (attempts to lock the timer's locker).

Test: tcpip_test.TestCancellableTimerReassignment
PiperOrigin-RevId: 307499822
2020-04-20 16:32:44 -07:00
Ghanan Gowripalan bcedf6a8e4 Put CancellableTimer tests in the tcpip_test package
CancellableTimer tests were in a timer_test package but lived within the
tcpip directory. This caused issues with go tools.

PiperOrigin-RevId: 289166345
2020-01-10 14:32:17 -08:00
Ghanan Gowripalan d057871f41 CancellableTimer to encapsulate the work of safely stopping timers
Add a new CancellableTimer type to encapsulate the work of safely stopping
timers when it fires at the same time some "related work" is being handled. The
term "related work" is some work that needs to be done while having obtained
some common lock (L).

Example: Say we have an invalidation timer that may be extended or cancelled by
some event. Creating a normal timer and simply cancelling may not be sufficient
as the timer may have already fired when the event handler attemps to cancel it.
Even if the timer and event handler obtains L before doing work, once the event
handler releases L, the timer will eventually obtain L and do some unwanted
work.

To prevent the timer from doing unwanted work, it checks if it should early
return instead of doing the normal work after obtaining L. When stopping the
timer callers must have L locked so the timer can be safely informed that it
should early return.

Test: Tests that CancellableTimer fires and resets properly. Test to make sure
the timer fn is not called after being stopped within the lock L.
PiperOrigin-RevId: 288806984
2020-01-08 17:50:54 -08:00