This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server: catch up on missed ticks (#218)
* server: warn on missed ticks On heavy load, or when upgrading the server, the plugin might not tick every seconds. To better quantify this, log when some ticks are missed. * server: catch up on missed ticks Catches up ticks up to 10 minutes (as longer may overload the server when starting up after being stopped a very long time). If it takes longer to catch up for ticks that a tick, the missed ticks will be caught up on the next turn.
- Loading branch information
Showing
2 changed files
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,55 @@ import ( | |
) | ||
|
||
func TestTriggerReminders(t *testing.T) { | ||
testTime := time.Now().UTC().Round(time.Second) | ||
serializedTestTime := []byte(testTime.Format(time.RFC3339)) | ||
|
||
t.Run("it triggers reminders scheduled for the current time", func(t *testing.T) { | ||
oneSecondAgo, _ := time.ParseDuration("-1s") | ||
lastTickAt := testTime.Add(oneSecondAgo) | ||
serializedLastTickAt := []byte(lastTickAt.Format(time.RFC3339)) | ||
|
||
api := &plugintest.API{} | ||
api.On("KVGet", string("LastTickAt")).Return(serializedLastTickAt, nil) | ||
api.On("KVSet", string("LastTickAt"), serializedTestTime).Return(nil) | ||
api.On("LogDebug", "Trigger reminders for " + fmt.Sprintf("%v", testTime)) | ||
api.On("KVGet", string(fmt.Sprintf("%v", testTime))).Return(nil, nil) | ||
defer api.AssertExpectations(t) | ||
|
||
p := &Plugin{} | ||
p.API = api | ||
|
||
p.TriggerReminders() | ||
}) | ||
|
||
t.Run("when ticks have been missed, it triggers reminders for the missed ticks as well", func(t *testing.T) { | ||
oneSecondsAgo, _ := time.ParseDuration("-1s") | ||
twoSecondsAgo, _ := time.ParseDuration("-2s") | ||
threeSecondsAgo, _ := time.ParseDuration("-3s") | ||
lastTickAt := testTime.Add(threeSecondsAgo) | ||
serializedLastTickAt := []byte(lastTickAt.Format(time.RFC3339)) | ||
|
||
api := &plugintest.API{} | ||
api.On("KVGet", string("LastTickAt")).Return(serializedLastTickAt, nil) | ||
api.On("KVSet", string("LastTickAt"), serializedTestTime).Return(nil) | ||
api.On("LogDebug", "Catching up on 2 reminder tick(s)...") | ||
api.On("LogDebug", "Trigger reminders for " + fmt.Sprintf("%v", testTime.Add(twoSecondsAgo))) | ||
api.On("KVGet", string(fmt.Sprintf("%v", testTime.Add(twoSecondsAgo)))).Return(nil, nil) | ||
api.On("LogDebug", "Trigger reminders for " + fmt.Sprintf("%v", testTime.Add(oneSecondsAgo))) | ||
api.On("KVGet", string(fmt.Sprintf("%v", testTime.Add(oneSecondsAgo)))).Return(nil, nil) | ||
api.On("LogDebug", "Caught up on missed reminder ticks.") | ||
api.On("LogDebug", "Trigger reminders for " + fmt.Sprintf("%v", testTime)) | ||
api.On("KVGet", string(fmt.Sprintf("%v", testTime))).Return(nil, nil) | ||
defer api.AssertExpectations(t) | ||
|
||
p := &Plugin{} | ||
p.API = api | ||
|
||
p.TriggerReminders() | ||
}) | ||
} | ||
|
||
func TestTriggerRemindersForTick(t *testing.T) { | ||
|
||
user := &model.User{ | ||
Email: "[email protected]", | ||
|
@@ -82,13 +131,14 @@ func TestTriggerReminders(t *testing.T) { | |
|
||
stringOccurrences, _ := json.Marshal(occurrences) | ||
api := &plugintest.API{} | ||
api.On("LogDebug", mock.Anything, mock.Anything, mock.Anything).Maybe() | ||
api.On("KVGet", string(fmt.Sprintf("%v", testTime))).Return(stringOccurrences, nil) | ||
defer api.AssertExpectations(t) | ||
|
||
p := &Plugin{} | ||
p.API = api | ||
|
||
p.TriggerReminders() | ||
p.TriggerRemindersForTick(testTime) | ||
|
||
}) | ||
|
||
|
@@ -102,7 +152,7 @@ func TestTriggerReminders(t *testing.T) { | |
p := &Plugin{} | ||
p.API = api | ||
|
||
p.TriggerReminders() | ||
p.TriggerRemindersForTick(testTime) | ||
|
||
}) | ||
|
||
|
@@ -117,7 +167,7 @@ func TestTriggerReminders(t *testing.T) { | |
p := &Plugin{} | ||
p.API = api | ||
|
||
p.TriggerReminders() | ||
p.TriggerRemindersForTick(testTime) | ||
|
||
}) | ||
|
||
|
@@ -132,7 +182,7 @@ func TestTriggerReminders(t *testing.T) { | |
p := &Plugin{} | ||
p.API = api | ||
|
||
p.TriggerReminders() | ||
p.TriggerRemindersForTick(testTime) | ||
|
||
}) | ||
|
||
|
@@ -178,7 +228,7 @@ func TestTriggerReminders(t *testing.T) { | |
p := &Plugin{} | ||
p.API = api | ||
|
||
p.TriggerReminders() | ||
p.TriggerRemindersForTick(testTime) | ||
|
||
}) | ||
|
||
|
@@ -222,7 +272,7 @@ func TestTriggerReminders(t *testing.T) { | |
p := &Plugin{} | ||
p.API = api | ||
|
||
p.TriggerReminders() | ||
p.TriggerRemindersForTick(testTime) | ||
|
||
}) | ||
|
||
|