fix(channels): stop stale typing loops on overwrite (#1392)
Co-authored-by: XYSK-lilong007 <267018309+XYSK-lilong007@users.noreply.github.com>
This commit is contained in:
parent
3d89604019
commit
15ccf457ac
2 changed files with 37 additions and 1 deletions
|
|
@ -127,7 +127,12 @@ func (m *Manager) SendPlaceholder(ctx context.Context, channel, chatID string) b
|
||||||
// Implements PlaceholderRecorder.
|
// Implements PlaceholderRecorder.
|
||||||
func (m *Manager) RecordTypingStop(channel, chatID string, stop func()) {
|
func (m *Manager) RecordTypingStop(channel, chatID string, stop func()) {
|
||||||
key := channel + ":" + chatID
|
key := channel + ":" + chatID
|
||||||
m.typingStops.Store(key, typingEntry{stop: stop, createdAt: time.Now()})
|
entry := typingEntry{stop: stop, createdAt: time.Now()}
|
||||||
|
if previous, loaded := m.typingStops.Swap(key, entry); loaded {
|
||||||
|
if oldEntry, ok := previous.(typingEntry); ok && oldEntry.stop != nil {
|
||||||
|
oldEntry.stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RecordReactionUndo registers a reaction undo function for later invocation.
|
// RecordReactionUndo registers a reaction undo function for later invocation.
|
||||||
|
|
|
||||||
|
|
@ -616,6 +616,37 @@ func TestRecordTypingStop_ConcurrentSafe(t *testing.T) {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRecordTypingStop_ReplacesExistingStop(t *testing.T) {
|
||||||
|
m := newTestManager()
|
||||||
|
var oldStopCalls int
|
||||||
|
var newStopCalls int
|
||||||
|
|
||||||
|
m.RecordTypingStop("test", "123", func() {
|
||||||
|
oldStopCalls++
|
||||||
|
})
|
||||||
|
|
||||||
|
m.RecordTypingStop("test", "123", func() {
|
||||||
|
newStopCalls++
|
||||||
|
})
|
||||||
|
|
||||||
|
if oldStopCalls != 1 {
|
||||||
|
t.Fatalf("expected previous typing stop to be called once when replaced, got %d", oldStopCalls)
|
||||||
|
}
|
||||||
|
if newStopCalls != 0 {
|
||||||
|
t.Fatalf("expected replacement typing stop to stay active until preSend, got %d calls", newStopCalls)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := bus.OutboundMessage{Channel: "test", ChatID: "123", Content: "hello"}
|
||||||
|
m.preSend(context.Background(), "test", msg, &mockChannel{})
|
||||||
|
|
||||||
|
if newStopCalls != 1 {
|
||||||
|
t.Fatalf("expected replacement typing stop to be called by preSend, got %d", newStopCalls)
|
||||||
|
}
|
||||||
|
if oldStopCalls != 1 {
|
||||||
|
t.Fatalf("expected previous typing stop to not be called again, got %d", oldStopCalls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSendWithRetry_PreSendEditsPlaceholder(t *testing.T) {
|
func TestSendWithRetry_PreSendEditsPlaceholder(t *testing.T) {
|
||||||
m := newTestManager()
|
m := newTestManager()
|
||||||
var sendCalled bool
|
var sendCalled bool
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue