chanfitness: make online period calculation tolerant of duplicates

To get our uptime, we first filter our event log to get online periods.
This change updates this code to be tolerant of consecutive online or
offline events in the log. This will be required for rate limiting,
because we will not record every event for anti-dos reasons, so we could
record an online event, ignore an offline event and then record another
offline event. We could just ignore this duplicate event, but we will
also need this tolerance for when we persist uptime and our peers
can have their last event before restart as an online event and record
another online event when we come back up.
This commit is contained in:
carla committed 2020-09-08 13:47:16 +02:00
1 parent c33d0aad46
commit 7930ef7cf4
2 files changed
+164 -36

No files matched your search

+106 -5
View File
@@ -72,10 +72,10 @@ func TestGetOnlinePeriod(t *testing.T) {
closedAt time.Time
}{
{
name: "No events",
name: "no events",
},
{
name: "Start on online period",
name: "start on online period",
events: []*channelEvent{
{
timestamp: threeHoursAgo,
@@ -94,7 +94,7 @@ func TestGetOnlinePeriod(t *testing.T) {
},
},
{
name: "Start on offline period",
name: "start on offline period",
events: []*channelEvent{
{
timestamp: fourHoursAgo,
@@ -103,7 +103,7 @@ func TestGetOnlinePeriod(t *testing.T) {
},
},
{
name: "End on an online period, channel not closed",
name: "end on an online period, channel not closed",
events: []*channelEvent{
{
timestamp: fourHoursAgo,
@@ -118,7 +118,7 @@ func TestGetOnlinePeriod(t *testing.T) {
},
},
{
name: "End on an online period, channel closed",
name: "end on an online period, channel closed",
events: []*channelEvent{
{
timestamp: fourHoursAgo,
@@ -133,12 +133,113 @@ func TestGetOnlinePeriod(t *testing.T) {
},
closedAt: oneHourAgo,
},
{
name: "duplicate online events, channel not closed",
events: []*channelEvent{
{
timestamp: fourHoursAgo,
eventType: peerOnlineEvent,
},
{
timestamp: threeHoursAgo,
eventType: peerOnlineEvent,
},
},
expectedOnline: []*onlinePeriod{
{
start: fourHoursAgo,
end: testNow,
},
},
},
{
name: "duplicate online events, channel closed",
events: []*channelEvent{
{
timestamp: fourHoursAgo,
eventType: peerOnlineEvent,
},
{
timestamp: twoHoursAgo,
eventType: peerOnlineEvent,
},
},
expectedOnline: []*onlinePeriod{
{
start: fourHoursAgo,
end: threeHoursAgo,
},
},
closedAt: threeHoursAgo,
},
{
name: "duplicate offline events, channel not closed",
events: []*channelEvent{
{
timestamp: fourHoursAgo,
eventType: peerOfflineEvent,
},
{
timestamp: threeHoursAgo,
eventType: peerOfflineEvent,
},
},
expectedOnline: nil,
},
{
name: "duplicate online then offline",
events: []*channelEvent{
{
timestamp: fourHoursAgo,
eventType: peerOnlineEvent,
},
{
timestamp: threeHoursAgo,
eventType: peerOnlineEvent,
},
{
timestamp: twoHoursAgo,
eventType: peerOfflineEvent,
},
},
expectedOnline: []*onlinePeriod{
{
start: fourHoursAgo,
end: twoHoursAgo,
},
},
},
{
name: "duplicate offline then online",
events: []*channelEvent{
{
timestamp: fourHoursAgo,
eventType: peerOfflineEvent,
},
{
timestamp: threeHoursAgo,
eventType: peerOfflineEvent,
},
{
timestamp: twoHoursAgo,
eventType: peerOnlineEvent,
},
},
expectedOnline: []*onlinePeriod{
{
start: twoHoursAgo,
end: testNow,
},
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
score := &chanEventLog{
events: test.events,
clock: clock.NewTestClock(testNow),